blob: ca8bf6cb086ea8714a46ac151301402287fc9fa4 [file]
/*
* Copyright (C) 2020 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 <err.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
#include <format>
#include <functional>
#include <print>
#include <string>
#include <vector>
#include <android-base/unique_fd.h>
#include <libdebuggerd/tombstone_proto_to_text.h>
#include "tombstone.pb.h"
#include "tombstone_symbolize.h"
using android::base::unique_fd;
enum {
OPT_DEBUG_DIR = 1000,
OPT_DISPLAY_SP,
OPT_HEADER,
OPT_SIGNAL_INFO,
OPT_REGISTERS_INFO,
OPT_BACKTRACE,
OPT_REGISTERS_STACK,
OPT_MEMORY_MAP,
OPT_LOGCAT,
};
[[noreturn]] void usage(bool error) {
std::println(stderr, "usage: pbtombstone [OPTION] TOMBSTONE.PB");
std::println(stderr, "Convert a protobuf tombstone to text.");
std::println(stderr, "Arguments:");
std::println(stderr, " -h, --help print this message");
std::println(stderr, " --display-sp print stack pointer in backtraces");
std::println(stderr, " --debug-file-directory PATH specify the path to a symbols directory");
std::println(stderr, " --header prints just the header");
std::println(stderr, " --signal_info prints just signal info");
std::println(stderr, " --registers prints just registers values");
std::println(stderr, " --backtrace prints just backtrace");
std::println(stderr, " --registers_stack prints just registers stack");
std::println(stderr, " --memory_map prints just memory maps");
std::println(stderr, " --logcat prints just logcat section");
exit(error);
}
int main(int argc, char* argv[]) {
std::vector<std::string> debug_file_directories;
static struct option long_options[] = {
{"debug-file-directory", required_argument, 0, OPT_DEBUG_DIR},
{"display-sp", no_argument, 0, OPT_DISPLAY_SP},
{"header", no_argument, 0, OPT_HEADER},
{"signal_info", no_argument, 0, OPT_SIGNAL_INFO},
{"registers", no_argument, 0, OPT_REGISTERS_INFO},
{"backtrace", no_argument, 0, OPT_BACKTRACE},
{"registers_stack", no_argument, 0, OPT_REGISTERS_STACK},
{"memory_map", no_argument, 0, OPT_MEMORY_MAP},
{"logcat", no_argument, 0, OPT_LOGCAT},
{"help", no_argument, 0, 'h'},
{},
};
int c;
int option_index = 0;
bool display_sp = false;
// Bitmask for display sections. These options are mutually exclusive
// in the sense that if any of these are specified, only the specified
// sections are printed.
enum DisplaySections : unsigned int {
kDisplayNone = 0,
kDisplayHeader = 1 << 0,
kDisplaySignalInfo = 1 << 1,
kDisplayRegisters = 1 << 2,
kDisplayBacktrace = 1 << 3,
kDisplayRegistersStack = 1 << 4,
kDisplayMemoryMap = 1 << 5,
kDisplayLogcat = 1 << 6,
};
unsigned int display_sections = kDisplayNone;
while ((c = getopt_long(argc, argv, "h", long_options, &option_index)) != -1) {
switch (c) {
case OPT_DEBUG_DIR:
debug_file_directories.push_back(optarg);
break;
case OPT_DISPLAY_SP:
display_sp = true;
break;
case OPT_HEADER:
display_sections |= kDisplayHeader;
break;
case OPT_SIGNAL_INFO:
display_sections |= kDisplaySignalInfo;
break;
case OPT_REGISTERS_INFO:
display_sections |= kDisplayRegisters;
break;
case OPT_BACKTRACE:
display_sections |= kDisplayBacktrace;
break;
case OPT_REGISTERS_STACK:
display_sections |= kDisplayRegistersStack;
break;
case OPT_MEMORY_MAP:
display_sections |= kDisplayMemoryMap;
break;
case OPT_LOGCAT:
display_sections |= kDisplayLogcat;
break;
case 'h':
usage(false);
break;
}
}
if (optind != argc - 1) {
usage(true);
}
unique_fd fd(open(argv[optind], O_RDONLY | O_CLOEXEC));
if (fd == -1) {
err(1, "failed to open tombstone '%s'", argv[1]);
}
Tombstone tombstone;
if (!tombstone.ParseFromFileDescriptor(fd.get())) {
err(1, "failed to parse tombstone");
}
Symbolizer sym;
sym.Start(debug_file_directories);
std::function<std::string(const BacktraceFrame& frame)> symbolize_func =
[](const BacktraceFrame&) { return std::string(); };
if (!debug_file_directories.empty()) {
symbolize_func = [&sym](const BacktraceFrame& frame) {
return symbolize_backtrace_frame(frame, sym);
};
}
if (display_sections == kDisplayNone) {
std::print("{}", tombstone_proto_to_text::get_text(tombstone, symbolize_func, display_sp));
} else {
if (display_sections & kDisplayHeader) {
std::print("{}", tombstone_proto_to_text::get_header(tombstone));
}
if (display_sections & kDisplaySignalInfo) {
std::print("{}", tombstone_proto_to_text::get_signal_info(tombstone));
}
if (display_sections & kDisplayRegisters) {
std::print("{}", tombstone_proto_to_text::get_registers(
tombstone.arch(), tombstone.threads().at(tombstone.tid())));
}
if (display_sections & kDisplayBacktrace) {
std::print("{}",
tombstone_proto_to_text::get_backtrace(tombstone, symbolize_func, display_sp));
}
if (display_sections & kDisplayRegistersStack) {
std::print("{}", tombstone_proto_to_text::get_registers_stack(tombstone));
}
if (display_sections & kDisplayMemoryMap) {
std::print("{}", tombstone_proto_to_text::get_memory_maps(tombstone));
}
if (display_sections & kDisplayLogcat) {
std::print("{}", tombstone_proto_to_text::get_logs(tombstone));
}
}
return 0;
}