2017-02-10 22:09:07 +01:00
|
|
|
/*
|
2017-02-10 22:09:07 +01:00
|
|
|
* Copyright (C) 2006-2017 The Android Open Source Project
|
2017-02-10 22:09:07 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <ctype.h>
|
2015-05-27 16:39:56 +02:00
|
|
|
#include <dirent.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <errno.h>
|
2019-12-06 19:25:37 +01:00
|
|
|
#include <error.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <fcntl.h>
|
2018-06-16 00:16:20 +02:00
|
|
|
#include <getopt.h>
|
2014-10-17 07:19:55 +02:00
|
|
|
#include <math.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <stdarg.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-04-10 14:51:39 +02:00
|
|
|
#include <sys/cdefs.h>
|
2019-07-05 03:09:38 +02:00
|
|
|
#include <sys/ioctl.h>
|
2015-06-09 08:36:34 +02:00
|
|
|
#include <sys/resource.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/stat.h>
|
2015-05-27 16:39:56 +02:00
|
|
|
#include <sys/types.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-27 16:39:56 +02:00
|
|
|
#include <memory>
|
2019-08-08 17:53:59 +02:00
|
|
|
#include <regex>
|
2015-05-27 16:39:56 +02:00
|
|
|
#include <string>
|
2018-09-05 20:05:57 +02:00
|
|
|
#include <utility>
|
2017-02-10 22:09:07 +01:00
|
|
|
#include <vector>
|
2015-05-27 16:39:56 +02:00
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/file.h>
|
2019-10-25 02:35:26 +02:00
|
|
|
#include <android-base/macros.h>
|
2019-10-29 15:05:24 +01:00
|
|
|
#include <android-base/parseint.h>
|
2017-02-21 23:31:19 +01:00
|
|
|
#include <android-base/properties.h>
|
2016-08-03 23:20:41 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/strings.h>
|
2019-10-25 02:35:26 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2019-10-29 15:05:24 +01:00
|
|
|
#include <android/log.h>
|
2015-06-02 16:57:16 +02:00
|
|
|
#include <log/event_tag_map.h>
|
2019-10-29 15:05:24 +01:00
|
|
|
#include <log/log_id.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
#include <log/logprint.h>
|
2016-09-30 22:30:33 +02:00
|
|
|
#include <private/android_logger.h>
|
2018-12-21 21:30:16 +01:00
|
|
|
#include <processgroup/sched_policy.h>
|
2016-02-17 20:58:01 +01:00
|
|
|
#include <system/thread_defs.h>
|
2013-12-13 20:10:11 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#define DEFAULT_MAX_ROTATED_LOGS 4
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
using android::base::Join;
|
|
|
|
using android::base::ParseByteCount;
|
|
|
|
using android::base::ParseUint;
|
|
|
|
using android::base::Split;
|
2019-10-25 02:35:26 +02:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
|
|
|
class Logcat {
|
|
|
|
public:
|
|
|
|
int Run(int argc, char** argv);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void RotateLogs();
|
2019-10-29 15:05:24 +01:00
|
|
|
void ProcessBuffer(struct log_msg* buf);
|
|
|
|
void PrintDividers(log_id_t log_id, bool print_dividers);
|
2019-10-25 02:35:26 +02:00
|
|
|
void SetupOutputAndSchedulingPolicy(bool blocking);
|
|
|
|
int SetLogFormat(const char* format_string);
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
// Used for all options
|
2019-10-25 02:35:26 +02:00
|
|
|
android::base::unique_fd output_fd_{dup(STDOUT_FILENO)};
|
|
|
|
std::unique_ptr<AndroidLogFormat, decltype(&android_log_format_free)> logformat_{
|
|
|
|
android_log_format_new(), &android_log_format_free};
|
2019-10-29 15:05:24 +01:00
|
|
|
|
|
|
|
// For logging to a file and log rotation
|
2019-10-25 02:35:26 +02:00
|
|
|
const char* output_file_name_ = nullptr;
|
2019-10-29 15:05:24 +01:00
|
|
|
size_t log_rotate_size_kb_ = 0; // 0 means "no log rotation"
|
|
|
|
size_t max_rotated_logs_ = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded"
|
2019-10-25 02:35:26 +02:00
|
|
|
size_t out_byte_count_ = 0;
|
2019-10-29 15:05:24 +01:00
|
|
|
|
|
|
|
// For binary log buffers
|
2019-10-25 02:35:26 +02:00
|
|
|
int print_binary_ = 0;
|
|
|
|
std::unique_ptr<EventTagMap, decltype(&android_closeEventTagMap)> event_tag_map_{
|
|
|
|
nullptr, &android_closeEventTagMap};
|
2019-10-29 15:05:24 +01:00
|
|
|
bool has_opened_event_tag_map_ = false;
|
2017-03-06 23:56:04 +01:00
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
// For the related --regex, --max-count, --print
|
|
|
|
std::unique_ptr<std::regex> regex_;
|
|
|
|
size_t max_count_ = 0; // 0 means "infinite"
|
|
|
|
size_t print_count_ = 0;
|
2019-10-25 02:35:26 +02:00
|
|
|
bool print_it_anyways_ = false;
|
2019-10-29 15:05:24 +01:00
|
|
|
|
|
|
|
// For PrintDividers()
|
|
|
|
log_id_t last_printed_id_ = LOG_ID_MAX;
|
|
|
|
bool printed_start_[LOG_ID_MAX] = {};
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
bool debug_ = false;
|
2017-02-10 22:09:07 +01:00
|
|
|
};
|
|
|
|
|
2019-07-05 03:09:38 +02:00
|
|
|
#ifndef F2FS_IOC_SET_PIN_FILE
|
|
|
|
#define F2FS_IOCTL_MAGIC 0xf5
|
|
|
|
#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int openLogFile(const char* pathname, size_t sizeKB) {
|
2020-01-18 03:01:03 +01:00
|
|
|
int fd = open(pathname, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP);
|
2019-07-05 03:09:38 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no need to check errors
|
|
|
|
__u32 set = 1;
|
|
|
|
ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
|
|
|
|
fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, (sizeKB << 10));
|
|
|
|
return fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
void Logcat::RotateLogs() {
|
2009-03-04 04:32:55 +01:00
|
|
|
// Can't rotate logs if we're not outputting to a file
|
2019-10-25 02:35:26 +02:00
|
|
|
if (!output_file_name_) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
output_fd_.reset();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// Compute the maximum number of digits needed to count up to
|
2017-02-10 22:09:07 +01:00
|
|
|
// maxRotatedLogs in decimal. eg:
|
|
|
|
// maxRotatedLogs == 30
|
2017-02-10 22:09:07 +01:00
|
|
|
// -> log10(30) == 1.477
|
|
|
|
// -> maxRotationCountDigits == 2
|
2019-10-25 02:35:26 +02:00
|
|
|
int max_rotation_count_digits =
|
|
|
|
max_rotated_logs_ > 0 ? (int)(floor(log10(max_rotated_logs_) + 1)) : 0;
|
2014-10-17 07:19:55 +02:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
for (int i = max_rotated_logs_; i > 0; i--) {
|
|
|
|
std::string file1 =
|
|
|
|
StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-08-03 23:20:41 +02:00
|
|
|
std::string file0;
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!(i - 1)) {
|
2019-10-25 02:35:26 +02:00
|
|
|
file0 = output_file_name_;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2019-10-25 02:35:26 +02:00
|
|
|
file0 = StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i - 1);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!file0.length() || !file1.length()) {
|
2015-04-10 14:51:39 +02:00
|
|
|
perror("while rotating log files");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
int err = rename(file0.c_str(), file1.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (err < 0 && errno != ENOENT) {
|
|
|
|
perror("while rotating log files");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
output_fd_.reset(openLogFile(output_file_name_, log_rotate_size_kb_));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (!output_fd_.ok()) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, errno, "Couldn't open output file");
|
2017-02-17 22:15:51 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
out_byte_count_ = 0;
|
2016-03-18 00:18:55 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
void Logcat::ProcessBuffer(struct log_msg* buf) {
|
2010-03-18 00:10:26 +01:00
|
|
|
int bytesWritten = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
int err;
|
|
|
|
AndroidLogEntry entry;
|
|
|
|
char binaryMsgBuf[1024];
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
bool is_binary =
|
|
|
|
buf->id() == LOG_ID_EVENTS || buf->id() == LOG_ID_STATS || buf->id() == LOG_ID_SECURITY;
|
|
|
|
|
|
|
|
if (is_binary) {
|
2019-10-25 02:35:26 +02:00
|
|
|
if (!event_tag_map_ && !has_opened_event_tag_map_) {
|
|
|
|
event_tag_map_.reset(android_openEventTagMap(nullptr));
|
|
|
|
has_opened_event_tag_map_ = true;
|
2015-02-26 23:33:35 +01:00
|
|
|
}
|
2019-10-25 02:35:26 +02:00
|
|
|
err = android_log_processBinaryLogBuffer(&buf->entry, &entry, event_tag_map_.get(),
|
2019-10-16 01:53:11 +02:00
|
|
|
binaryMsgBuf, sizeof(binaryMsgBuf));
|
2017-02-10 22:09:07 +01:00
|
|
|
// printf(">>> pri=%d len=%d msg='%s'\n",
|
2009-03-04 04:32:55 +01:00
|
|
|
// entry.priority, entry.messageLen, entry.message);
|
|
|
|
} else {
|
2019-10-16 01:53:11 +02:00
|
|
|
err = android_log_processLogBuffer(&buf->entry, &entry);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2019-10-25 02:35:26 +02:00
|
|
|
if (err < 0 && !debug_) return;
|
2010-03-01 18:11:54 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (android_log_shouldPrintLine(logformat_.get(), std::string(entry.tag, entry.tagLen).c_str(),
|
|
|
|
entry.priority)) {
|
|
|
|
bool match = !regex_ ||
|
|
|
|
std::regex_search(entry.message, entry.message + entry.messageLen, *regex_);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
print_count_ += match;
|
|
|
|
if (match || print_it_anyways_) {
|
|
|
|
bytesWritten = android_log_printLogLine(logformat_.get(), output_fd_.get(), &entry);
|
2016-03-17 22:04:52 +01:00
|
|
|
|
2016-03-30 18:38:31 +02:00
|
|
|
if (bytesWritten < 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Output error.");
|
2016-03-30 18:38:31 +02:00
|
|
|
}
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
out_byte_count_ += bytesWritten;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (log_rotate_size_kb_ > 0 && (out_byte_count_ / 1024) >= log_rotate_size_kb_) {
|
|
|
|
RotateLogs();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
void Logcat::PrintDividers(log_id_t log_id, bool print_dividers) {
|
|
|
|
if (log_id == last_printed_id_ || print_binary_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!printed_start_[log_id] || print_dividers) {
|
|
|
|
if (dprintf(output_fd_.get(), "--------- %s %s\n",
|
|
|
|
printed_start_[log_id] ? "switch to" : "beginning of",
|
|
|
|
android_log_id_to_name(log_id)) < 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, errno, "Output error");
|
2010-03-12 05:32:17 +01:00
|
|
|
}
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
last_printed_id_ = log_id;
|
|
|
|
printed_start_[log_id] = true;
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
void Logcat::SetupOutputAndSchedulingPolicy(bool blocking) {
|
|
|
|
if (!output_file_name_) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-08-04 16:53:52 +02:00
|
|
|
if (blocking) {
|
|
|
|
// Lower priority and set to batch scheduling if we are saving
|
|
|
|
// the logs into files and taking continuous content.
|
2019-10-25 02:35:26 +02:00
|
|
|
if (set_sched_policy(0, SP_BACKGROUND) < 0) {
|
|
|
|
fprintf(stderr, "failed to set background scheduling policy\n");
|
2015-06-02 16:57:16 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
struct sched_param param = {};
|
2017-02-10 22:09:07 +01:00
|
|
|
if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) {
|
2015-06-02 16:57:16 +02:00
|
|
|
fprintf(stderr, "failed to set to batch scheduler\n");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
|
|
|
|
fprintf(stderr, "failed set to priority\n");
|
2015-06-09 08:36:34 +02:00
|
|
|
}
|
2016-08-04 16:53:52 +02:00
|
|
|
}
|
2015-06-09 08:36:34 +02:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
output_fd_.reset(openLogFile(output_file_name_, log_rotate_size_kb_));
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (!output_fd_.ok()) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, errno, "Couldn't open output file");
|
2016-08-04 16:53:52 +02:00
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2016-08-04 16:53:52 +02:00
|
|
|
struct stat statbuf;
|
2019-10-25 02:35:26 +02:00
|
|
|
if (fstat(output_fd_.get(), &statbuf) == -1) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, errno, "Couldn't get output file stat");
|
2016-08-04 16:53:52 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid output file stat.");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-08-04 16:53:52 +02:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
out_byte_count_ = statbuf.st_size;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format off
|
2019-10-25 02:35:26 +02:00
|
|
|
static void show_help() {
|
|
|
|
const char* cmd = getprogname();
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
fprintf(stderr, "Usage: %s [options] [filterspecs]\n", cmd);
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2019-11-21 19:31:06 +01:00
|
|
|
fprintf(stderr, R"init(
|
|
|
|
General options:
|
|
|
|
-b, --buffer=<buffer> Request alternate ring buffer(s):
|
|
|
|
main system radio events crash default all
|
|
|
|
Additionally, 'kernel' for userdebug and eng builds, and
|
|
|
|
'security' for Device Owner installations.
|
|
|
|
Multiple -b parameters or comma separated list of buffers are
|
|
|
|
allowed. Buffers are interleaved.
|
|
|
|
Default -b main,system,crash,kernel.
|
|
|
|
-L, --last Dump logs from prior to last reboot from pstore.
|
|
|
|
-c, --clear Clear (flush) the entire log and exit.
|
|
|
|
if -f is specified, clear the specified file and its related rotated
|
|
|
|
log files instead.
|
|
|
|
if -L is specified, clear pstore log instead.
|
|
|
|
-d Dump the log and then exit (don't block).
|
|
|
|
--pid=<pid> Only print logs from the given pid.
|
|
|
|
--wrap Sleep for 2 hours or when buffer about to wrap whichever
|
|
|
|
comes first. Improves efficiency of polling by providing
|
|
|
|
an about-to-wrap wakeup.
|
|
|
|
|
|
|
|
Formatting:
|
|
|
|
-v, --format=<format> Sets log print format verb and adverbs, where <format> is one of:
|
|
|
|
brief help long process raw tag thread threadtime time
|
|
|
|
Modifying adverbs can be added:
|
|
|
|
color descriptive epoch monotonic printable uid usec UTC year zone
|
|
|
|
Multiple -v parameters or comma separated list of format and format
|
|
|
|
modifiers are allowed.
|
|
|
|
-D, --dividers Print dividers between each log buffer.
|
|
|
|
-B, --binary Output the log in binary.
|
|
|
|
|
|
|
|
Outfile files:
|
|
|
|
-f, --file=<file> Log to file instead of stdout.
|
|
|
|
-r, --rotate-kbytes=<n> Rotate log every <n> kbytes. Requires -f option.
|
|
|
|
-n, --rotate-count=<count> Sets max number of rotated logs to <count>, default 4.
|
|
|
|
--id=<id> If the signature <id> for logging to file changes, then clear the
|
|
|
|
associated files and continue.
|
|
|
|
|
|
|
|
Logd control:
|
|
|
|
These options send a control message to the logd daemon on device, print its return message if
|
|
|
|
applicable, then exit. They are incompatible with -L, as these attributes do not apply to pstore.
|
|
|
|
-g, --buffer-size Get the size of the ring buffers within logd.
|
|
|
|
-G, --buffer-size=<size> Set size of a ring buffer in logd. May suffix with K or M.
|
|
|
|
This can individually control each buffer's size with -b.
|
|
|
|
-S, --statistics Output statistics.
|
|
|
|
--pid can be used to provide pid specific stats.
|
|
|
|
-p, --prune Print prune white and ~black list. Service is specified as UID,
|
|
|
|
UID/PID or /PID. Weighed for quicker pruning if prefix with ~,
|
|
|
|
otherwise weighed for longevity if unadorned. All other pruning
|
|
|
|
activity is oldest first. Special case ~! represents an automatic
|
|
|
|
quicker pruning for the noisiest UID as determined by the current
|
|
|
|
statistics.
|
|
|
|
-P, --prune='<list> ...' Set prune white and ~black list, using same format as listed above.
|
|
|
|
Must be quoted.
|
|
|
|
|
|
|
|
Filtering:
|
|
|
|
-s Set default filter to silent. Equivalent to filterspec '*:S'
|
|
|
|
-e, --regex=<expr> Only print lines where the log message matches <expr> where <expr> is
|
|
|
|
an ECMAScript regular expression.
|
|
|
|
-m, --max-count=<count> Quit after printing <count> lines. This is meant to be paired with
|
|
|
|
--regex, but will work on its own.
|
|
|
|
--print This option is only applicable when --regex is set and only useful if
|
|
|
|
--max-count is also provided.
|
|
|
|
With --print, logcat will print all messages even if they do not
|
|
|
|
match the regex. Logcat will quit after printing the max-count number
|
|
|
|
of lines that match the regex.
|
|
|
|
-t <count> Print only the most recent <count> lines (implies -d).
|
|
|
|
-t '<time>' Print the lines since specified time (implies -d).
|
|
|
|
-T <count> Print only the most recent <count> lines (does not imply -d).
|
|
|
|
-T '<time>' Print the lines since specified time (not imply -d).
|
|
|
|
count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'
|
|
|
|
'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format.
|
|
|
|
)init");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
fprintf(stderr, "\nfilterspecs are a series of \n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" <tag>[:priority]\n\n"
|
|
|
|
"where <tag> is a log component tag (or * for all) and priority is:\n"
|
2015-03-09 17:32:56 +01:00
|
|
|
" V Verbose (default for <tag>)\n"
|
|
|
|
" D Debug (default for '*')\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
" I Info\n"
|
|
|
|
" W Warn\n"
|
|
|
|
" E Error\n"
|
|
|
|
" F Fatal\n"
|
2015-03-09 17:32:56 +01:00
|
|
|
" S Silent (suppress all output)\n"
|
|
|
|
"\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
|
|
|
|
"If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
|
|
|
|
"eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
|
|
|
|
"\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
|
|
|
|
"\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
|
2014-09-16 18:15:15 +02:00
|
|
|
"or defaults to \"threadtime\"\n\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
static void show_format_help() {
|
|
|
|
fprintf(stderr,
|
2016-10-18 20:30:11 +02:00
|
|
|
"-v <format>, --format=<format> options:\n"
|
|
|
|
" Sets log print format verb and adverbs, where <format> is:\n"
|
|
|
|
" brief long process raw tag thread threadtime time\n"
|
|
|
|
" and individually flagged modifying adverbs can be added:\n"
|
|
|
|
" color descriptive epoch monotonic printable uid usec UTC year zone\n"
|
|
|
|
"\nSingle format verbs:\n"
|
|
|
|
" brief — Display priority/tag and PID of the process issuing the message.\n"
|
|
|
|
" long — Display all metadata fields, separate messages with blank lines.\n"
|
|
|
|
" process — Display PID only.\n"
|
|
|
|
" raw — Display the raw log message, with no other metadata fields.\n"
|
|
|
|
" tag — Display the priority/tag only.\n"
|
2017-05-15 22:40:33 +02:00
|
|
|
" thread — Display priority, PID and TID of process issuing the message.\n"
|
2016-10-18 20:30:11 +02:00
|
|
|
" threadtime — Display the date, invocation time, priority, tag, and the PID\n"
|
|
|
|
" and TID of the thread issuing the message. (the default format).\n"
|
|
|
|
" time — Display the date, invocation time, priority/tag, and PID of the\n"
|
|
|
|
" process issuing the message.\n"
|
|
|
|
"\nAdverb modifiers can be used in combination:\n"
|
|
|
|
" color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
|
|
|
|
" \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
|
|
|
|
" descriptive — events logs only, descriptions from event-log-tags database.\n"
|
|
|
|
" epoch — Display time as seconds since Jan 1 1970.\n"
|
|
|
|
" monotonic — Display time as cpu seconds since last boot.\n"
|
|
|
|
" printable — Ensure that any binary logging content is escaped.\n"
|
|
|
|
" uid — If permitted, display the UID or Android ID of logged process.\n"
|
|
|
|
" usec — Display time down the microsecond precision.\n"
|
|
|
|
" UTC — Display time as UTC.\n"
|
|
|
|
" year — Add the year to the displayed time.\n"
|
|
|
|
" zone — Add the local timezone to the displayed time.\n"
|
|
|
|
" \"<zone>\" — Print using this public named timezone (experimental).\n\n"
|
|
|
|
);
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format on
|
2016-10-18 20:30:11 +02:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
int Logcat::SetLogFormat(const char* format_string) {
|
|
|
|
AndroidLogPrintFormat format = android_log_formatFromString(format_string);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
// invalid string?
|
|
|
|
if (format == FORMAT_OFF) return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
return android_log_setPrintFormat(logformat_.get(), format);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 20:05:57 +02:00
|
|
|
static std::pair<unsigned long, const char*> format_of_size(unsigned long value) {
|
|
|
|
static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}};
|
|
|
|
size_t i;
|
2014-05-06 16:34:59 +02:00
|
|
|
for (i = 0;
|
2017-02-10 22:09:07 +01:00
|
|
|
(i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
|
|
|
|
value /= 1024, ++i)
|
|
|
|
;
|
2018-09-05 20:05:57 +02:00
|
|
|
return std::make_pair(value, multipliers[i]);
|
2014-05-06 16:34:59 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
static char* parseTime(log_time& t, const char* cp) {
|
|
|
|
char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
|
2017-03-01 17:30:06 +01:00
|
|
|
if (ep) return ep;
|
2015-09-01 00:53:41 +02:00
|
|
|
ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
|
2017-03-01 17:30:06 +01:00
|
|
|
if (ep) return ep;
|
2015-09-01 00:53:41 +02:00
|
|
|
return t.strptime(cp, "%s.%q");
|
2015-08-31 17:01:33 +02:00
|
|
|
}
|
2015-05-27 16:39:56 +02:00
|
|
|
|
2016-08-04 16:43:46 +02:00
|
|
|
// Find last logged line in <outputFileName>, or <outputFileName>.1
|
2017-03-03 00:09:41 +01:00
|
|
|
static log_time lastLogTime(const char* outputFileName) {
|
2015-05-27 16:39:56 +02:00
|
|
|
log_time retval(log_time::EPOCH);
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!outputFileName) return retval;
|
2015-05-27 16:39:56 +02:00
|
|
|
|
|
|
|
std::string directory;
|
2017-03-03 00:09:41 +01:00
|
|
|
const char* file = strrchr(outputFileName, '/');
|
2015-05-27 16:39:56 +02:00
|
|
|
if (!file) {
|
|
|
|
directory = ".";
|
|
|
|
file = outputFileName;
|
|
|
|
} else {
|
2017-03-03 00:09:41 +01:00
|
|
|
directory = std::string(outputFileName, file - outputFileName);
|
2015-05-27 16:39:56 +02:00
|
|
|
++file;
|
|
|
|
}
|
2016-04-01 16:52:20 +02:00
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
|
|
|
|
closedir);
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!dir.get()) return retval;
|
2016-04-01 16:52:20 +02:00
|
|
|
|
2016-08-04 16:43:46 +02:00
|
|
|
log_time now(android_log_clockid());
|
2016-04-01 16:52:20 +02:00
|
|
|
|
2015-05-27 16:39:56 +02:00
|
|
|
size_t len = strlen(file);
|
|
|
|
log_time modulo(0, NS_PER_SEC);
|
2017-02-10 22:09:07 +01:00
|
|
|
struct dirent* dp;
|
2016-04-01 16:52:20 +02:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
while (!!(dp = readdir(dir.get()))) {
|
|
|
|
if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
|
2017-02-10 22:09:07 +01:00
|
|
|
(dp->d_name[len] && ((dp->d_name[len] != '.') ||
|
2017-03-01 17:30:06 +01:00
|
|
|
(strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
|
2015-05-27 16:39:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_name = directory;
|
|
|
|
file_name += "/";
|
|
|
|
file_name += dp->d_name;
|
|
|
|
std::string file;
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!android::base::ReadFileToString(file_name, &file)) continue;
|
2015-05-27 16:39:56 +02:00
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
for (const auto& line : android::base::Split(file, "\n")) {
|
|
|
|
log_time t(log_time::EPOCH);
|
2017-02-10 22:09:07 +01:00
|
|
|
char* ep = parseTime(t, line.c_str());
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!ep || (*ep != ' ')) continue;
|
2015-05-27 16:39:56 +02:00
|
|
|
// determine the time precision of the logs (eg: msec or usec)
|
|
|
|
for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
|
|
|
|
if (t.tv_nsec % (mod * 10)) {
|
|
|
|
modulo.tv_nsec = mod;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We filter any times later than current as we may not have the
|
|
|
|
// year stored with each log entry. Also, since it is possible for
|
|
|
|
// entries to be recorded out of order (very rare) we select the
|
|
|
|
// maximum we find just in case.
|
|
|
|
if ((t < now) && (t > retval)) {
|
|
|
|
retval = t;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We count on the basename file to be the definitive end, so stop here.
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!dp->d_name[len] && found) break;
|
2015-05-27 16:39:56 +02:00
|
|
|
}
|
2017-03-01 17:30:06 +01:00
|
|
|
if (retval == log_time::EPOCH) return retval;
|
2015-05-27 16:39:56 +02:00
|
|
|
// tail_time prints matching or higher, round up by the modulo to prevent
|
|
|
|
// a replay of the last entry we have just checked.
|
|
|
|
retval += modulo;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
void ReportErrorName(const std::string& name, bool allow_security,
|
|
|
|
std::vector<std::string>* errors) {
|
|
|
|
if (allow_security || name != "security") {
|
|
|
|
errors->emplace_back(name);
|
2017-01-20 23:07:29 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
int Logcat::Run(int argc, char** argv) {
|
2017-02-28 18:20:31 +01:00
|
|
|
bool hasSetLogFormat = false;
|
2017-01-20 23:07:29 +01:00
|
|
|
bool clearLog = false;
|
2019-10-29 15:05:24 +01:00
|
|
|
bool security_buffer_selected =
|
|
|
|
false; // Do not report errors on the security buffer unless it is explicitly named.
|
2017-01-20 23:07:29 +01:00
|
|
|
bool getLogSize = false;
|
|
|
|
bool getPruneList = false;
|
|
|
|
bool printStatistics = false;
|
|
|
|
bool printDividers = false;
|
2014-02-11 21:29:31 +01:00
|
|
|
unsigned long setLogSize = 0;
|
2017-03-03 00:09:41 +01:00
|
|
|
const char* setPruneList = nullptr;
|
|
|
|
const char* setId = nullptr;
|
2015-01-26 19:46:44 +01:00
|
|
|
int mode = ANDROID_LOG_RDONLY;
|
2017-02-10 22:09:07 +01:00
|
|
|
std::string forceFilters;
|
2015-04-10 14:51:39 +02:00
|
|
|
size_t tail_lines = 0;
|
2014-02-15 01:05:05 +01:00
|
|
|
log_time tail_time(log_time::EPOCH);
|
2015-06-05 23:10:12 +02:00
|
|
|
size_t pid = 0;
|
2016-03-17 22:04:52 +01:00
|
|
|
bool got_t = false;
|
2019-10-29 15:05:24 +01:00
|
|
|
unsigned id_mask = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (argc == 2 && !strcmp(argv[1], "--help")) {
|
2019-10-25 02:35:26 +02:00
|
|
|
show_help();
|
|
|
|
return EXIT_SUCCESS;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 18:20:31 +01:00
|
|
|
// meant to catch comma-delimited values, but cast a wider
|
|
|
|
// net for stability dealing with possible mistaken inputs.
|
|
|
|
static const char delimiters[] = ",:; \t\n\r\f";
|
|
|
|
|
2018-06-16 00:16:20 +02:00
|
|
|
optind = 0;
|
|
|
|
while (true) {
|
2015-06-05 23:10:12 +02:00
|
|
|
int option_index = 0;
|
2016-03-30 18:38:31 +02:00
|
|
|
// list of long-argument only strings for later comparison
|
2015-06-05 23:10:12 +02:00
|
|
|
static const char pid_str[] = "pid";
|
2016-11-17 00:28:31 +01:00
|
|
|
static const char debug_str[] = "debug";
|
2016-08-03 23:20:41 +02:00
|
|
|
static const char id_str[] = "id";
|
2015-11-30 22:48:56 +01:00
|
|
|
static const char wrap_str[] = "wrap";
|
2016-03-30 18:38:31 +02:00
|
|
|
static const char print_str[] = "print";
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format off
|
2015-06-05 23:10:12 +02:00
|
|
|
static const struct option long_options[] = {
|
2017-03-01 17:30:06 +01:00
|
|
|
{ "binary", no_argument, nullptr, 'B' },
|
|
|
|
{ "buffer", required_argument, nullptr, 'b' },
|
|
|
|
{ "buffer-size", optional_argument, nullptr, 'g' },
|
|
|
|
{ "clear", no_argument, nullptr, 'c' },
|
|
|
|
{ debug_str, no_argument, nullptr, 0 },
|
|
|
|
{ "dividers", no_argument, nullptr, 'D' },
|
|
|
|
{ "file", required_argument, nullptr, 'f' },
|
|
|
|
{ "format", required_argument, nullptr, 'v' },
|
2016-04-01 16:52:20 +02:00
|
|
|
// hidden and undocumented reserved alias for --regex
|
2017-03-01 17:30:06 +01:00
|
|
|
{ "grep", required_argument, nullptr, 'e' },
|
2016-03-30 18:15:09 +02:00
|
|
|
// hidden and undocumented reserved alias for --max-count
|
2017-03-01 17:30:06 +01:00
|
|
|
{ "head", required_argument, nullptr, 'm' },
|
2017-04-03 18:30:20 +02:00
|
|
|
{ "help", no_argument, nullptr, 'h' },
|
2017-03-01 17:30:06 +01:00
|
|
|
{ id_str, required_argument, nullptr, 0 },
|
|
|
|
{ "last", no_argument, nullptr, 'L' },
|
|
|
|
{ "max-count", required_argument, nullptr, 'm' },
|
|
|
|
{ pid_str, required_argument, nullptr, 0 },
|
|
|
|
{ print_str, no_argument, nullptr, 0 },
|
|
|
|
{ "prune", optional_argument, nullptr, 'p' },
|
|
|
|
{ "regex", required_argument, nullptr, 'e' },
|
|
|
|
{ "rotate-count", required_argument, nullptr, 'n' },
|
|
|
|
{ "rotate-kbytes", required_argument, nullptr, 'r' },
|
|
|
|
{ "statistics", no_argument, nullptr, 'S' },
|
2016-03-30 18:15:09 +02:00
|
|
|
// hidden and undocumented reserved alias for -t
|
2017-03-01 17:30:06 +01:00
|
|
|
{ "tail", required_argument, nullptr, 't' },
|
2015-11-30 22:48:56 +01:00
|
|
|
// support, but ignore and do not document, the optional argument
|
2017-03-01 17:30:06 +01:00
|
|
|
{ wrap_str, optional_argument, nullptr, 0 },
|
|
|
|
{ nullptr, 0, nullptr, 0 }
|
2015-06-05 23:10:12 +02:00
|
|
|
};
|
2017-02-10 22:09:07 +01:00
|
|
|
// clang-format on
|
2015-06-05 23:10:12 +02:00
|
|
|
|
2018-06-16 00:16:20 +02:00
|
|
|
int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
|
|
|
|
&option_index);
|
|
|
|
if (c == -1) break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-06-16 00:16:20 +02:00
|
|
|
switch (c) {
|
2015-06-05 23:10:12 +02:00
|
|
|
case 0:
|
2016-08-03 23:20:41 +02:00
|
|
|
// only long options
|
2015-06-05 23:10:12 +02:00
|
|
|
if (long_options[option_index].name == pid_str) {
|
2019-08-02 19:13:57 +02:00
|
|
|
if (pid != 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Only one --pid argument can be provided.");
|
2019-08-02 19:13:57 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!ParseUint(optarg, &pid) || pid < 1) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "%s %s out of range.",
|
|
|
|
long_options[option_index].name, optarg);
|
2015-06-05 23:10:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-11-30 22:48:56 +01:00
|
|
|
if (long_options[option_index].name == wrap_str) {
|
2017-02-10 22:09:07 +01:00
|
|
|
mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
|
2015-11-30 22:48:56 +01:00
|
|
|
ANDROID_LOG_NONBLOCK;
|
|
|
|
// ToDo: implement API that supports setting a wrap timeout
|
|
|
|
size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
|
2019-10-29 15:05:24 +01:00
|
|
|
if (optarg && (!ParseUint(optarg, &dummy) || dummy < 1)) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "%s %s out of range.",
|
|
|
|
long_options[option_index].name, optarg);
|
2015-11-30 22:48:56 +01:00
|
|
|
}
|
2019-10-25 02:35:26 +02:00
|
|
|
if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
|
|
|
|
fprintf(stderr, "WARNING: %s %u seconds, ignoring %zu\n",
|
|
|
|
long_options[option_index].name, ANDROID_LOG_WRAP_DEFAULT_TIMEOUT,
|
|
|
|
dummy);
|
2015-11-30 22:48:56 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-03-30 18:38:31 +02:00
|
|
|
if (long_options[option_index].name == print_str) {
|
2019-10-25 02:35:26 +02:00
|
|
|
print_it_anyways_ = true;
|
2016-03-30 18:38:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-11-17 00:28:31 +01:00
|
|
|
if (long_options[option_index].name == debug_str) {
|
2019-10-25 02:35:26 +02:00
|
|
|
debug_ = true;
|
2016-11-17 00:28:31 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-08-03 23:20:41 +02:00
|
|
|
if (long_options[option_index].name == id_str) {
|
2018-06-16 00:16:20 +02:00
|
|
|
setId = (optarg && optarg[0]) ? optarg : nullptr;
|
2016-08-03 23:20:41 +02:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2015-06-05 23:10:12 +02:00
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
case 's':
|
2009-03-04 04:32:55 +01:00
|
|
|
// default to all silent
|
2019-10-25 02:35:26 +02:00
|
|
|
android_log_addFilterRule(logformat_.get(), "*:s");
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
case 'c':
|
2017-01-20 23:07:29 +01:00
|
|
|
clearLog = true;
|
2015-01-26 19:46:44 +01:00
|
|
|
mode |= ANDROID_LOG_WRONLY;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-12-15 19:01:31 +01:00
|
|
|
case 'L':
|
2017-02-10 22:09:07 +01:00
|
|
|
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
|
|
|
|
ANDROID_LOG_NONBLOCK;
|
|
|
|
break;
|
2014-12-15 19:01:31 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
case 'd':
|
2015-01-26 19:46:44 +01:00
|
|
|
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-03-12 05:32:17 +01:00
|
|
|
case 't':
|
2016-03-17 22:04:52 +01:00
|
|
|
got_t = true;
|
2015-01-26 19:46:44 +01:00
|
|
|
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2013-12-09 22:47:00 +01:00
|
|
|
case 'T':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (strspn(optarg, "0123456789") != strlen(optarg)) {
|
|
|
|
char* cp = parseTime(tail_time, optarg);
|
2014-02-15 01:05:05 +01:00
|
|
|
if (!cp) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "-%c '%s' not in time format.", c, optarg);
|
2014-02-15 01:05:05 +01:00
|
|
|
}
|
|
|
|
if (*cp) {
|
2018-06-16 00:16:20 +02:00
|
|
|
char ch = *cp;
|
2014-02-15 01:05:05 +01:00
|
|
|
*cp = '\0';
|
2019-12-06 19:25:37 +01:00
|
|
|
fprintf(stderr, "WARNING: -%c '%s' '%c%s' time truncated\n", c, optarg, ch,
|
|
|
|
cp + 1);
|
2018-06-16 00:16:20 +02:00
|
|
|
*cp = ch;
|
2014-02-15 01:05:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!ParseUint(optarg, &tail_lines) || tail_lines < 1) {
|
2019-10-25 02:35:26 +02:00
|
|
|
fprintf(stderr, "WARNING: -%c %s invalid, setting to 1\n", c, optarg);
|
2014-02-15 01:05:05 +01:00
|
|
|
tail_lines = 1;
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2010-03-12 05:32:17 +01:00
|
|
|
|
2015-01-26 22:41:33 +01:00
|
|
|
case 'D':
|
|
|
|
printDividers = true;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2015-01-26 22:41:33 +01:00
|
|
|
|
2016-03-18 00:18:55 +01:00
|
|
|
case 'e':
|
2019-10-25 02:35:26 +02:00
|
|
|
regex_.reset(new std::regex(optarg));
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2016-03-18 00:18:55 +01:00
|
|
|
|
2016-03-17 22:04:52 +01:00
|
|
|
case 'm': {
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!ParseUint(optarg, &max_count_) || max_count_ < 1) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "-%c '%s' isn't an integer greater than zero.", c,
|
|
|
|
optarg);
|
2016-03-17 22:04:52 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
} break;
|
2016-03-17 22:04:52 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
case 'g':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!optarg) {
|
2017-01-20 23:07:29 +01:00
|
|
|
getLogSize = true;
|
logcat: Add low hanging fruit long arguments
Add long arguments file, rotate_kbytes, rotate_count, format,
dividers, clear, buffer_size, last, buffer, binary, statistics
and prune.
Bug: 21615139
Change-Id: Ieb1bbe3d47e6711a1e91c74522630d39f8981621
2015-11-30 21:57:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
case 'G': {
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!ParseByteCount(optarg, &setLogSize) || setLogSize < 1) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "-G must be specified as <num><multiplier>.");
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
} break;
|
2014-02-11 21:29:31 +01:00
|
|
|
|
|
|
|
case 'p':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!optarg) {
|
2017-01-20 23:07:29 +01:00
|
|
|
getPruneList = true;
|
logcat: Add low hanging fruit long arguments
Add long arguments file, rotate_kbytes, rotate_count, format,
dividers, clear, buffer_size, last, buffer, binary, statistics
and prune.
Bug: 21615139
Change-Id: Ieb1bbe3d47e6711a1e91c74522630d39f8981621
2015-11-30 21:57:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-09-13 20:08:41 +02:00
|
|
|
FALLTHROUGH_INTENDED;
|
2014-02-11 21:29:31 +01:00
|
|
|
|
|
|
|
case 'P':
|
2018-06-16 00:16:20 +02:00
|
|
|
setPruneList = optarg;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2014-02-11 21:29:31 +01:00
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
case 'b':
|
|
|
|
for (const auto& buffer : Split(optarg, delimiters)) {
|
|
|
|
if (buffer == "default") {
|
|
|
|
id_mask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) | (1 << LOG_ID_CRASH);
|
|
|
|
} else if (buffer == "all") {
|
|
|
|
id_mask = -1;
|
2016-04-11 23:03:48 +02:00
|
|
|
} else {
|
2019-10-29 15:05:24 +01:00
|
|
|
log_id_t log_id = android_name_to_log_id(buffer.c_str());
|
|
|
|
if (log_id >= LOG_ID_MAX) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Unknown buffer '%s' listed for -b.",
|
|
|
|
buffer.c_str());
|
2015-12-04 19:59:45 +01:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
if (log_id == LOG_ID_SECURITY) {
|
|
|
|
security_buffer_selected = true;
|
2015-12-04 19:59:45 +01:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
id_mask |= (1 << log_id);
|
2016-04-11 23:03:48 +02:00
|
|
|
}
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
case 'B':
|
2019-10-25 02:35:26 +02:00
|
|
|
print_binary_ = 1;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
case 'f':
|
2017-03-01 17:30:06 +01:00
|
|
|
if ((tail_time == log_time::EPOCH) && !tail_lines) {
|
2018-06-16 00:16:20 +02:00
|
|
|
tail_time = lastLogTime(optarg);
|
2015-05-27 16:39:56 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
// redirect output to a file
|
2019-10-25 02:35:26 +02:00
|
|
|
output_file_name_ = optarg;
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-06-07 22:03:10 +02:00
|
|
|
case 'r':
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!ParseUint(optarg, &log_rotate_size_kb_) || log_rotate_size_kb_ < 1) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -r.", optarg);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-06-07 22:03:10 +02:00
|
|
|
case 'n':
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!ParseUint(optarg, &max_rotated_logs_) || max_rotated_logs_ < 1) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -n.", optarg);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
case 'v':
|
2018-06-16 00:16:20 +02:00
|
|
|
if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
|
2019-10-25 02:35:26 +02:00
|
|
|
show_format_help();
|
|
|
|
return EXIT_SUCCESS;
|
2016-10-18 20:30:11 +02:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
for (const auto& arg : Split(optarg, delimiters)) {
|
|
|
|
int err = SetLogFormat(arg.c_str());
|
2017-02-28 18:20:31 +01:00
|
|
|
if (err < 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid parameter '%s' to -v.", arg.c_str());
|
2017-02-28 18:20:31 +01:00
|
|
|
}
|
|
|
|
if (err) hasSetLogFormat = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
case 'Q':
|
2017-02-21 23:31:19 +01:00
|
|
|
#define LOGCAT_FILTER "androidboot.logcat="
|
|
|
|
#define CONSOLE_PIPE_OPTION "androidboot.consolepipe="
|
2017-02-10 22:09:07 +01:00
|
|
|
#define CONSOLE_OPTION "androidboot.console="
|
2017-02-21 23:31:19 +01:00
|
|
|
#define QEMU_PROPERTY "ro.kernel.qemu"
|
|
|
|
#define QEMU_CMDLINE "qemu.cmdline"
|
2017-02-10 22:09:07 +01:00
|
|
|
// This is a *hidden* option used to start a version of logcat
|
|
|
|
// in an emulated device only. It basically looks for
|
|
|
|
// androidboot.logcat= on the kernel command line. If
|
|
|
|
// something is found, it extracts a log filter and uses it to
|
2017-02-21 23:31:19 +01:00
|
|
|
// run the program. The logcat output will go to consolepipe if
|
|
|
|
// androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise,
|
|
|
|
// it goes to androidboot.console (e.g. tty)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2017-02-21 23:31:19 +01:00
|
|
|
// if not in emulator, exit quietly
|
|
|
|
if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) {
|
2019-10-25 02:35:26 +02:00
|
|
|
return EXIT_SUCCESS;
|
2017-02-21 23:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, "");
|
|
|
|
if (cmdline.empty()) {
|
|
|
|
android::base::ReadFileToString("/proc/cmdline", &cmdline);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER);
|
2017-02-10 22:09:07 +01:00
|
|
|
// if nothing found or invalid filters, exit quietly
|
2017-02-21 23:31:19 +01:00
|
|
|
if (!logcatFilter) {
|
2019-10-25 02:35:26 +02:00
|
|
|
return EXIT_SUCCESS;
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
const char* p = logcatFilter + strlen(LOGCAT_FILTER);
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* q = strpbrk(p, " \t\n\r");
|
|
|
|
if (!q) q = p + strlen(p);
|
|
|
|
forceFilters = std::string(p, q);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
// redirect our output to the emulator console pipe or console
|
|
|
|
const char* consolePipe =
|
|
|
|
strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION);
|
2017-02-10 22:09:07 +01:00
|
|
|
const char* console =
|
|
|
|
strstr(cmdline.c_str(), CONSOLE_OPTION);
|
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
if (consolePipe) {
|
|
|
|
p = consolePipe + strlen(CONSOLE_PIPE_OPTION);
|
|
|
|
} else if (console) {
|
|
|
|
p = console + strlen(CONSOLE_OPTION);
|
|
|
|
} else {
|
2019-10-25 02:35:26 +02:00
|
|
|
return EXIT_FAILURE;
|
2017-02-21 23:31:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
q = strpbrk(p, " \t\n\r");
|
|
|
|
int len = q ? q - p : strlen(p);
|
|
|
|
std::string devname = "/dev/" + std::string(p, len);
|
2017-02-21 23:31:19 +01:00
|
|
|
std::string pipePurpose("pipe:logcat");
|
|
|
|
if (consolePipe) {
|
|
|
|
// example: "qemu_pipe,pipe:logcat"
|
|
|
|
// upon opening of /dev/qemu_pipe, the "pipe:logcat"
|
|
|
|
// string with trailing '\0' should be written to the fd
|
2017-08-03 22:56:49 +02:00
|
|
|
size_t pos = devname.find(',');
|
2017-02-21 23:31:19 +01:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
pipePurpose = devname.substr(pos + 1);
|
|
|
|
devname = devname.substr(0, pos);
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
fprintf(stderr, "logcat using %s\n", devname.c_str());
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
int fd = open(devname.c_str(), O_WRONLY | O_CLOEXEC);
|
|
|
|
if (fd < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2017-02-21 23:31:19 +01:00
|
|
|
if (consolePipe) {
|
|
|
|
// need the trailing '\0'
|
2019-10-25 02:35:26 +02:00
|
|
|
if (!android::base::WriteFully(fd, pipePurpose.c_str(),
|
|
|
|
pipePurpose.size() + 1)) {
|
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
2017-02-21 23:31:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
// close output and error channels, replace with console
|
2019-11-27 21:58:58 +01:00
|
|
|
dup2(fd, output_fd_.get());
|
2019-10-25 02:35:26 +02:00
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
close(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-02-06 23:48:50 +01:00
|
|
|
case 'S':
|
2017-01-20 23:07:29 +01:00
|
|
|
printStatistics = true;
|
2014-02-06 23:48:50 +01:00
|
|
|
break;
|
|
|
|
|
2015-04-10 14:51:39 +02:00
|
|
|
case ':':
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Option '%s' needs an argument.", argv[optind - 1]);
|
|
|
|
break;
|
2015-04-10 14:51:39 +02:00
|
|
|
|
2017-04-03 18:30:20 +02:00
|
|
|
case 'h':
|
2019-10-25 02:35:26 +02:00
|
|
|
show_help();
|
|
|
|
show_format_help();
|
|
|
|
return EXIT_SUCCESS;
|
2017-04-03 18:30:20 +02:00
|
|
|
|
2019-12-06 19:25:37 +01:00
|
|
|
case '?':
|
|
|
|
error(EXIT_FAILURE, 0, "Unknown option '%s'.", argv[optind - 1]);
|
|
|
|
break;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
default:
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Unknown getopt_long() result '%c'.", c);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (max_count_ && got_t) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Cannot use -m (--max-count) and -t together.");
|
2016-03-17 22:04:52 +01:00
|
|
|
}
|
2019-10-25 02:35:26 +02:00
|
|
|
if (print_it_anyways_ && (!regex_ || !max_count_)) {
|
2016-03-30 18:38:31 +02:00
|
|
|
// One day it would be nice if --print -v color and --regex <expr>
|
|
|
|
// could play with each other and show regex highlighted content.
|
2019-10-25 02:35:26 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: "
|
|
|
|
"--print ignored, to be used in combination with\n"
|
|
|
|
" "
|
|
|
|
"--regex <expr> and --max-count <N>\n");
|
|
|
|
print_it_anyways_ = false;
|
2016-03-30 18:38:31 +02:00
|
|
|
}
|
2016-03-17 22:04:52 +01:00
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
// If no buffers are specified, default to using these buffers.
|
|
|
|
if (id_mask == 0) {
|
|
|
|
id_mask = (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) | (1 << LOG_ID_CRASH) |
|
|
|
|
(1 << LOG_ID_KERNEL);
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (log_rotate_size_kb_ != 0 && !output_file_name_) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "-r requires -f as well.");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (setId != 0) {
|
|
|
|
if (!output_file_name_) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "--id='%s' requires -f as well.", setId);
|
2016-08-03 23:20:41 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
std::string file_name = StringPrintf("%s.id", output_file_name_);
|
2016-08-03 23:20:41 +02:00
|
|
|
std::string file;
|
|
|
|
bool file_ok = android::base::ReadFileToString(file_name, &file);
|
2017-02-10 22:09:07 +01:00
|
|
|
android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
|
|
|
|
getuid(), getgid());
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!file_ok || !file.compare(setId)) setId = nullptr;
|
2016-08-03 23:20:41 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!hasSetLogFormat) {
|
2019-10-25 02:35:26 +02:00
|
|
|
const char* logFormat = getenv("ANDROID_PRINTF_LOG");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!!logFormat) {
|
2019-10-29 15:05:24 +01:00
|
|
|
for (const auto& arg : Split(logFormat, delimiters)) {
|
|
|
|
int err = SetLogFormat(arg.c_str());
|
2017-02-28 18:20:31 +01:00
|
|
|
// environment should not cause crash of logcat
|
2019-10-25 02:35:26 +02:00
|
|
|
if (err < 0) {
|
2019-10-29 15:05:24 +01:00
|
|
|
fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg.c_str());
|
2017-02-28 18:20:31 +01:00
|
|
|
}
|
|
|
|
if (err > 0) hasSetLogFormat = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-02-28 18:20:31 +01:00
|
|
|
}
|
|
|
|
if (!hasSetLogFormat) {
|
2019-10-25 02:35:26 +02:00
|
|
|
SetLogFormat("threadtime");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:09:07 +01:00
|
|
|
if (forceFilters.size()) {
|
2019-10-29 15:05:24 +01:00
|
|
|
int err = android_log_addFilterString(logformat_.get(), forceFilters.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
if (err < 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid filter expression in logcat args.");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-06-16 00:16:20 +02:00
|
|
|
} else if (argc == optind) {
|
2009-03-04 04:32:55 +01:00
|
|
|
// Add from environment variable
|
2019-10-25 02:35:26 +02:00
|
|
|
const char* env_tags_orig = getenv("ANDROID_LOG_TAGS");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!!env_tags_orig) {
|
2019-10-29 15:05:24 +01:00
|
|
|
int err = android_log_addFilterString(logformat_.get(), env_tags_orig);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-11-22 19:55:48 +01:00
|
|
|
if (err < 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid filter expression in ANDROID_LOG_TAGS.");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add from commandline
|
2018-06-16 00:16:20 +02:00
|
|
|
for (int i = optind ; i < argc ; i++) {
|
2019-10-29 15:05:24 +01:00
|
|
|
int err = android_log_addFilterString(logformat_.get(), argv[i]);
|
2013-11-22 19:55:48 +01:00
|
|
|
if (err < 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid filter expression '%s'.", argv[i]);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
liblog: simplify logd 'command' functions and struct logger
There are a set of functions, such as android_logger_get_log_size()
and android_logger_get_prune_list() that talk to the logd command
socket to perform their activities. There's a transport abstraction
layer that handles these symbols to optionally route them to other
transports, originally designed for pstore or local logger; however
these functions fundamentally only make sense for logd.
Ideally, these functions would be removed and new functions would be
added that do not depend on struct logger_list or struct logger and
more clearly indicate that they only work with logd. For example:
android_logger_get_size(struct logger*) could be
logd_get_buffer_size(log_id_t log_id). We would remove the need to
'open' the struct logger and make it clear that it only operates on
logd.
Since liblog is an llndk library however, we cannot change or remove
these symbols. Since these symbols are not frequently used, it seems
acceptable to keep them as is and not introduce improved versions.
We, however, do want to simplify the code that handles them and this
change removes the transport abstraction layer that handles them.
They retain the behavior that unless the struct logger_list was opened
for logd, that the functions return -EINVAL.
The one exception to this is android_logger_clear(). If the struct
logger provided to this function was opened from a struct logger_list
that used pstore for its mode argument, this function will clear the
entire pstore log. This function does not respect the 'logId'
parameter of the struct logger, since that would not be possible.
This change removes this android_logger_clear() behavior and makes it
strictly for logd, for symmetry with the rest of the functions and due
to the lack of clarity regarding the 'logId' parameter of its input.
The only caller of this function, logcat, will clear pstore directly.
struct logger was built to encapsulate the information needed to
connect to a logger device from the old kernel logger. Now that we
only support reading from pstore and from logd, there is much less
information needed to be captured. Specifically, we only need to know
the log_id and whether or not it was opened as part of a pstore or
logd 'list'.
Test: liblog-unit-test
Test: logcat -c/-g/-G/-p/-P/-S work
Test: logcat -c works with -L
Test: logcat -g/-G/-p/-P/-S continue to fail with -L
Change-Id: I2c549b6f8539de94510e223949ab209ecc40e2d0
2019-11-14 17:56:39 +01:00
|
|
|
if (mode & ANDROID_LOG_PSTORE) {
|
2019-12-04 23:37:38 +01:00
|
|
|
if (output_file_name_) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "-c is ambiguous with both -f and -L specified.");
|
2019-12-04 23:37:38 +01:00
|
|
|
}
|
|
|
|
if (setLogSize || getLogSize || printStatistics || getPruneList || setPruneList) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "-L is incompatible with -g/-G, -S, and -p/-P.");
|
2019-12-04 23:37:38 +01:00
|
|
|
}
|
liblog: simplify logd 'command' functions and struct logger
There are a set of functions, such as android_logger_get_log_size()
and android_logger_get_prune_list() that talk to the logd command
socket to perform their activities. There's a transport abstraction
layer that handles these symbols to optionally route them to other
transports, originally designed for pstore or local logger; however
these functions fundamentally only make sense for logd.
Ideally, these functions would be removed and new functions would be
added that do not depend on struct logger_list or struct logger and
more clearly indicate that they only work with logd. For example:
android_logger_get_size(struct logger*) could be
logd_get_buffer_size(log_id_t log_id). We would remove the need to
'open' the struct logger and make it clear that it only operates on
logd.
Since liblog is an llndk library however, we cannot change or remove
these symbols. Since these symbols are not frequently used, it seems
acceptable to keep them as is and not introduce improved versions.
We, however, do want to simplify the code that handles them and this
change removes the transport abstraction layer that handles them.
They retain the behavior that unless the struct logger_list was opened
for logd, that the functions return -EINVAL.
The one exception to this is android_logger_clear(). If the struct
logger provided to this function was opened from a struct logger_list
that used pstore for its mode argument, this function will clear the
entire pstore log. This function does not respect the 'logId'
parameter of the struct logger, since that would not be possible.
This change removes this android_logger_clear() behavior and makes it
strictly for logd, for symmetry with the rest of the functions and due
to the lack of clarity regarding the 'logId' parameter of its input.
The only caller of this function, logcat, will clear pstore directly.
struct logger was built to encapsulate the information needed to
connect to a logger device from the old kernel logger. Now that we
only support reading from pstore and from logd, there is much less
information needed to be captured. Specifically, we only need to know
the log_id and whether or not it was opened as part of a pstore or
logd 'list'.
Test: liblog-unit-test
Test: logcat -c/-g/-G/-p/-P/-S work
Test: logcat -c works with -L
Test: logcat -g/-G/-p/-P/-S continue to fail with -L
Change-Id: I2c549b6f8539de94510e223949ab209ecc40e2d0
2019-11-14 17:56:39 +01:00
|
|
|
if (clearLog) {
|
|
|
|
unlink("/sys/fs/pstore/pmsg-ramoops-0");
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2019-12-04 23:37:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (output_file_name_) {
|
liblog: simplify logd 'command' functions and struct logger
There are a set of functions, such as android_logger_get_log_size()
and android_logger_get_prune_list() that talk to the logd command
socket to perform their activities. There's a transport abstraction
layer that handles these symbols to optionally route them to other
transports, originally designed for pstore or local logger; however
these functions fundamentally only make sense for logd.
Ideally, these functions would be removed and new functions would be
added that do not depend on struct logger_list or struct logger and
more clearly indicate that they only work with logd. For example:
android_logger_get_size(struct logger*) could be
logd_get_buffer_size(log_id_t log_id). We would remove the need to
'open' the struct logger and make it clear that it only operates on
logd.
Since liblog is an llndk library however, we cannot change or remove
these symbols. Since these symbols are not frequently used, it seems
acceptable to keep them as is and not introduce improved versions.
We, however, do want to simplify the code that handles them and this
change removes the transport abstraction layer that handles them.
They retain the behavior that unless the struct logger_list was opened
for logd, that the functions return -EINVAL.
The one exception to this is android_logger_clear(). If the struct
logger provided to this function was opened from a struct logger_list
that used pstore for its mode argument, this function will clear the
entire pstore log. This function does not respect the 'logId'
parameter of the struct logger, since that would not be possible.
This change removes this android_logger_clear() behavior and makes it
strictly for logd, for symmetry with the rest of the functions and due
to the lack of clarity regarding the 'logId' parameter of its input.
The only caller of this function, logcat, will clear pstore directly.
struct logger was built to encapsulate the information needed to
connect to a logger device from the old kernel logger. Now that we
only support reading from pstore and from logd, there is much less
information needed to be captured. Specifically, we only need to know
the log_id and whether or not it was opened as part of a pstore or
logd 'list'.
Test: liblog-unit-test
Test: logcat -c/-g/-G/-p/-P/-S work
Test: logcat -c works with -L
Test: logcat -g/-G/-p/-P/-S continue to fail with -L
Change-Id: I2c549b6f8539de94510e223949ab209ecc40e2d0
2019-11-14 17:56:39 +01:00
|
|
|
if (setLogSize || getLogSize || printStatistics || getPruneList || setPruneList) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "-f is incompatible with -g/-G, -S, and -p/-P.");
|
2019-12-04 23:37:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clearLog || setId) {
|
|
|
|
int max_rotation_count_digits =
|
|
|
|
max_rotated_logs_ > 0 ? (int)(floor(log10(max_rotated_logs_) + 1)) : 0;
|
|
|
|
|
|
|
|
for (int i = max_rotated_logs_; i >= 0; --i) {
|
|
|
|
std::string file;
|
|
|
|
|
|
|
|
if (!i) {
|
|
|
|
file = output_file_name_;
|
|
|
|
} else {
|
|
|
|
file = StringPrintf("%s.%.*d", output_file_name_, max_rotation_count_digits, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
int err = unlink(file.c_str());
|
|
|
|
|
|
|
|
if (err < 0 && errno != ENOENT) {
|
|
|
|
fprintf(stderr, "failed to delete log file '%s': %s\n", file.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clearLog) {
|
|
|
|
return EXIT_SUCCESS;
|
liblog: simplify logd 'command' functions and struct logger
There are a set of functions, such as android_logger_get_log_size()
and android_logger_get_prune_list() that talk to the logd command
socket to perform their activities. There's a transport abstraction
layer that handles these symbols to optionally route them to other
transports, originally designed for pstore or local logger; however
these functions fundamentally only make sense for logd.
Ideally, these functions would be removed and new functions would be
added that do not depend on struct logger_list or struct logger and
more clearly indicate that they only work with logd. For example:
android_logger_get_size(struct logger*) could be
logd_get_buffer_size(log_id_t log_id). We would remove the need to
'open' the struct logger and make it clear that it only operates on
logd.
Since liblog is an llndk library however, we cannot change or remove
these symbols. Since these symbols are not frequently used, it seems
acceptable to keep them as is and not introduce improved versions.
We, however, do want to simplify the code that handles them and this
change removes the transport abstraction layer that handles them.
They retain the behavior that unless the struct logger_list was opened
for logd, that the functions return -EINVAL.
The one exception to this is android_logger_clear(). If the struct
logger provided to this function was opened from a struct logger_list
that used pstore for its mode argument, this function will clear the
entire pstore log. This function does not respect the 'logId'
parameter of the struct logger, since that would not be possible.
This change removes this android_logger_clear() behavior and makes it
strictly for logd, for symmetry with the rest of the functions and due
to the lack of clarity regarding the 'logId' parameter of its input.
The only caller of this function, logcat, will clear pstore directly.
struct logger was built to encapsulate the information needed to
connect to a logger device from the old kernel logger. Now that we
only support reading from pstore and from logd, there is much less
information needed to be captured. Specifically, we only need to know
the log_id and whether or not it was opened as part of a pstore or
logd 'list'.
Test: liblog-unit-test
Test: logcat -c/-g/-G/-p/-P/-S work
Test: logcat -c works with -L
Test: logcat -g/-G/-p/-P/-S continue to fail with -L
Change-Id: I2c549b6f8539de94510e223949ab209ecc40e2d0
2019-11-14 17:56:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
std::unique_ptr<logger_list, decltype(&android_logger_list_free)> logger_list{
|
|
|
|
nullptr, &android_logger_list_free};
|
2014-02-15 01:05:05 +01:00
|
|
|
if (tail_time != log_time::EPOCH) {
|
2019-10-29 15:05:24 +01:00
|
|
|
logger_list.reset(android_logger_list_alloc_time(mode, tail_time, pid));
|
2014-02-15 01:05:05 +01:00
|
|
|
} else {
|
2019-10-29 15:05:24 +01:00
|
|
|
logger_list.reset(android_logger_list_alloc(mode, tail_lines, pid));
|
2014-02-15 01:05:05 +01:00
|
|
|
}
|
2015-09-17 00:34:00 +02:00
|
|
|
// We have three orthogonal actions below to clear, set log size and
|
|
|
|
// get log size. All sharing the same iteration loop.
|
2019-10-29 15:05:24 +01:00
|
|
|
std::vector<std::string> open_device_failures;
|
|
|
|
std::vector<std::string> clear_failures;
|
|
|
|
std::vector<std::string> set_size_failures;
|
|
|
|
std::vector<std::string> get_size_failures;
|
|
|
|
|
|
|
|
for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
|
|
|
|
if (!(id_mask & (1 << i))) continue;
|
|
|
|
const char* buffer_name = android_log_id_to_name(static_cast<log_id_t>(i));
|
|
|
|
|
|
|
|
auto logger = android_logger_open(logger_list.get(), static_cast<log_id_t>(i));
|
|
|
|
if (logger == nullptr) {
|
|
|
|
ReportErrorName(buffer_name, security_buffer_selected, &open_device_failures);
|
2015-09-17 00:34:00 +02:00
|
|
|
continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-12-04 23:37:38 +01:00
|
|
|
if (clearLog) {
|
|
|
|
if (android_logger_clear(logger)) {
|
2019-10-29 15:05:24 +01:00
|
|
|
ReportErrorName(buffer_name, security_buffer_selected, &clear_failures);
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-09-17 00:34:00 +02:00
|
|
|
if (setLogSize) {
|
2019-10-29 15:05:24 +01:00
|
|
|
if (android_logger_set_log_size(logger, setLogSize)) {
|
|
|
|
ReportErrorName(buffer_name, security_buffer_selected, &set_size_failures);
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
|
2010-02-26 19:04:23 +01:00
|
|
|
if (getLogSize) {
|
2019-10-29 15:05:24 +01:00
|
|
|
long size = android_logger_get_log_size(logger);
|
|
|
|
long readable = android_logger_get_log_readable_size(logger);
|
2015-09-17 00:34:00 +02:00
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
if (size < 0 || readable < 0) {
|
|
|
|
ReportErrorName(buffer_name, security_buffer_selected, &get_size_failures);
|
2015-09-17 00:34:00 +02:00
|
|
|
} else {
|
2018-09-05 20:05:57 +02:00
|
|
|
auto size_format = format_of_size(size);
|
|
|
|
auto readable_format = format_of_size(readable);
|
2017-02-10 22:09:07 +01:00
|
|
|
std::string str = android::base::StringPrintf(
|
2019-10-29 15:05:24 +01:00
|
|
|
"%s: ring buffer is %lu %sB (%lu %sB consumed),"
|
|
|
|
" max entry is %d B, max payload is %d B\n",
|
|
|
|
buffer_name, size_format.first, size_format.second, readable_format.first,
|
|
|
|
readable_format.second, (int)LOGGER_ENTRY_MAX_LEN,
|
|
|
|
(int)LOGGER_ENTRY_MAX_PAYLOAD);
|
2019-10-25 02:35:26 +02:00
|
|
|
TEMP_FAILURE_RETRY(write(output_fd_.get(), str.data(), str.length()));
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2010-02-26 19:04:23 +01:00
|
|
|
}
|
2017-02-10 22:09:07 +01:00
|
|
|
|
2015-09-17 00:34:00 +02:00
|
|
|
// report any errors in the above loop and exit
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!open_device_failures.empty()) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Unable to open log device%s '%s'.",
|
|
|
|
open_device_failures.size() > 1 ? "s" : "", Join(open_device_failures, ",").c_str());
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!clear_failures.empty()) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "failed to clear the '%s' log%s.", Join(clear_failures, ",").c_str(),
|
|
|
|
clear_failures.size() > 1 ? "s" : "");
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!set_size_failures.empty()) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "failed to set the '%s' log size%s.",
|
|
|
|
Join(set_size_failures, ",").c_str(), set_size_failures.size() > 1 ? "s" : "");
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
2019-10-29 15:05:24 +01:00
|
|
|
if (!get_size_failures.empty()) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "failed to get the readable '%s' log size%s.",
|
|
|
|
Join(get_size_failures, ",").c_str(), get_size_failures.size() > 1 ? "s" : "");
|
2015-09-17 00:34:00 +02:00
|
|
|
}
|
2010-02-26 19:04:23 +01:00
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
if (setPruneList) {
|
2015-04-10 14:51:39 +02:00
|
|
|
size_t len = strlen(setPruneList);
|
2019-12-06 20:01:51 +01:00
|
|
|
if (android_logger_set_prune_list(logger_list.get(), setPruneList, len)) {
|
|
|
|
error(EXIT_FAILURE, 0, "Failed to set the prune list.");
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2019-10-25 02:35:26 +02:00
|
|
|
return EXIT_SUCCESS;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
|
2014-04-02 02:19:47 +02:00
|
|
|
if (printStatistics || getPruneList) {
|
2019-12-06 22:33:11 +01:00
|
|
|
std::string buf(8192, '\0');
|
|
|
|
size_t ret_length = 0;
|
|
|
|
int retry = 32;
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2019-12-06 22:33:11 +01:00
|
|
|
for (; retry >= 0; --retry) {
|
2014-02-11 21:29:31 +01:00
|
|
|
if (getPruneList) {
|
2019-12-06 22:33:11 +01:00
|
|
|
android_logger_get_prune_list(logger_list.get(), buf.data(), buf.size());
|
2014-02-11 21:29:31 +01:00
|
|
|
} else {
|
2019-12-06 22:33:11 +01:00
|
|
|
android_logger_get_statistics(logger_list.get(), buf.data(), buf.size());
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2019-12-06 22:33:11 +01:00
|
|
|
|
|
|
|
ret_length = atol(buf.c_str());
|
|
|
|
if (ret_length < 3) {
|
|
|
|
error(EXIT_FAILURE, 0, "Failed to read data.");
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
2019-12-06 22:33:11 +01:00
|
|
|
|
|
|
|
if (ret_length < buf.size()) {
|
2014-02-06 23:48:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-12-06 22:33:11 +01:00
|
|
|
|
|
|
|
buf.resize(ret_length + 1);
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 22:33:11 +01:00
|
|
|
if (retry < 0) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Failed to read data.");
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 22:33:11 +01:00
|
|
|
buf.resize(ret_length);
|
|
|
|
if (buf.back() == '\f') {
|
|
|
|
buf.pop_back();
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 22:33:11 +01:00
|
|
|
// Remove the byte count prefix
|
|
|
|
const char* cp = buf.c_str();
|
|
|
|
while (isdigit(*cp)) ++cp;
|
|
|
|
if (*cp == '\n') ++cp;
|
|
|
|
|
|
|
|
size_t len = strlen(cp);
|
2019-10-25 02:35:26 +02:00
|
|
|
TEMP_FAILURE_RETRY(write(output_fd_.get(), cp, len));
|
|
|
|
return EXIT_SUCCESS;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (getLogSize || setLogSize || clearLog) return EXIT_SUCCESS;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
SetupOutputAndSchedulingPolicy(!(mode & ANDROID_LOG_NONBLOCK));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
while (!max_count_ || print_count_ < max_count_) {
|
2013-11-22 19:55:48 +01:00
|
|
|
struct log_msg log_msg;
|
2019-10-29 15:05:24 +01:00
|
|
|
int ret = android_logger_list_read(logger_list.get(), &log_msg);
|
2017-03-01 17:30:06 +01:00
|
|
|
if (!ret) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, R"init(Unexpected EOF!
|
2019-11-21 19:31:06 +01:00
|
|
|
|
|
|
|
This means that either logd crashed, or more likely, this instance of logcat was unable to read log
|
|
|
|
messages as quickly as they were being produced.
|
|
|
|
|
2019-12-06 19:25:37 +01:00
|
|
|
If you have enabled significant logging, look into using the -G option to increase log buffer sizes.)init");
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2017-03-01 17:30:06 +01:00
|
|
|
if (ret == -EAGAIN) break;
|
2013-11-22 19:55:48 +01:00
|
|
|
|
|
|
|
if (ret == -EIO) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Unexpected EOF!");
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
if (ret == -EINVAL) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Unexpected length.");
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, errno, "Logcat read failure");
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
if (log_msg.id() > LOG_ID_MAX) {
|
2019-12-06 19:25:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Unexpected log id (%d) over LOG_ID_MAX (%d).", log_msg.id(),
|
|
|
|
LOG_ID_MAX);
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 15:05:24 +01:00
|
|
|
PrintDividers(log_msg.id(), printDividers);
|
|
|
|
|
2019-10-25 02:35:26 +02:00
|
|
|
if (print_binary_) {
|
|
|
|
TEMP_FAILURE_RETRY(write(output_fd_.get(), &log_msg, log_msg.len()));
|
2013-11-22 19:55:48 +01:00
|
|
|
} else {
|
2019-10-29 15:05:24 +01:00
|
|
|
ProcessBuffer(&log_msg);
|
2013-11-22 19:55:48 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-25 02:35:26 +02:00
|
|
|
return EXIT_SUCCESS;
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 21:51:03 +01:00
|
|
|
int main(int argc, char** argv) {
|
2019-10-25 02:35:26 +02:00
|
|
|
Logcat logcat;
|
|
|
|
return logcat.Run(argc, argv);
|
2017-02-10 22:09:07 +01:00
|
|
|
}
|