Merge "Allow __NR_pipe for 32-bit processes"
diff --git a/adb/Android.mk b/adb/Android.mk
index 41cb735..b444afa 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -81,10 +81,14 @@
LIBADB_darwin_SRC_FILES := \
sysdeps_unix.cpp \
+ client/usb_dispatch.cpp \
+ client/usb_libusb.cpp \
client/usb_osx.cpp \
LIBADB_linux_SRC_FILES := \
sysdeps_unix.cpp \
+ client/usb_dispatch.cpp \
+ client/usb_libusb.cpp \
client/usb_linux.cpp \
LIBADB_windows_SRC_FILES := \
@@ -150,6 +154,8 @@
# Even though we're building a static library (and thus there's no link step for
# this to take effect), this adds the includes to our path.
LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase
+LOCAL_STATIC_LIBRARIES_linux := libusb
+LOCAL_STATIC_LIBRARIES_darwin := libusb
LOCAL_C_INCLUDES_windows := development/host/windows/usb/api/
LOCAL_MULTILIB := first
@@ -169,7 +175,7 @@
shell_service_test.cpp \
LOCAL_SANITIZE := $(adb_target_sanitize)
-LOCAL_STATIC_LIBRARIES := libadbd libcrypto_utils libcrypto
+LOCAL_STATIC_LIBRARIES := libadbd libcrypto_utils libcrypto libusb
LOCAL_SHARED_LIBRARIES := liblog libbase libcutils
include $(BUILD_NATIVE_TEST)
@@ -219,10 +225,13 @@
libdiagnose_usb \
libgmock_host \
+LOCAL_STATIC_LIBRARIES_linux := libusb
+LOCAL_STATIC_LIBRARIES_darwin := libusb
+
# Set entrypoint to wmain from sysdeps_win32.cpp instead of main
LOCAL_LDFLAGS_windows := -municode
LOCAL_LDLIBS_linux := -lrt -ldl -lpthread
-LOCAL_LDLIBS_darwin := -framework CoreFoundation -framework IOKit
+LOCAL_LDLIBS_darwin := -framework CoreFoundation -framework IOKit -lobjc
LOCAL_LDLIBS_windows := -lws2_32 -luserenv
LOCAL_STATIC_LIBRARIES_windows := AdbWinApi
@@ -236,7 +245,7 @@
LOCAL_LDLIBS_linux := -lrt -ldl -lpthread
-LOCAL_LDLIBS_darwin := -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
+LOCAL_LDLIBS_darwin := -lpthread -framework CoreFoundation -framework IOKit -framework Carbon -lobjc
# Use wmain instead of main
LOCAL_LDFLAGS_windows := -municode
@@ -287,6 +296,9 @@
LOCAL_STATIC_LIBRARIES_darwin := libcutils
LOCAL_STATIC_LIBRARIES_linux := libcutils
+LOCAL_STATIC_LIBRARIES_darwin += libusb
+LOCAL_STATIC_LIBRARIES_linux += libusb
+
LOCAL_CXX_STL := libc++_static
# Don't add anything here, we don't want additional shared dependencies
diff --git a/adb/adb.cpp b/adb/adb.cpp
index 3cd50ba..ece143c 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -40,6 +40,7 @@
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/parsenetaddress.h>
+#include <android-base/quick_exit.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
@@ -1047,7 +1048,7 @@
// may not read the OKAY sent above.
adb_shutdown(reply_fd);
- exit(0);
+ android::base::quick_exit(0);
}
#if ADB_HOST
diff --git a/adb/adb.h b/adb/adb.h
index 7db3b15..6a38f18 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -28,6 +28,7 @@
#include "adb_trace.h"
#include "fdevent.h"
#include "socket.h"
+#include "usb.h"
constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024;
@@ -54,7 +55,6 @@
#define ADB_SERVER_VERSION 38
class atransport;
-struct usb_handle;
struct amessage {
uint32_t command; /* command identifier constant */
@@ -195,18 +195,6 @@
bool local_connect(int port);
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
-// USB host/client interface.
-void usb_init();
-int usb_write(usb_handle *h, const void *data, int len);
-int usb_read(usb_handle *h, void *data, int len);
-int usb_close(usb_handle *h);
-void usb_kick(usb_handle *h);
-
-// USB device detection.
-#if ADB_HOST
-int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol);
-#endif
-
ConnectionState connection_state(atransport *t);
extern const char* adb_device_banner;
diff --git a/adb/client/main.cpp b/adb/client/main.cpp
index d583516..97a54fd 100644
--- a/adb/client/main.cpp
+++ b/adb/client/main.cpp
@@ -28,6 +28,7 @@
#include <android-base/errors.h>
#include <android-base/file.h>
#include <android-base/logging.h>
+#include <android-base/quick_exit.h>
#include <android-base/stringprintf.h>
#include "adb.h"
@@ -86,7 +87,7 @@
static BOOL WINAPI ctrlc_handler(DWORD type) {
// TODO: Consider trying to kill a starting up adb server (if we're in
// launch_server) by calling GenerateConsoleCtrlEvent().
- exit(STATUS_CONTROL_C_EXIT);
+ android::base::quick_exit(STATUS_CONTROL_C_EXIT);
return TRUE;
}
#endif
@@ -108,6 +109,10 @@
}
SetConsoleCtrlHandler(ctrlc_handler, TRUE);
+#else
+ signal(SIGINT, [](int) {
+ android::base::quick_exit(0);
+ });
#endif
init_transport_registration();
diff --git a/adb/client/usb_dispatch.cpp b/adb/client/usb_dispatch.cpp
new file mode 100644
index 0000000..597e66e
--- /dev/null
+++ b/adb/client/usb_dispatch.cpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2017 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 <android-base/logging.h>
+#include "usb.h"
+
+static bool should_use_libusb() {
+ static bool enable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "1") == 0;
+ return enable;
+}
+
+void usb_init() {
+ if (should_use_libusb()) {
+ LOG(INFO) << "using libusb backend";
+ libusb::usb_init();
+ } else {
+ LOG(INFO) << "using native backend";
+ native::usb_init();
+ }
+}
+
+int usb_write(usb_handle* h, const void* data, int len) {
+ return should_use_libusb()
+ ? libusb::usb_write(reinterpret_cast<libusb::usb_handle*>(h), data, len)
+ : native::usb_write(reinterpret_cast<native::usb_handle*>(h), data, len);
+}
+
+int usb_read(usb_handle* h, void* data, int len) {
+ return should_use_libusb()
+ ? libusb::usb_read(reinterpret_cast<libusb::usb_handle*>(h), data, len)
+ : native::usb_read(reinterpret_cast<native::usb_handle*>(h), data, len);
+}
+
+int usb_close(usb_handle* h) {
+ return should_use_libusb() ? libusb::usb_close(reinterpret_cast<libusb::usb_handle*>(h))
+ : native::usb_close(reinterpret_cast<native::usb_handle*>(h));
+}
+
+void usb_kick(usb_handle* h) {
+ should_use_libusb() ? libusb::usb_kick(reinterpret_cast<libusb::usb_handle*>(h))
+ : native::usb_kick(reinterpret_cast<native::usb_handle*>(h));
+}
diff --git a/adb/client/usb_libusb.cpp b/adb/client/usb_libusb.cpp
new file mode 100644
index 0000000..7adb262
--- /dev/null
+++ b/adb/client/usb_libusb.cpp
@@ -0,0 +1,519 @@
+/*
+ * Copyright (C) 2016 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 "usb.h"
+
+#include "sysdeps.h"
+
+#include <stdint.h>
+
+#include <atomic>
+#include <chrono>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <thread>
+#include <unordered_map>
+
+#include <libusb/libusb.h>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/quick_exit.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+
+#include "adb.h"
+#include "transport.h"
+#include "usb.h"
+
+using namespace std::literals;
+
+using android::base::StringPrintf;
+
+// RAII wrappers for libusb.
+struct ConfigDescriptorDeleter {
+ void operator()(libusb_config_descriptor* desc) {
+ libusb_free_config_descriptor(desc);
+ }
+};
+
+using unique_config_descriptor = std::unique_ptr<libusb_config_descriptor, ConfigDescriptorDeleter>;
+
+struct DeviceHandleDeleter {
+ void operator()(libusb_device_handle* h) {
+ libusb_close(h);
+ }
+};
+
+using unique_device_handle = std::unique_ptr<libusb_device_handle, DeviceHandleDeleter>;
+
+struct transfer_info {
+ transfer_info(const char* name, uint16_t zero_mask) :
+ name(name),
+ transfer(libusb_alloc_transfer(0)),
+ zero_mask(zero_mask)
+ {
+ }
+
+ ~transfer_info() {
+ libusb_free_transfer(transfer);
+ }
+
+ const char* name;
+ libusb_transfer* transfer;
+ bool transfer_complete;
+ std::condition_variable cv;
+ std::mutex mutex;
+ uint16_t zero_mask;
+
+ void Notify() {
+ LOG(DEBUG) << "notifying " << name << " transfer complete";
+ transfer_complete = true;
+ cv.notify_one();
+ }
+};
+
+namespace libusb {
+struct usb_handle : public ::usb_handle {
+ usb_handle(const std::string& device_address, const std::string& serial,
+ unique_device_handle&& device_handle, uint8_t interface, uint8_t bulk_in,
+ uint8_t bulk_out, size_t zero_mask)
+ : device_address(device_address),
+ serial(serial),
+ closing(false),
+ device_handle(device_handle.release()),
+ read("read", zero_mask),
+ write("write", zero_mask),
+ interface(interface),
+ bulk_in(bulk_in),
+ bulk_out(bulk_out) {
+ }
+
+ ~usb_handle() {
+ Close();
+ }
+
+ void Close() {
+ std::unique_lock<std::mutex> lock(device_handle_mutex);
+ // Cancelling transfers will trigger more Closes, so make sure this only happens once.
+ if (closing) {
+ return;
+ }
+ closing = true;
+
+ // Make sure that no new transfers come in.
+ libusb_device_handle* handle = device_handle;
+ if (!handle) {
+ return;
+ }
+
+ device_handle = nullptr;
+
+ // Cancel already dispatched transfers.
+ libusb_cancel_transfer(read.transfer);
+ libusb_cancel_transfer(write.transfer);
+
+ libusb_release_interface(handle, interface);
+ libusb_close(handle);
+ }
+
+ std::string device_address;
+ std::string serial;
+
+ std::atomic<bool> closing;
+ std::mutex device_handle_mutex;
+ libusb_device_handle* device_handle;
+
+ transfer_info read;
+ transfer_info write;
+
+ uint8_t interface;
+ uint8_t bulk_in;
+ uint8_t bulk_out;
+};
+
+static auto& usb_handles = *new std::unordered_map<std::string, std::unique_ptr<usb_handle>>();
+static auto& usb_handles_mutex = *new std::mutex();
+
+static std::thread* device_poll_thread = nullptr;
+static std::atomic<bool> terminate_device_poll_thread(false);
+
+static std::string get_device_address(libusb_device* device) {
+ return StringPrintf("usb:%d:%d", libusb_get_bus_number(device),
+ libusb_get_device_address(device));
+}
+
+static bool endpoint_is_output(uint8_t endpoint) {
+ return (endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT;
+}
+
+static bool should_perform_zero_transfer(uint8_t endpoint, size_t write_length, uint16_t zero_mask) {
+ return endpoint_is_output(endpoint) && write_length != 0 && zero_mask != 0 &&
+ (write_length & zero_mask) == 0;
+}
+
+static void poll_for_devices() {
+ libusb_device** list;
+ adb_thread_setname("device poll");
+ while (!terminate_device_poll_thread) {
+ const ssize_t device_count = libusb_get_device_list(nullptr, &list);
+
+ LOG(VERBOSE) << "found " << device_count << " attached devices";
+
+ for (ssize_t i = 0; i < device_count; ++i) {
+ libusb_device* device = list[i];
+ std::string device_address = get_device_address(device);
+ std::string device_serial;
+
+ // Figure out if we want to open the device.
+ libusb_device_descriptor device_desc;
+ int rc = libusb_get_device_descriptor(device, &device_desc);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to get device descriptor for device at " << device_address
+ << ": " << libusb_error_name(rc);
+ }
+
+ if (device_desc.bDeviceClass != LIBUSB_CLASS_PER_INTERFACE) {
+ // Assume that all Android devices have the device class set to per interface.
+ // TODO: Is this assumption valid?
+ LOG(VERBOSE) << "skipping device with incorrect class at " << device_address;
+ continue;
+ }
+
+ libusb_config_descriptor* config_raw;
+ rc = libusb_get_active_config_descriptor(device, &config_raw);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to get active config descriptor for device at "
+ << device_address << ": " << libusb_error_name(rc);
+ continue;
+ }
+ const unique_config_descriptor config(config_raw);
+
+ // Use size_t for interface_num so <iostream>s don't mangle it.
+ size_t interface_num;
+ uint16_t zero_mask;
+ uint8_t bulk_in = 0, bulk_out = 0;
+ bool found_adb = false;
+
+ for (interface_num = 0; interface_num < config->bNumInterfaces; ++interface_num) {
+ const libusb_interface& interface = config->interface[interface_num];
+ if (interface.num_altsetting != 1) {
+ // Assume that interfaces with alternate settings aren't adb interfaces.
+ // TODO: Is this assumption valid?
+ LOG(VERBOSE) << "skipping interface with incorrect num_altsetting at "
+ << device_address << " (interface " << interface_num << ")";
+ continue;
+ }
+
+ const libusb_interface_descriptor& interface_desc = interface.altsetting[0];
+ if (!is_adb_interface(interface_desc.bInterfaceClass,
+ interface_desc.bInterfaceSubClass,
+ interface_desc.bInterfaceProtocol)) {
+ LOG(VERBOSE) << "skipping non-adb interface at " << device_address
+ << " (interface " << interface_num << ")";
+ continue;
+ }
+
+ LOG(VERBOSE) << "found potential adb interface at " << device_address
+ << " (interface " << interface_num << ")";
+
+ bool found_in = false;
+ bool found_out = false;
+ for (size_t endpoint_num = 0; endpoint_num < interface_desc.bNumEndpoints;
+ ++endpoint_num) {
+ const auto& endpoint_desc = interface_desc.endpoint[endpoint_num];
+ const uint8_t endpoint_addr = endpoint_desc.bEndpointAddress;
+ const uint8_t endpoint_attr = endpoint_desc.bmAttributes;
+
+ const uint8_t transfer_type = endpoint_attr & LIBUSB_TRANSFER_TYPE_MASK;
+
+ if (transfer_type != LIBUSB_TRANSFER_TYPE_BULK) {
+ continue;
+ }
+
+ if (endpoint_is_output(endpoint_addr) && !found_out) {
+ found_out = true;
+ bulk_out = endpoint_addr;
+ zero_mask = endpoint_desc.wMaxPacketSize - 1;
+ } else if (!endpoint_is_output(endpoint_addr) && !found_in) {
+ found_in = true;
+ bulk_in = endpoint_addr;
+ }
+ }
+
+ if (found_in && found_out) {
+ found_adb = true;
+ break;
+ } else {
+ LOG(VERBOSE) << "rejecting potential adb interface at " << device_address
+ << "(interface " << interface_num << "): missing bulk endpoints "
+ << "(found_in = " << found_in << ", found_out = " << found_out
+ << ")";
+ }
+ }
+
+ if (!found_adb) {
+ LOG(VERBOSE) << "skipping device with no adb interfaces at " << device_address;
+ continue;
+ }
+
+ {
+ std::unique_lock<std::mutex> lock(usb_handles_mutex);
+ if (usb_handles.find(device_address) != usb_handles.end()) {
+ LOG(VERBOSE) << "device at " << device_address
+ << " has already been registered, skipping";
+ continue;
+ }
+ }
+
+ libusb_device_handle* handle_raw;
+ rc = libusb_open(list[i], &handle_raw);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to open usb device at " << device_address << ": "
+ << libusb_error_name(rc);
+ continue;
+ }
+
+ unique_device_handle handle(handle_raw);
+ LOG(DEBUG) << "successfully opened adb device at " << device_address << ", "
+ << StringPrintf("bulk_in = %#x, bulk_out = %#x", bulk_in, bulk_out);
+
+ device_serial.resize(255);
+ rc = libusb_get_string_descriptor_ascii(
+ handle_raw, device_desc.iSerialNumber,
+ reinterpret_cast<unsigned char*>(&device_serial[0]), device_serial.length());
+ if (rc == 0) {
+ LOG(WARNING) << "received empty serial from device at " << device_address;
+ continue;
+ } else if (rc < 0) {
+ LOG(WARNING) << "failed to get serial from device at " << device_address
+ << libusb_error_name(rc);
+ continue;
+ }
+ device_serial.resize(rc);
+
+ // Try to reset the device.
+ rc = libusb_reset_device(handle_raw);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to reset opened device '" << device_serial
+ << "': " << libusb_error_name(rc);
+ continue;
+ }
+
+ // WARNING: this isn't released via RAII.
+ rc = libusb_claim_interface(handle.get(), interface_num);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to claim adb interface for device '" << device_serial << "'"
+ << libusb_error_name(rc);
+ continue;
+ }
+
+ for (uint8_t endpoint : {bulk_in, bulk_out}) {
+ rc = libusb_clear_halt(handle.get(), endpoint);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to clear halt on device '" << device_serial
+ << "' endpoint 0x" << std::hex << endpoint << ": "
+ << libusb_error_name(rc);
+ libusb_release_interface(handle.get(), interface_num);
+ continue;
+ }
+ }
+
+ auto result =
+ std::make_unique<usb_handle>(device_address, device_serial, std::move(handle),
+ interface_num, bulk_in, bulk_out, zero_mask);
+ usb_handle* usb_handle_raw = result.get();
+
+ {
+ std::unique_lock<std::mutex> lock(usb_handles_mutex);
+ usb_handles[device_address] = std::move(result);
+ }
+
+ register_usb_transport(usb_handle_raw, device_serial.c_str(), device_address.c_str(), 1);
+
+ LOG(INFO) << "registered new usb device '" << device_serial << "'";
+ }
+ libusb_free_device_list(list, 1);
+
+ std::this_thread::sleep_for(500ms);
+ }
+}
+
+void usb_init() {
+ LOG(DEBUG) << "initializing libusb...";
+ int rc = libusb_init(nullptr);
+ if (rc != 0) {
+ LOG(FATAL) << "failed to initialize libusb: " << libusb_error_name(rc);
+ }
+
+ // Spawn a thread for libusb_handle_events.
+ std::thread([]() {
+ adb_thread_setname("libusb");
+ while (true) {
+ libusb_handle_events(nullptr);
+ }
+ }).detach();
+
+ // Spawn a thread to do device enumeration.
+ // TODO: Use libusb_hotplug_* instead?
+ device_poll_thread = new std::thread(poll_for_devices);
+ android::base::at_quick_exit([]() {
+ terminate_device_poll_thread = true;
+ std::unique_lock<std::mutex> lock(usb_handles_mutex);
+ for (auto& it : usb_handles) {
+ it.second->Close();
+ }
+ lock.unlock();
+ device_poll_thread->join();
+ });
+}
+
+// Dispatch a libusb transfer, unlock |device_lock|, and then wait for the result.
+static int perform_usb_transfer(usb_handle* h, transfer_info* info,
+ std::unique_lock<std::mutex> device_lock) {
+ libusb_transfer* transfer = info->transfer;
+
+ transfer->user_data = info;
+ transfer->callback = [](libusb_transfer* transfer) {
+ transfer_info* info = static_cast<transfer_info*>(transfer->user_data);
+
+ LOG(DEBUG) << info->name << " transfer callback entered";
+
+ // Make sure that the original submitter has made it to the condition_variable wait.
+ std::unique_lock<std::mutex> lock(info->mutex);
+
+ LOG(DEBUG) << info->name << " callback successfully acquired lock";
+
+ if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
+ LOG(WARNING) << info->name
+ << " transfer failed: " << libusb_error_name(transfer->status);
+ info->Notify();
+ return;
+ }
+
+ if (transfer->actual_length != transfer->length) {
+ LOG(DEBUG) << info->name << " transfer incomplete, resubmitting";
+ transfer->length -= transfer->actual_length;
+ transfer->buffer += transfer->actual_length;
+ int rc = libusb_submit_transfer(transfer);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to submit " << info->name
+ << " transfer: " << libusb_error_name(rc);
+ transfer->status = LIBUSB_TRANSFER_ERROR;
+ info->Notify();
+ }
+ return;
+ }
+
+ if (should_perform_zero_transfer(transfer->endpoint, transfer->length, info->zero_mask)) {
+ LOG(DEBUG) << "submitting zero-length write";
+ transfer->length = 0;
+ int rc = libusb_submit_transfer(transfer);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to submit zero-length write: " << libusb_error_name(rc);
+ transfer->status = LIBUSB_TRANSFER_ERROR;
+ info->Notify();
+ }
+ return;
+ }
+
+ LOG(VERBOSE) << info->name << "transfer fully complete";
+ info->Notify();
+ };
+
+ LOG(DEBUG) << "locking " << info->name << " transfer_info mutex";
+ std::unique_lock<std::mutex> lock(info->mutex);
+ info->transfer_complete = false;
+ LOG(DEBUG) << "submitting " << info->name << " transfer";
+ int rc = libusb_submit_transfer(transfer);
+ if (rc != 0) {
+ LOG(WARNING) << "failed to submit " << info->name << " transfer: " << libusb_error_name(rc);
+ errno = EIO;
+ return -1;
+ }
+
+ LOG(DEBUG) << info->name << " transfer successfully submitted";
+ device_lock.unlock();
+ info->cv.wait(lock, [info]() { return info->transfer_complete; });
+ if (transfer->status != 0) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int usb_write(usb_handle* h, const void* d, int len) {
+ LOG(DEBUG) << "usb_write of length " << len;
+
+ std::unique_lock<std::mutex> lock(h->device_handle_mutex);
+ if (!h->device_handle) {
+ errno = EIO;
+ return -1;
+ }
+
+ transfer_info* info = &h->write;
+ info->transfer->dev_handle = h->device_handle;
+ info->transfer->flags = 0;
+ info->transfer->endpoint = h->bulk_out;
+ info->transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
+ info->transfer->length = len;
+ info->transfer->buffer = reinterpret_cast<unsigned char*>(const_cast<void*>(d));
+ info->transfer->num_iso_packets = 0;
+
+ int rc = perform_usb_transfer(h, info, std::move(lock));
+ LOG(DEBUG) << "usb_write(" << len << ") = " << rc;
+ return rc;
+}
+
+int usb_read(usb_handle* h, void* d, int len) {
+ LOG(DEBUG) << "usb_read of length " << len;
+
+ std::unique_lock<std::mutex> lock(h->device_handle_mutex);
+ if (!h->device_handle) {
+ errno = EIO;
+ return -1;
+ }
+
+ transfer_info* info = &h->read;
+ info->transfer->dev_handle = h->device_handle;
+ info->transfer->flags = 0;
+ info->transfer->endpoint = h->bulk_in;
+ info->transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
+ info->transfer->length = len;
+ info->transfer->buffer = reinterpret_cast<unsigned char*>(d);
+ info->transfer->num_iso_packets = 0;
+
+ int rc = perform_usb_transfer(h, info, std::move(lock));
+ LOG(DEBUG) << "usb_read(" << len << ") = " << rc;
+ return rc;
+}
+
+int usb_close(usb_handle* h) {
+ std::unique_lock<std::mutex> lock(usb_handles_mutex);
+ auto it = usb_handles.find(h->device_address);
+ if (it == usb_handles.end()) {
+ LOG(FATAL) << "attempted to close unregistered usb_handle for '" << h->serial << "'";
+ }
+ usb_handles.erase(h->device_address);
+ return 0;
+}
+
+void usb_kick(usb_handle* h) {
+ h->Close();
+}
+} // namespace libusb
diff --git a/adb/client/usb_linux.cpp b/adb/client/usb_linux.cpp
index e7f1338..13b7674 100644
--- a/adb/client/usb_linux.cpp
+++ b/adb/client/usb_linux.cpp
@@ -46,6 +46,7 @@
#include "adb.h"
#include "transport.h"
+#include "usb.h"
using namespace std::chrono_literals;
using namespace std::literals;
@@ -53,7 +54,8 @@
/* usb scan debugging is waaaay too verbose */
#define DBGX(x...)
-struct usb_handle {
+namespace native {
+struct usb_handle : public ::usb_handle {
~usb_handle() {
if (fd != -1) unix_close(fd);
}
@@ -595,3 +597,4 @@
fatal_errno("cannot create device_poll thread");
}
}
+} // namespace native
diff --git a/adb/client/usb_osx.cpp b/adb/client/usb_osx.cpp
index e541f6e..d4fc7c0 100644
--- a/adb/client/usb_osx.cpp
+++ b/adb/client/usb_osx.cpp
@@ -44,6 +44,7 @@
using namespace std::chrono_literals;
+namespace native {
struct usb_handle
{
UInt8 bulkIn;
@@ -298,7 +299,8 @@
usb_handle* handle_p = handle.get();
VLOG(USB) << "Add usb device " << serial;
AddDevice(std::move(handle));
- register_usb_transport(handle_p, serial, devpath.c_str(), 1);
+ register_usb_transport(reinterpret_cast<::usb_handle*>(handle_p), serial, devpath.c_str(),
+ 1);
}
}
@@ -558,3 +560,4 @@
std::lock_guard<std::mutex> lock_guard(g_usb_handles_mutex);
usb_kick_locked(handle);
}
+} // namespace native
diff --git a/adb/transport_usb.cpp b/adb/transport_usb.cpp
index d054601..3d6cc99 100644
--- a/adb/transport_usb.cpp
+++ b/adb/transport_usb.cpp
@@ -93,9 +93,7 @@
t->usb = h;
}
-#if ADB_HOST
int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol)
{
return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
}
-#endif
diff --git a/adb/usb.h b/adb/usb.h
new file mode 100644
index 0000000..879bacc
--- /dev/null
+++ b/adb/usb.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#pragma once
+
+// USB host/client interface.
+
+#define ADB_USB_INTERFACE(handle_ref_type) \
+ void usb_init(); \
+ int usb_write(handle_ref_type h, const void* data, int len); \
+ int usb_read(handle_ref_type h, void* data, int len); \
+ int usb_close(handle_ref_type h); \
+ void usb_kick(handle_ref_type h)
+
+#if defined(_WIN32) || !ADB_HOST
+// Windows and the daemon have a single implementation.
+
+struct usb_handle;
+ADB_USB_INTERFACE(usb_handle*);
+
+#else // linux host || darwin
+// Linux and Darwin clients have native and libusb implementations.
+
+namespace libusb {
+ struct usb_handle;
+ ADB_USB_INTERFACE(libusb::usb_handle*);
+}
+
+namespace native {
+ struct usb_handle;
+ ADB_USB_INTERFACE(native::usb_handle*);
+}
+
+// Empty base that both implementations' opaque handles inherit from.
+struct usb_handle {
+};
+
+ADB_USB_INTERFACE(::usb_handle*);
+
+#endif // linux host || darwin
+
+
+// USB device detection.
+int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol);
diff --git a/fastboot/usb_osx.cpp b/fastboot/usb_osx.cpp
index ee5d575..032ae31 100644
--- a/fastboot/usb_osx.cpp
+++ b/fastboot/usb_osx.cpp
@@ -92,7 +92,6 @@
HRESULT result;
SInt32 score;
UInt8 interfaceNumEndpoints;
- UInt8 configuration;
// Placing the constant KIOUSBFindInterfaceDontCare into the following
// fields of the IOUSBFindInterfaceRequest structure will allow us to
@@ -102,13 +101,6 @@
request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
- // SetConfiguration will kill an existing UMS connection, so let's
- // not do this if not necessary.
- configuration = 0;
- (*dev)->GetConfiguration(dev, &configuration);
- if (configuration != 1)
- (*dev)->SetConfiguration(dev, 1);
-
// Get an iterator for the interfaces on the device
kr = (*dev)->CreateInterfaceIterator(dev, &request, &iterator);
diff --git a/liblog/include/log/log.h b/liblog/include/log/log.h
index 6a7450c..db22211 100644
--- a/liblog/include/log/log.h
+++ b/liblog/include/log/log.h
@@ -31,6 +31,9 @@
#include <log/log_id.h>
#include <log/log_main.h>
#include <log/log_radio.h>
+#include <log/log_read.h>
+#include <log/log_system.h>
+#include <log/log_time.h>
#include <log/uio.h> /* helper to define iovec for portability */
#ifdef __cplusplus
@@ -76,90 +79,6 @@
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
-/*
- * Simplified macro to send a verbose system log message using current LOG_TAG.
- */
-#ifndef SLOGV
-#define __SLOGV(...) \
- ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
-#if LOG_NDEBUG
-#define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0)
-#else
-#define SLOGV(...) __SLOGV(__VA_ARGS__)
-#endif
-#endif
-
-#ifndef SLOGV_IF
-#if LOG_NDEBUG
-#define SLOGV_IF(cond, ...) ((void)0)
-#else
-#define SLOGV_IF(cond, ...) \
- ( (__predict_false(cond)) \
- ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
- : (void)0 )
-#endif
-#endif
-
-/*
- * Simplified macro to send a debug system log message using current LOG_TAG.
- */
-#ifndef SLOGD
-#define SLOGD(...) \
- ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
-#endif
-
-#ifndef SLOGD_IF
-#define SLOGD_IF(cond, ...) \
- ( (__predict_false(cond)) \
- ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
- : (void)0 )
-#endif
-
-/*
- * Simplified macro to send an info system log message using current LOG_TAG.
- */
-#ifndef SLOGI
-#define SLOGI(...) \
- ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
-#endif
-
-#ifndef SLOGI_IF
-#define SLOGI_IF(cond, ...) \
- ( (__predict_false(cond)) \
- ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
- : (void)0 )
-#endif
-
-/*
- * Simplified macro to send a warning system log message using current LOG_TAG.
- */
-#ifndef SLOGW
-#define SLOGW(...) \
- ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
-#endif
-
-#ifndef SLOGW_IF
-#define SLOGW_IF(cond, ...) \
- ( (__predict_false(cond)) \
- ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
- : (void)0 )
-#endif
-
-/*
- * Simplified macro to send an error system log message using current LOG_TAG.
- */
-#ifndef SLOGE
-#define SLOGE(...) \
- ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
-#endif
-
-#ifndef SLOGE_IF
-#define SLOGE_IF(cond, ...) \
- ( (__predict_false(cond)) \
- ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
- : (void)0 )
-#endif
-
/* --------------------------------------------------------------------- */
/*
@@ -227,424 +146,6 @@
(void) __android_log_bswrite(_tag, _value);
#endif
-/* --------------------------------------------------------------------- */
-
-/*
- * Native log reading interface section. See logcat for sample code.
- *
- * The preferred API is an exec of logcat. Likely uses of this interface
- * are if native code suffers from exec or filtration being too costly,
- * access to raw information, or parsing is an issue.
- */
-
-/*
- * The userspace structure for version 1 of the logger_entry ABI.
- */
-#ifndef __struct_logger_entry_defined
-#define __struct_logger_entry_defined
-struct logger_entry {
- uint16_t len; /* length of the payload */
- uint16_t __pad; /* no matter what, we get 2 bytes of padding */
- int32_t pid; /* generating process's pid */
- int32_t tid; /* generating process's tid */
- int32_t sec; /* seconds since Epoch */
- int32_t nsec; /* nanoseconds */
-#ifndef __cplusplus
- char msg[0]; /* the entry's payload */
-#endif
-};
-#endif
-
-/*
- * The userspace structure for version 2 of the logger_entry ABI.
- */
-#ifndef __struct_logger_entry_v2_defined
-#define __struct_logger_entry_v2_defined
-struct logger_entry_v2 {
- uint16_t len; /* length of the payload */
- uint16_t hdr_size; /* sizeof(struct logger_entry_v2) */
- int32_t pid; /* generating process's pid */
- int32_t tid; /* generating process's tid */
- int32_t sec; /* seconds since Epoch */
- int32_t nsec; /* nanoseconds */
- uint32_t euid; /* effective UID of logger */
-#ifndef __cplusplus
- char msg[0]; /* the entry's payload */
-#endif
-} __attribute__((__packed__));
-#endif
-
-/*
- * The userspace structure for version 3 of the logger_entry ABI.
- */
-#ifndef __struct_logger_entry_v3_defined
-#define __struct_logger_entry_v3_defined
-struct logger_entry_v3 {
- uint16_t len; /* length of the payload */
- uint16_t hdr_size; /* sizeof(struct logger_entry_v3) */
- int32_t pid; /* generating process's pid */
- int32_t tid; /* generating process's tid */
- int32_t sec; /* seconds since Epoch */
- int32_t nsec; /* nanoseconds */
- uint32_t lid; /* log id of the payload */
-#ifndef __cplusplus
- char msg[0]; /* the entry's payload */
-#endif
-} __attribute__((__packed__));
-#endif
-
-/*
- * The userspace structure for version 4 of the logger_entry ABI.
- */
-#ifndef __struct_logger_entry_v4_defined
-#define __struct_logger_entry_v4_defined
-struct logger_entry_v4 {
- uint16_t len; /* length of the payload */
- uint16_t hdr_size; /* sizeof(struct logger_entry_v4) */
- int32_t pid; /* generating process's pid */
- uint32_t tid; /* generating process's tid */
- uint32_t sec; /* seconds since Epoch */
- uint32_t nsec; /* nanoseconds */
- uint32_t lid; /* log id of the payload, bottom 4 bits currently */
- uint32_t uid; /* generating process's uid */
-#ifndef __cplusplus
- char msg[0]; /* the entry's payload */
-#endif
-};
-#endif
-
-/* struct log_time is a wire-format variant of struct timespec */
-#define NS_PER_SEC 1000000000ULL
-
-#ifndef __struct_log_time_defined
-#define __struct_log_time_defined
-#ifdef __cplusplus
-
-/*
- * NB: we did NOT define a copy constructor. This will result in structure
- * no longer being compatible with pass-by-value which is desired
- * efficient behavior. Also, pass-by-reference breaks C/C++ ABI.
- */
-struct log_time {
-public:
- uint32_t tv_sec; /* good to Feb 5 2106 */
- uint32_t tv_nsec;
-
- static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
- static const uint32_t tv_nsec_max = 999999999UL;
-
- log_time(const timespec& T)
- {
- tv_sec = static_cast<uint32_t>(T.tv_sec);
- tv_nsec = static_cast<uint32_t>(T.tv_nsec);
- }
- log_time(uint32_t sec, uint32_t nsec)
- {
- tv_sec = sec;
- tv_nsec = nsec;
- }
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
-#define __struct_log_time_private_defined
- static const timespec EPOCH;
-#endif
- log_time()
- {
- }
-#ifdef __linux__
- log_time(clockid_t id)
- {
- timespec T;
- clock_gettime(id, &T);
- tv_sec = static_cast<uint32_t>(T.tv_sec);
- tv_nsec = static_cast<uint32_t>(T.tv_nsec);
- }
-#endif
- log_time(const char* T)
- {
- const uint8_t* c = reinterpret_cast<const uint8_t*>(T);
- tv_sec = c[0] |
- (static_cast<uint32_t>(c[1]) << 8) |
- (static_cast<uint32_t>(c[2]) << 16) |
- (static_cast<uint32_t>(c[3]) << 24);
- tv_nsec = c[4] |
- (static_cast<uint32_t>(c[5]) << 8) |
- (static_cast<uint32_t>(c[6]) << 16) |
- (static_cast<uint32_t>(c[7]) << 24);
- }
-
- /* timespec */
- bool operator== (const timespec& T) const
- {
- return (tv_sec == static_cast<uint32_t>(T.tv_sec))
- && (tv_nsec == static_cast<uint32_t>(T.tv_nsec));
- }
- bool operator!= (const timespec& T) const
- {
- return !(*this == T);
- }
- bool operator< (const timespec& T) const
- {
- return (tv_sec < static_cast<uint32_t>(T.tv_sec))
- || ((tv_sec == static_cast<uint32_t>(T.tv_sec))
- && (tv_nsec < static_cast<uint32_t>(T.tv_nsec)));
- }
- bool operator>= (const timespec& T) const
- {
- return !(*this < T);
- }
- bool operator> (const timespec& T) const
- {
- return (tv_sec > static_cast<uint32_t>(T.tv_sec))
- || ((tv_sec == static_cast<uint32_t>(T.tv_sec))
- && (tv_nsec > static_cast<uint32_t>(T.tv_nsec)));
- }
- bool operator<= (const timespec& T) const
- {
- return !(*this > T);
- }
-
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
- log_time operator-= (const timespec& T);
- log_time operator- (const timespec& T) const
- {
- log_time local(*this);
- return local -= T;
- }
- log_time operator+= (const timespec& T);
- log_time operator+ (const timespec& T) const
- {
- log_time local(*this);
- return local += T;
- }
-#endif
-
- /* log_time */
- bool operator== (const log_time& T) const
- {
- return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);
- }
- bool operator!= (const log_time& T) const
- {
- return !(*this == T);
- }
- bool operator< (const log_time& T) const
- {
- return (tv_sec < T.tv_sec)
- || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec));
- }
- bool operator>= (const log_time& T) const
- {
- return !(*this < T);
- }
- bool operator> (const log_time& T) const
- {
- return (tv_sec > T.tv_sec)
- || ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec));
- }
- bool operator<= (const log_time& T) const
- {
- return !(*this > T);
- }
-
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
- log_time operator-= (const log_time& T);
- log_time operator- (const log_time& T) const
- {
- log_time local(*this);
- return local -= T;
- }
- log_time operator+= (const log_time& T);
- log_time operator+ (const log_time& T) const
- {
- log_time local(*this);
- return local += T;
- }
-#endif
-
- uint64_t nsec() const
- {
- return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec;
- }
-
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
- static const char default_format[];
-
- /* Add %#q for the fraction of a second to the standard library functions */
- char* strptime(const char* s, const char* format = default_format);
-#endif
-} __attribute__((__packed__));
-
-#else
-
-typedef struct log_time {
- uint32_t tv_sec;
- uint32_t tv_nsec;
-} __attribute__((__packed__)) log_time;
-
-#endif
-#endif
-
-/*
- * The maximum size of the log entry payload that can be
- * written to the logger. An attempt to write more than
- * this amount will result in a truncated log entry.
- */
-#define LOGGER_ENTRY_MAX_PAYLOAD 4068
-
-/*
- * The maximum size of a log entry which can be read from the
- * kernel logger driver. An attempt to read less than this amount
- * may result in read() returning EINVAL.
- */
-#define LOGGER_ENTRY_MAX_LEN (5*1024)
-
-#ifndef __struct_log_msg_defined
-#define __struct_log_msg_defined
-struct log_msg {
- union {
- unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
- struct logger_entry_v4 entry;
- struct logger_entry_v4 entry_v4;
- struct logger_entry_v3 entry_v3;
- struct logger_entry_v2 entry_v2;
- struct logger_entry entry_v1;
- } __attribute__((aligned(4)));
-#ifdef __cplusplus
- /* Matching log_time operators */
- bool operator== (const log_msg& T) const
- {
- return (entry.sec == T.entry.sec) && (entry.nsec == T.entry.nsec);
- }
- bool operator!= (const log_msg& T) const
- {
- return !(*this == T);
- }
- bool operator< (const log_msg& T) const
- {
- return (entry.sec < T.entry.sec)
- || ((entry.sec == T.entry.sec)
- && (entry.nsec < T.entry.nsec));
- }
- bool operator>= (const log_msg& T) const
- {
- return !(*this < T);
- }
- bool operator> (const log_msg& T) const
- {
- return (entry.sec > T.entry.sec)
- || ((entry.sec == T.entry.sec)
- && (entry.nsec > T.entry.nsec));
- }
- bool operator<= (const log_msg& T) const
- {
- return !(*this > T);
- }
- uint64_t nsec() const
- {
- return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec;
- }
-
- /* packet methods */
- log_id_t id()
- {
- return static_cast<log_id_t>(entry.lid);
- }
- char* msg()
- {
- unsigned short hdr_size = entry.hdr_size;
- if (!hdr_size) {
- hdr_size = sizeof(entry_v1);
- }
- if ((hdr_size < sizeof(entry_v1)) || (hdr_size > sizeof(entry))) {
- return NULL;
- }
- return reinterpret_cast<char*>(buf) + hdr_size;
- }
- unsigned int len()
- {
- return (entry.hdr_size ?
- entry.hdr_size :
- static_cast<uint16_t>(sizeof(entry_v1))) +
- entry.len;
- }
-#endif
-};
-#endif
-
-#ifndef __ANDROID_USE_LIBLOG_READER_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 3
-#elif __ANDROID_API__ > 23 /* > Marshmallow */
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 3
-#elif __ANDROID_API__ > 22 /* > Lollipop */
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 2
-#elif __ANDROID_API__ > 19 /* > KitKat */
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_READER_INTERFACE
-
-struct logger;
-
-log_id_t android_logger_get_id(struct logger* logger);
-
-int android_logger_clear(struct logger* logger);
-long android_logger_get_log_size(struct logger* logger);
-int android_logger_set_log_size(struct logger* logger, unsigned long size);
-long android_logger_get_log_readable_size(struct logger* logger);
-int android_logger_get_log_version(struct logger* logger);
-
-struct logger_list;
-
-#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 1
-ssize_t android_logger_get_statistics(struct logger_list* logger_list,
- char* buf, size_t len);
-ssize_t android_logger_get_prune_list(struct logger_list* logger_list,
- char* buf, size_t len);
-int android_logger_set_prune_list(struct logger_list* logger_list,
- char* buf, size_t len);
-#endif
-
-#define ANDROID_LOG_RDONLY O_RDONLY
-#define ANDROID_LOG_WRONLY O_WRONLY
-#define ANDROID_LOG_RDWR O_RDWR
-#define ANDROID_LOG_ACCMODE O_ACCMODE
-#define ANDROID_LOG_NONBLOCK O_NONBLOCK
-#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 2
-#define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */
-#define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */
-#endif
-#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 1
-#define ANDROID_LOG_PSTORE 0x80000000
-#endif
-
-struct logger_list* android_logger_list_alloc(int mode,
- unsigned int tail,
- pid_t pid);
-struct logger_list* android_logger_list_alloc_time(int mode,
- log_time start,
- pid_t pid);
-void android_logger_list_free(struct logger_list* logger_list);
-/* In the purest sense, the following two are orthogonal interfaces */
-int android_logger_list_read(struct logger_list* logger_list,
- struct log_msg* log_msg);
-
-/* Multiple log_id_t opens */
-struct logger* android_logger_open(struct logger_list* logger_list,
- log_id_t id);
-#define android_logger_close android_logger_free
-/* Single log_id_t open */
-struct logger_list* android_logger_list_open(log_id_t id,
- int mode,
- unsigned int tail,
- pid_t pid);
-#define android_logger_list_close android_logger_list_free
-
-#endif /* __ANDROID_USE_LIBLOG_READER_INTERFACE */
-
#ifdef __linux__
#ifndef __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
diff --git a/liblog/include/log/log_read.h b/liblog/include/log/log_read.h
new file mode 100644
index 0000000..5b5eebc
--- /dev/null
+++ b/liblog/include/log/log_read.h
@@ -0,0 +1,291 @@
+/*
+ * Copyright (C) 2005-2017 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.
+ */
+
+#ifndef _LIBS_LOG_LOG_READ_H
+#define _LIBS_LOG_LOG_READ_H
+
+/* deal with possible sys/cdefs.h conflict with fcntl.h */
+#ifdef __unused
+#define __unused_defined __unused
+#undef __unused
+#endif
+
+#include <fcntl.h> /* Pick up O_* macros */
+
+/* restore definitions from above */
+#ifdef __unused_defined
+#define __unused __attribute__((__unused__))
+#endif
+
+#include <stdint.h>
+
+#include <log/log_id.h>
+#include <log/log_time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Native log reading interface section. See logcat for sample code.
+ *
+ * The preferred API is an exec of logcat. Likely uses of this interface
+ * are if native code suffers from exec or filtration being too costly,
+ * access to raw information, or parsing is an issue.
+ */
+
+/*
+ * The userspace structure for version 1 of the logger_entry ABI.
+ */
+#ifndef __struct_logger_entry_defined
+#define __struct_logger_entry_defined
+struct logger_entry {
+ uint16_t len; /* length of the payload */
+ uint16_t __pad; /* no matter what, we get 2 bytes of padding */
+ int32_t pid; /* generating process's pid */
+ int32_t tid; /* generating process's tid */
+ int32_t sec; /* seconds since Epoch */
+ int32_t nsec; /* nanoseconds */
+#ifndef __cplusplus
+ char msg[0]; /* the entry's payload */
+#endif
+};
+#endif
+
+/*
+ * The userspace structure for version 2 of the logger_entry ABI.
+ */
+#ifndef __struct_logger_entry_v2_defined
+#define __struct_logger_entry_v2_defined
+struct logger_entry_v2 {
+ uint16_t len; /* length of the payload */
+ uint16_t hdr_size; /* sizeof(struct logger_entry_v2) */
+ int32_t pid; /* generating process's pid */
+ int32_t tid; /* generating process's tid */
+ int32_t sec; /* seconds since Epoch */
+ int32_t nsec; /* nanoseconds */
+ uint32_t euid; /* effective UID of logger */
+#ifndef __cplusplus
+ char msg[0]; /* the entry's payload */
+#endif
+} __attribute__((__packed__));
+#endif
+
+/*
+ * The userspace structure for version 3 of the logger_entry ABI.
+ */
+#ifndef __struct_logger_entry_v3_defined
+#define __struct_logger_entry_v3_defined
+struct logger_entry_v3 {
+ uint16_t len; /* length of the payload */
+ uint16_t hdr_size; /* sizeof(struct logger_entry_v3) */
+ int32_t pid; /* generating process's pid */
+ int32_t tid; /* generating process's tid */
+ int32_t sec; /* seconds since Epoch */
+ int32_t nsec; /* nanoseconds */
+ uint32_t lid; /* log id of the payload */
+#ifndef __cplusplus
+ char msg[0]; /* the entry's payload */
+#endif
+} __attribute__((__packed__));
+#endif
+
+/*
+ * The userspace structure for version 4 of the logger_entry ABI.
+ */
+#ifndef __struct_logger_entry_v4_defined
+#define __struct_logger_entry_v4_defined
+struct logger_entry_v4 {
+ uint16_t len; /* length of the payload */
+ uint16_t hdr_size; /* sizeof(struct logger_entry_v4) */
+ int32_t pid; /* generating process's pid */
+ uint32_t tid; /* generating process's tid */
+ uint32_t sec; /* seconds since Epoch */
+ uint32_t nsec; /* nanoseconds */
+ uint32_t lid; /* log id of the payload, bottom 4 bits currently */
+ uint32_t uid; /* generating process's uid */
+#ifndef __cplusplus
+ char msg[0]; /* the entry's payload */
+#endif
+};
+#endif
+
+/*
+ * The maximum size of the log entry payload that can be
+ * written to the logger. An attempt to write more than
+ * this amount will result in a truncated log entry.
+ */
+#define LOGGER_ENTRY_MAX_PAYLOAD 4068
+
+/*
+ * The maximum size of a log entry which can be read.
+ * An attempt to read less than this amount may result
+ * in read() returning EINVAL.
+ */
+#define LOGGER_ENTRY_MAX_LEN (5*1024)
+
+#ifndef __struct_log_msg_defined
+#define __struct_log_msg_defined
+struct log_msg {
+ union {
+ unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
+ struct logger_entry_v4 entry;
+ struct logger_entry_v4 entry_v4;
+ struct logger_entry_v3 entry_v3;
+ struct logger_entry_v2 entry_v2;
+ struct logger_entry entry_v1;
+ } __attribute__((aligned(4)));
+#ifdef __cplusplus
+ /* Matching log_time operators */
+ bool operator== (const log_msg& T) const
+ {
+ return (entry.sec == T.entry.sec) && (entry.nsec == T.entry.nsec);
+ }
+ bool operator!= (const log_msg& T) const
+ {
+ return !(*this == T);
+ }
+ bool operator< (const log_msg& T) const
+ {
+ return (entry.sec < T.entry.sec)
+ || ((entry.sec == T.entry.sec)
+ && (entry.nsec < T.entry.nsec));
+ }
+ bool operator>= (const log_msg& T) const
+ {
+ return !(*this < T);
+ }
+ bool operator> (const log_msg& T) const
+ {
+ return (entry.sec > T.entry.sec)
+ || ((entry.sec == T.entry.sec)
+ && (entry.nsec > T.entry.nsec));
+ }
+ bool operator<= (const log_msg& T) const
+ {
+ return !(*this > T);
+ }
+ uint64_t nsec() const
+ {
+ return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec;
+ }
+
+ /* packet methods */
+ log_id_t id()
+ {
+ return static_cast<log_id_t>(entry.lid);
+ }
+ char* msg()
+ {
+ unsigned short hdr_size = entry.hdr_size;
+ if (!hdr_size) {
+ hdr_size = sizeof(entry_v1);
+ }
+ if ((hdr_size < sizeof(entry_v1)) || (hdr_size > sizeof(entry))) {
+ return NULL;
+ }
+ return reinterpret_cast<char*>(buf) + hdr_size;
+ }
+ unsigned int len()
+ {
+ return (entry.hdr_size ?
+ entry.hdr_size :
+ static_cast<uint16_t>(sizeof(entry_v1))) +
+ entry.len;
+ }
+#endif
+};
+#endif
+
+#ifndef __ANDROID_USE_LIBLOG_READER_INTERFACE
+#ifndef __ANDROID_API__
+#define __ANDROID_USE_LIBLOG_READER_INTERFACE 3
+#elif __ANDROID_API__ > 23 /* > Marshmallow */
+#define __ANDROID_USE_LIBLOG_READER_INTERFACE 3
+#elif __ANDROID_API__ > 22 /* > Lollipop */
+#define __ANDROID_USE_LIBLOG_READER_INTERFACE 2
+#elif __ANDROID_API__ > 19 /* > KitKat */
+#define __ANDROID_USE_LIBLOG_READER_INTERFACE 1
+#else
+#define __ANDROID_USE_LIBLOG_READER_INTERFACE 0
+#endif
+#endif
+
+#if __ANDROID_USE_LIBLOG_READER_INTERFACE
+
+struct logger;
+
+log_id_t android_logger_get_id(struct logger* logger);
+
+int android_logger_clear(struct logger* logger);
+long android_logger_get_log_size(struct logger* logger);
+int android_logger_set_log_size(struct logger* logger, unsigned long size);
+long android_logger_get_log_readable_size(struct logger* logger);
+int android_logger_get_log_version(struct logger* logger);
+
+struct logger_list;
+
+#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 1
+ssize_t android_logger_get_statistics(struct logger_list* logger_list,
+ char* buf, size_t len);
+ssize_t android_logger_get_prune_list(struct logger_list* logger_list,
+ char* buf, size_t len);
+int android_logger_set_prune_list(struct logger_list* logger_list,
+ char* buf, size_t len);
+#endif
+
+#define ANDROID_LOG_RDONLY O_RDONLY
+#define ANDROID_LOG_WRONLY O_WRONLY
+#define ANDROID_LOG_RDWR O_RDWR
+#define ANDROID_LOG_ACCMODE O_ACCMODE
+#define ANDROID_LOG_NONBLOCK O_NONBLOCK
+#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 2
+#define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */
+#define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */
+#endif
+#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 1
+#define ANDROID_LOG_PSTORE 0x80000000
+#endif
+
+struct logger_list* android_logger_list_alloc(int mode,
+ unsigned int tail,
+ pid_t pid);
+struct logger_list* android_logger_list_alloc_time(int mode,
+ log_time start,
+ pid_t pid);
+void android_logger_list_free(struct logger_list* logger_list);
+/* In the purest sense, the following two are orthogonal interfaces */
+int android_logger_list_read(struct logger_list* logger_list,
+ struct log_msg* log_msg);
+
+/* Multiple log_id_t opens */
+struct logger* android_logger_open(struct logger_list* logger_list,
+ log_id_t id);
+#define android_logger_close android_logger_free
+/* Single log_id_t open */
+struct logger_list* android_logger_list_open(log_id_t id,
+ int mode,
+ unsigned int tail,
+ pid_t pid);
+#define android_logger_list_close android_logger_list_free
+
+#endif /* __ANDROID_USE_LIBLOG_READER_INTERFACE */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _LIBS_LOG_LOG_H */
diff --git a/liblog/include/log/log_system.h b/liblog/include/log/log_system.h
new file mode 100644
index 0000000..8c1ec96
--- /dev/null
+++ b/liblog/include/log/log_system.h
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2005-2017 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.
+ */
+
+#ifndef _LIBS_LOG_LOG_SYSTEM_H
+#define _LIBS_LOG_LOG_SYSTEM_H
+
+#include <android/log.h>
+#include <log/log_id.h>
+
+/*
+ * Normally we strip the effects of ALOGV (VERBOSE messages),
+ * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
+ * release builds be defining NDEBUG. You can modify this (for
+ * example with "#define LOG_NDEBUG 0" at the top of your source
+ * file) to change that behavior.
+ */
+
+#ifndef LOG_NDEBUG
+#ifdef NDEBUG
+#define LOG_NDEBUG 1
+#else
+#define LOG_NDEBUG 0
+#endif
+#endif
+
+/*
+ * Simplified macro to send a verbose system log message using current LOG_TAG.
+ */
+#ifndef SLOGV
+#define __SLOGV(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
+#if LOG_NDEBUG
+#define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0)
+#else
+#define SLOGV(...) __SLOGV(__VA_ARGS__)
+#endif
+#endif
+
+#ifndef SLOGV_IF
+#if LOG_NDEBUG
+#define SLOGV_IF(cond, ...) ((void)0)
+#else
+#define SLOGV_IF(cond, ...) \
+ ( (__predict_false(cond)) \
+ ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
+ : (void)0 )
+#endif
+#endif
+
+/*
+ * Simplified macro to send a debug system log message using current LOG_TAG.
+ */
+#ifndef SLOGD
+#define SLOGD(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
+#endif
+
+#ifndef SLOGD_IF
+#define SLOGD_IF(cond, ...) \
+ ( (__predict_false(cond)) \
+ ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
+ : (void)0 )
+#endif
+
+/*
+ * Simplified macro to send an info system log message using current LOG_TAG.
+ */
+#ifndef SLOGI
+#define SLOGI(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
+#endif
+
+#ifndef SLOGI_IF
+#define SLOGI_IF(cond, ...) \
+ ( (__predict_false(cond)) \
+ ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
+ : (void)0 )
+#endif
+
+/*
+ * Simplified macro to send a warning system log message using current LOG_TAG.
+ */
+#ifndef SLOGW
+#define SLOGW(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
+#endif
+
+#ifndef SLOGW_IF
+#define SLOGW_IF(cond, ...) \
+ ( (__predict_false(cond)) \
+ ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
+ : (void)0 )
+#endif
+
+/*
+ * Simplified macro to send an error system log message using current LOG_TAG.
+ */
+#ifndef SLOGE
+#define SLOGE(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
+#endif
+
+#ifndef SLOGE_IF
+#define SLOGE_IF(cond, ...) \
+ ( (__predict_false(cond)) \
+ ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
+ : (void)0 )
+#endif
+
+#endif /* _LIBS_LOG_LOG_SYSTEM_H */
diff --git a/liblog/include/log/log_time.h b/liblog/include/log/log_time.h
new file mode 100644
index 0000000..900dc1b
--- /dev/null
+++ b/liblog/include/log/log_time.h
@@ -0,0 +1,196 @@
+/*
+ * Copyright (C) 2005-2017 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.
+ */
+
+#ifndef _LIBS_LOG_LOG_TIME_H
+#define _LIBS_LOG_LOG_TIME_H
+
+#include <stdint.h>
+#include <time.h>
+
+/* struct log_time is a wire-format variant of struct timespec */
+#define NS_PER_SEC 1000000000ULL
+
+#ifndef __struct_log_time_defined
+#define __struct_log_time_defined
+
+#ifdef __cplusplus
+
+/*
+ * NB: we did NOT define a copy constructor. This will result in structure
+ * no longer being compatible with pass-by-value which is desired
+ * efficient behavior. Also, pass-by-reference breaks C/C++ ABI.
+ */
+struct log_time {
+public:
+ uint32_t tv_sec; /* good to Feb 5 2106 */
+ uint32_t tv_nsec;
+
+ static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
+ static const uint32_t tv_nsec_max = 999999999UL;
+
+ log_time(const timespec& T)
+ {
+ tv_sec = static_cast<uint32_t>(T.tv_sec);
+ tv_nsec = static_cast<uint32_t>(T.tv_nsec);
+ }
+ log_time(uint32_t sec, uint32_t nsec)
+ {
+ tv_sec = sec;
+ tv_nsec = nsec;
+ }
+#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+#define __struct_log_time_private_defined
+ static const timespec EPOCH;
+#endif
+ log_time()
+ {
+ }
+#ifdef __linux__
+ log_time(clockid_t id)
+ {
+ timespec T;
+ clock_gettime(id, &T);
+ tv_sec = static_cast<uint32_t>(T.tv_sec);
+ tv_nsec = static_cast<uint32_t>(T.tv_nsec);
+ }
+#endif
+ log_time(const char* T)
+ {
+ const uint8_t* c = reinterpret_cast<const uint8_t*>(T);
+ tv_sec = c[0] |
+ (static_cast<uint32_t>(c[1]) << 8) |
+ (static_cast<uint32_t>(c[2]) << 16) |
+ (static_cast<uint32_t>(c[3]) << 24);
+ tv_nsec = c[4] |
+ (static_cast<uint32_t>(c[5]) << 8) |
+ (static_cast<uint32_t>(c[6]) << 16) |
+ (static_cast<uint32_t>(c[7]) << 24);
+ }
+
+ /* timespec */
+ bool operator== (const timespec& T) const
+ {
+ return (tv_sec == static_cast<uint32_t>(T.tv_sec))
+ && (tv_nsec == static_cast<uint32_t>(T.tv_nsec));
+ }
+ bool operator!= (const timespec& T) const
+ {
+ return !(*this == T);
+ }
+ bool operator< (const timespec& T) const
+ {
+ return (tv_sec < static_cast<uint32_t>(T.tv_sec))
+ || ((tv_sec == static_cast<uint32_t>(T.tv_sec))
+ && (tv_nsec < static_cast<uint32_t>(T.tv_nsec)));
+ }
+ bool operator>= (const timespec& T) const
+ {
+ return !(*this < T);
+ }
+ bool operator> (const timespec& T) const
+ {
+ return (tv_sec > static_cast<uint32_t>(T.tv_sec))
+ || ((tv_sec == static_cast<uint32_t>(T.tv_sec))
+ && (tv_nsec > static_cast<uint32_t>(T.tv_nsec)));
+ }
+ bool operator<= (const timespec& T) const
+ {
+ return !(*this > T);
+ }
+
+#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+ log_time operator-= (const timespec& T);
+ log_time operator- (const timespec& T) const
+ {
+ log_time local(*this);
+ return local -= T;
+ }
+ log_time operator+= (const timespec& T);
+ log_time operator+ (const timespec& T) const
+ {
+ log_time local(*this);
+ return local += T;
+ }
+#endif
+
+ /* log_time */
+ bool operator== (const log_time& T) const
+ {
+ return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);
+ }
+ bool operator!= (const log_time& T) const
+ {
+ return !(*this == T);
+ }
+ bool operator< (const log_time& T) const
+ {
+ return (tv_sec < T.tv_sec)
+ || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec));
+ }
+ bool operator>= (const log_time& T) const
+ {
+ return !(*this < T);
+ }
+ bool operator> (const log_time& T) const
+ {
+ return (tv_sec > T.tv_sec)
+ || ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec));
+ }
+ bool operator<= (const log_time& T) const
+ {
+ return !(*this > T);
+ }
+
+#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+ log_time operator-= (const log_time& T);
+ log_time operator- (const log_time& T) const
+ {
+ log_time local(*this);
+ return local -= T;
+ }
+ log_time operator+= (const log_time& T);
+ log_time operator+ (const log_time& T) const
+ {
+ log_time local(*this);
+ return local += T;
+ }
+#endif
+
+ uint64_t nsec() const
+ {
+ return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec;
+ }
+
+#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+ static const char default_format[];
+
+ /* Add %#q for the fraction of a second to the standard library functions */
+ char* strptime(const char* s, const char* format = default_format);
+#endif
+} __attribute__((__packed__));
+
+#else
+
+typedef struct log_time {
+ uint32_t tv_sec;
+ uint32_t tv_nsec;
+} __attribute__((__packed__)) log_time;
+
+#endif
+
+#endif
+
+#endif /* _LIBS_LOG_LOG_TIME_H */
diff --git a/liblog/tests/Android.mk b/liblog/tests/Android.mk
index 1a685df..097befc 100644
--- a/liblog/tests/Android.mk
+++ b/liblog/tests/Android.mk
@@ -57,7 +57,10 @@
test_src_files := \
liblog_test.cpp \
log_id_test.cpp \
- log_radio_test.cpp
+ log_radio_test.cpp \
+ log_read_test.cpp \
+ log_system_test.cpp \
+ log_time_test.cpp
# to prevent breaking the build if bionic not relatively visible to us
ifneq ($(wildcard $(LOCAL_PATH)/../../../../bionic/libc/bionic/libc_logging.cpp),)
diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp
index d03c6d1..8b9f0f0 100644
--- a/liblog/tests/liblog_test.cpp
+++ b/liblog/tests/liblog_test.cpp
@@ -1115,47 +1115,6 @@
#endif
}
-TEST(liblog, android_logger_get_) {
-#ifdef __ANDROID__
- // This test assumes the log buffers are filled with noise from
- // normal operations. It will fail if done immediately after a
- // logcat -c.
- struct logger_list * logger_list = android_logger_list_alloc(ANDROID_LOG_WRONLY, 0, 0);
-
- for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
- log_id_t id = static_cast<log_id_t>(i);
- const char *name = android_log_id_to_name(id);
- if (id != android_name_to_log_id(name)) {
- continue;
- }
- fprintf(stderr, "log buffer %s\r", name);
- struct logger * logger;
- EXPECT_TRUE(NULL != (logger = android_logger_open(logger_list, id)));
- EXPECT_EQ(id, android_logger_get_id(logger));
- ssize_t get_log_size = android_logger_get_log_size(logger);
- /* security buffer is allowed to be denied */
- if (strcmp("security", name)) {
- EXPECT_LT(0, get_log_size);
- /* crash buffer is allowed to be empty, that is actually healthy! */
- EXPECT_LE((strcmp("crash", name)) != 0,
- android_logger_get_log_readable_size(logger));
- } else {
- EXPECT_NE(0, get_log_size);
- if (get_log_size < 0) {
- EXPECT_GT(0, android_logger_get_log_readable_size(logger));
- } else {
- EXPECT_LE(0, android_logger_get_log_readable_size(logger));
- }
- }
- EXPECT_LT(0, android_logger_get_log_version(logger));
- }
-
- android_logger_list_close(logger_list);
-#else
- GTEST_LOG_(INFO) << "This test does nothing.\n";
-#endif
-}
-
#ifdef __ANDROID__
static bool checkPriForTag(AndroidLogFormat *p_format, const char *tag, android_LogPriority pri) {
return android_log_shouldPrintLine(p_format, tag, pri)
diff --git a/liblog/tests/log_read_test.cpp b/liblog/tests/log_read_test.cpp
new file mode 100644
index 0000000..2e02407
--- /dev/null
+++ b/liblog/tests/log_read_test.cpp
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2013-2017 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 <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <string>
+
+#include <android/log.h> // minimal logging API
+#include <android-base/stringprintf.h>
+#include <gtest/gtest.h>
+// Test the APIs in this standalone include file
+#include <log/log_read.h>
+// Do not use anything in log/log_time.h despite side effects of the above.
+
+TEST(liblog, __android_log_write__android_logger_list_read) {
+#ifdef __ANDROID__
+ pid_t pid = getpid();
+
+ struct logger_list *logger_list;
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ std::string buf = android::base::StringPrintf("pid=%u ts=%ld.%09ld",
+ pid, ts.tv_sec, ts.tv_nsec);
+ static const char tag[] = "liblog.__android_log_write__android_logger_list_read";
+ static const char prio = ANDROID_LOG_DEBUG;
+ ASSERT_LT(0, __android_log_write(prio, tag, buf.c_str()));
+ usleep(1000000);
+
+ buf = std::string(&prio, sizeof(prio)) +
+ tag +
+ std::string("", 1) +
+ buf +
+ std::string("", 1);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) break;
+
+ EXPECT_EQ(log_msg.entry.pid, pid);
+ // There may be a future where we leak "liblog" tagged LOG_ID_EVENT
+ // binary messages through so that logger losses can be correlated?
+ EXPECT_EQ(log_msg.id(), LOG_ID_MAIN);
+
+ if (log_msg.entry.len != buf.length()) continue;
+
+ if (buf != std::string(log_msg.msg(), log_msg.entry.len)) continue;
+
+ ++count;
+ }
+ android_logger_list_close(logger_list);
+
+ EXPECT_EQ(1, count);
+#else
+ GTEST_LOG_(INFO) << "This test does nothing.\n";
+#endif
+}
+
+TEST(liblog, android_logger_get_) {
+#ifdef __ANDROID__
+ // This test assumes the log buffers are filled with noise from
+ // normal operations. It will fail if done immediately after a
+ // logcat -c.
+ struct logger_list * logger_list = android_logger_list_alloc(ANDROID_LOG_WRONLY, 0, 0);
+
+ for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
+ log_id_t id = static_cast<log_id_t>(i);
+ const char *name = android_log_id_to_name(id);
+ if (id != android_name_to_log_id(name)) {
+ continue;
+ }
+ fprintf(stderr, "log buffer %s\r", name);
+ struct logger * logger;
+ EXPECT_TRUE(NULL != (logger = android_logger_open(logger_list, id)));
+ EXPECT_EQ(id, android_logger_get_id(logger));
+ ssize_t get_log_size = android_logger_get_log_size(logger);
+ /* security buffer is allowed to be denied */
+ if (strcmp("security", name)) {
+ EXPECT_LT(0, get_log_size);
+ /* crash buffer is allowed to be empty, that is actually healthy! */
+ EXPECT_LE((strcmp("crash", name)) != 0,
+ android_logger_get_log_readable_size(logger));
+ } else {
+ EXPECT_NE(0, get_log_size);
+ if (get_log_size < 0) {
+ EXPECT_GT(0, android_logger_get_log_readable_size(logger));
+ } else {
+ EXPECT_LE(0, android_logger_get_log_readable_size(logger));
+ }
+ }
+ EXPECT_LT(0, android_logger_get_log_version(logger));
+ }
+
+ android_logger_list_close(logger_list);
+#else
+ GTEST_LOG_(INFO) << "This test does nothing.\n";
+#endif
+}
+
diff --git a/liblog/tests/log_system_test.cpp b/liblog/tests/log_system_test.cpp
new file mode 100644
index 0000000..b62832e
--- /dev/null
+++ b/liblog/tests/log_system_test.cpp
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2017 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 <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <string>
+
+#include <android-base/file.h>
+#include <android-base/stringprintf.h>
+#include <gtest/gtest.h>
+// Test the APIs in this standalone include file
+#include <log/log_system.h>
+
+TEST(liblog, SLOG) {
+#ifdef __ANDROID__
+ static const char content[] = "log_system.h";
+ static const char content_false[] = "log_system.h false";
+
+ // ratelimit content to 10/s to keep away from spam filters
+ // do not send identical content together to keep away from spam filters
+
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGV"
+ SLOGV(content);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGD"
+ SLOGD(content);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGI"
+ SLOGI(content);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGW"
+ SLOGW(content);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGE"
+ SLOGE(content);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGV"
+ SLOGV_IF(true, content);
+ usleep(100000);
+ SLOGV_IF(false, content_false);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGD"
+ SLOGD_IF(true, content);
+ usleep(100000);
+ SLOGD_IF(false, content_false);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGI"
+ SLOGI_IF(true, content);
+ usleep(100000);
+ SLOGI_IF(false, content_false);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGW"
+ SLOGW_IF(true, content);
+ usleep(100000);
+ SLOGW_IF(false, content_false);
+ usleep(100000);
+#undef LOG_TAG
+#define LOG_TAG "TEST__SLOGE"
+ SLOGE_IF(true, content);
+ usleep(100000);
+ SLOGE_IF(false, content_false);
+
+ // give time for content to long-path through logger
+ sleep(1);
+
+ std::string buf = android::base::StringPrintf(
+ "logcat -b system --pid=%u -d -s"
+ " TEST__SLOGV TEST__SLOGD TEST__SLOGI TEST__SLOGW TEST__SLOGE",
+ (unsigned)getpid());
+ FILE* fp = popen(buf.c_str(), "r");
+ int count = 0;
+ int count_false = 0;
+ if (fp) {
+ if (!android::base::ReadFdToString(fileno(fp), &buf)) buf = "";
+ pclose(fp);
+ for (size_t pos = 0; (pos = buf.find(content, pos)) != std::string::npos; ++pos) {
+ ++count;
+ }
+ for (size_t pos = 0; (pos = buf.find(content_false, pos)) != std::string::npos; ++pos) {
+ ++count_false;
+ }
+ }
+ EXPECT_EQ(0, count_false);
+#if LOG_NDEBUG
+ ASSERT_EQ(8, count);
+#else
+ ASSERT_EQ(10, count);
+#endif
+
+#else
+ GTEST_LOG_(INFO) << "This test does nothing.\n";
+#endif
+}
diff --git a/liblog/tests/log_time_test.cpp b/liblog/tests/log_time_test.cpp
new file mode 100644
index 0000000..f2601b6
--- /dev/null
+++ b/liblog/tests/log_time_test.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2017 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 <time.h>
+
+#include <gtest/gtest.h>
+// Test the APIs in this standalone include file
+#include <log/log_time.h>
+
+TEST(liblog, log_time) {
+#ifdef __ANDROID__
+
+#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+ log_time(CLOCK_MONOTONIC);
+
+ EXPECT_EQ(log_time, log_time::EPOCH);
+#endif
+
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ log_time tl(ts);
+
+ EXPECT_EQ(tl, ts);
+ EXPECT_GE(tl, ts);
+ EXPECT_LE(tl, ts);
+
+#else
+ GTEST_LOG_(INFO) << "This test does nothing.\n";
+#endif
+}