From 39a1730a82a6517f2c4e2ee34edb0ac83bbbb58a Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 7 Mar 2024 17:50:10 -0800 Subject: [PATCH] Make pbtombstone a host tool. This is preparation for the next patch, which adds host-side symbolization capabilities to pbtombstone. Bug: 328531087 Change-Id: Id5813ae6b121af784643b1ed76084e49fdca118b --- debuggerd/Android.bp | 16 +-- debuggerd/debuggerd_test.cpp | 3 +- .../include/libdebuggerd/tombstone.h | 4 - .../libdebuggerd/tombstone_proto_to_text.h | 26 +++++ .../include/libdebuggerd/utility.h | 7 -- .../include/libdebuggerd/utility_host.h | 31 ++++++ debuggerd/libdebuggerd/scudo.cpp | 1 + .../test/tombstone_proto_to_text_test.cpp | 1 + debuggerd/libdebuggerd/tombstone.cpp | 1 + debuggerd/libdebuggerd/tombstone_proto.cpp | 1 + .../libdebuggerd/tombstone_proto_to_text.cpp | 39 +++---- debuggerd/libdebuggerd/utility.cpp | 1 + debuggerd/libdebuggerd/utility_host.cpp | 101 ++++++++++++++++++ debuggerd/pbtombstone.cpp | 2 +- debuggerd/proto/Android.bp | 1 + 15 files changed, 186 insertions(+), 49 deletions(-) create mode 100644 debuggerd/libdebuggerd/include/libdebuggerd/tombstone_proto_to_text.h create mode 100644 debuggerd/libdebuggerd/include/libdebuggerd/utility_host.h create mode 100644 debuggerd/libdebuggerd/utility_host.cpp diff --git a/debuggerd/Android.bp b/debuggerd/Android.bp index c365cac52..d4dc9a340 100644 --- a/debuggerd/Android.bp +++ b/debuggerd/Android.bp @@ -200,22 +200,18 @@ cc_library { ramdisk_available: true, recovery_available: true, vendor_ramdisk_available: true, + host_supported: true, local_include_dirs: ["libdebuggerd/include"], export_include_dirs: ["libdebuggerd/include"], srcs: [ "libdebuggerd/tombstone_proto_to_text.cpp", - ], - - header_libs: [ - "bionic_libc_platform_headers", + "libdebuggerd/utility_host.cpp", ], static_libs: [ "libbase", - "liblog_for_runtime_apex", - "libunwindstack", ], whole_static_libs: [ @@ -223,6 +219,10 @@ cc_library { "libprotobuf-cpp-lite", ], + shared_libs: [ + "liblog", + ], + apex_available: [ "//apex_available:platform", "com.android.runtime", @@ -331,15 +331,15 @@ cc_library_static { cc_binary { name: "pbtombstone", + host_supported: true, defaults: ["debuggerd_defaults"], srcs: ["pbtombstone.cpp"], static_libs: [ "libbase", - "libdebuggerd", + "libdebuggerd_tombstone_proto_to_text", "liblog", "libprotobuf-cpp-lite", "libtombstone_proto", - "libunwindstack", ], } diff --git a/debuggerd/debuggerd_test.cpp b/debuggerd/debuggerd_test.cpp index e33cea5c8..13c8d70b5 100644 --- a/debuggerd/debuggerd_test.cpp +++ b/debuggerd/debuggerd_test.cpp @@ -70,6 +70,7 @@ #include "crash_test.h" #include "debuggerd/handler.h" #include "gtest/gtest.h" +#include "libdebuggerd/utility_host.h" #include "protocol.h" #include "tombstoned/tombstoned.h" #include "util.h" @@ -741,8 +742,6 @@ TEST_F(CrasherTest, mte_multiple_causes) { } #if defined(__aarch64__) -constexpr size_t kTagGranuleSize = 16; - static uintptr_t CreateTagMapping() { // Some of the MTE tag dump tests assert that there is an inaccessible page to the left and right // of the PROT_MTE page, so map three pages and set the two guard pages to PROT_NONE. diff --git a/debuggerd/libdebuggerd/include/libdebuggerd/tombstone.h b/debuggerd/libdebuggerd/include/libdebuggerd/tombstone.h index 074b0957a..39989c3a3 100644 --- a/debuggerd/libdebuggerd/include/libdebuggerd/tombstone.h +++ b/debuggerd/libdebuggerd/include/libdebuggerd/tombstone.h @@ -67,10 +67,6 @@ void engrave_tombstone_proto(Tombstone* tombstone, unwindstack::AndroidUnwinder* const Architecture* guest_arch, unwindstack::AndroidUnwinder* guest_unwinder); -bool tombstone_proto_to_text( - const Tombstone& tombstone, - std::function callback); - void fill_in_backtrace_frame(BacktraceFrame* f, const unwindstack::FrameData& frame); void set_human_readable_cause(Cause* cause, uint64_t fault_addr); #if defined(__aarch64__) diff --git a/debuggerd/libdebuggerd/include/libdebuggerd/tombstone_proto_to_text.h b/debuggerd/libdebuggerd/include/libdebuggerd/tombstone_proto_to_text.h new file mode 100644 index 000000000..515a15f0a --- /dev/null +++ b/debuggerd/libdebuggerd/include/libdebuggerd/tombstone_proto_to_text.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2024 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 + +#include +#include + +class Tombstone; + +bool tombstone_proto_to_text( + const Tombstone& tombstone, + std::function callback); diff --git a/debuggerd/libdebuggerd/include/libdebuggerd/utility.h b/debuggerd/libdebuggerd/include/libdebuggerd/utility.h index 26c2cd44a..b86c13d08 100644 --- a/debuggerd/libdebuggerd/include/libdebuggerd/utility.h +++ b/debuggerd/libdebuggerd/include/libdebuggerd/utility.h @@ -91,10 +91,3 @@ bool signal_has_si_addr(const siginfo_t*); void get_signal_sender(char* buf, size_t n, const siginfo_t*); const char* get_signame(const siginfo_t*); const char* get_sigcode(const siginfo_t*); - -// Number of bytes per MTE granule. -constexpr size_t kTagGranuleSize = 16; - -// Number of rows and columns to display in an MTE tag dump. -constexpr size_t kNumTagColumns = 16; -constexpr size_t kNumTagRows = 16; diff --git a/debuggerd/libdebuggerd/include/libdebuggerd/utility_host.h b/debuggerd/libdebuggerd/include/libdebuggerd/utility_host.h new file mode 100644 index 000000000..43fb8bdc1 --- /dev/null +++ b/debuggerd/libdebuggerd/include/libdebuggerd/utility_host.h @@ -0,0 +1,31 @@ +/* + * Copyright 2024, 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 + +#include + +#include + +std::string describe_tagged_addr_ctrl(long ctrl); +std::string describe_pac_enabled_keys(long keys); + +// Number of bytes per MTE granule. +constexpr size_t kTagGranuleSize = 16; + +// Number of rows and columns to display in an MTE tag dump. +constexpr size_t kNumTagColumns = 16; +constexpr size_t kNumTagRows = 16; diff --git a/debuggerd/libdebuggerd/scudo.cpp b/debuggerd/libdebuggerd/scudo.cpp index 4ee87c841..71d5fcfa7 100644 --- a/debuggerd/libdebuggerd/scudo.cpp +++ b/debuggerd/libdebuggerd/scudo.cpp @@ -18,6 +18,7 @@ #include "libdebuggerd/scudo.h" #include "libdebuggerd/tombstone.h" +#include "libdebuggerd/utility_host.h" #include "unwindstack/AndroidUnwinder.h" #include "unwindstack/Memory.h" diff --git a/debuggerd/libdebuggerd/test/tombstone_proto_to_text_test.cpp b/debuggerd/libdebuggerd/test/tombstone_proto_to_text_test.cpp index 4fd264301..3fdb71d48 100644 --- a/debuggerd/libdebuggerd/test/tombstone_proto_to_text_test.cpp +++ b/debuggerd/libdebuggerd/test/tombstone_proto_to_text_test.cpp @@ -22,6 +22,7 @@ #include #include "libdebuggerd/tombstone.h" +#include "libdebuggerd/tombstone_proto_to_text.h" #include "tombstone.pb.h" using CallbackType = std::function; diff --git a/debuggerd/libdebuggerd/tombstone.cpp b/debuggerd/libdebuggerd/tombstone.cpp index 0ce55738a..d483b9889 100644 --- a/debuggerd/libdebuggerd/tombstone.cpp +++ b/debuggerd/libdebuggerd/tombstone.cpp @@ -17,6 +17,7 @@ #define LOG_TAG "DEBUG" #include "libdebuggerd/tombstone.h" +#include "libdebuggerd/tombstone_proto_to_text.h" #include #include diff --git a/debuggerd/libdebuggerd/tombstone_proto.cpp b/debuggerd/libdebuggerd/tombstone_proto.cpp index ed4fd5369..d59358c65 100644 --- a/debuggerd/libdebuggerd/tombstone_proto.cpp +++ b/debuggerd/libdebuggerd/tombstone_proto.cpp @@ -69,6 +69,7 @@ #include "libdebuggerd/open_files_list.h" #include "libdebuggerd/utility.h" +#include "libdebuggerd/utility_host.h" #include "util.h" #include "tombstone.pb.h" diff --git a/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp b/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp index c3f94700f..611e237b4 100644 --- a/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp +++ b/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp @@ -14,7 +14,8 @@ * limitations under the License. */ -#include +#include +#include #include @@ -30,8 +31,6 @@ #include #include #include -#include -#include #include "tombstone.pb.h" @@ -57,28 +56,6 @@ static std::string describe_end(long value, std::string& desc) { return desc.empty() ? "" : " (" + desc.substr(2) + ")"; } -static std::string describe_tagged_addr_ctrl(long value) { - std::string desc; - DESCRIBE_FLAG(PR_TAGGED_ADDR_ENABLE); - DESCRIBE_FLAG(PR_MTE_TCF_SYNC); - DESCRIBE_FLAG(PR_MTE_TCF_ASYNC); - if (value & PR_MTE_TAG_MASK) { - desc += StringPrintf(", mask 0x%04lx", (value & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT); - value &= ~PR_MTE_TAG_MASK; - } - return describe_end(value, desc); -} - -static std::string describe_pac_enabled_keys(long value) { - std::string desc; - DESCRIBE_FLAG(PR_PAC_APIAKEY); - DESCRIBE_FLAG(PR_PAC_APIBKEY); - DESCRIBE_FLAG(PR_PAC_APDAKEY); - DESCRIBE_FLAG(PR_PAC_APDBKEY); - DESCRIBE_FLAG(PR_PAC_APGAKEY); - return describe_end(value, desc); -} - static const char* abi_string(const Architecture& arch) { switch (arch) { case Architecture::ARM32: @@ -113,6 +90,13 @@ static int pointer_width(const Tombstone& tombstone) { } } +static uint64_t untag_address(Architecture arch, uint64_t addr) { + if (arch == Architecture::ARM64) { + return addr & ((1ULL << 56) - 1); + } + return addr; +} + static void print_thread_header(CallbackType callback, const Tombstone& tombstone, const Thread& thread, bool should_log) { const char* process_name = ""; @@ -321,7 +305,8 @@ static void print_tag_dump(CallbackType callback, const Tombstone& tombstone) { size_t tag_index = 0; size_t num_tags = tags.length(); - uintptr_t fault_granule = untag_address(signal.fault_address()) & ~(kTagGranuleSize - 1); + uintptr_t fault_granule = + untag_address(tombstone.arch(), signal.fault_address()) & ~(kTagGranuleSize - 1); for (size_t row = 0; tag_index < num_tags; ++row) { uintptr_t row_addr = (memory_dump.begin_address() + row * kNumTagColumns * kTagGranuleSize) & kRowStartMask; @@ -369,7 +354,7 @@ static void print_memory_maps(CallbackType callback, const Tombstone& tombstone) const Signal& signal_info = tombstone.signal_info(); bool has_fault_address = signal_info.has_fault_address(); - uint64_t fault_address = untag_address(signal_info.fault_address()); + uint64_t fault_address = untag_address(tombstone.arch(), signal_info.fault_address()); bool preamble_printed = false; bool printed_fault_address_marker = false; for (const auto& map : tombstone.memory_mappings()) { diff --git a/debuggerd/libdebuggerd/utility.cpp b/debuggerd/libdebuggerd/utility.cpp index 742ac7c27..b5a93b7db 100644 --- a/debuggerd/libdebuggerd/utility.cpp +++ b/debuggerd/libdebuggerd/utility.cpp @@ -17,6 +17,7 @@ #define LOG_TAG "DEBUG" #include "libdebuggerd/utility.h" +#include "libdebuggerd/utility_host.h" #include #include diff --git a/debuggerd/libdebuggerd/utility_host.cpp b/debuggerd/libdebuggerd/utility_host.cpp new file mode 100644 index 000000000..72a560cfe --- /dev/null +++ b/debuggerd/libdebuggerd/utility_host.cpp @@ -0,0 +1,101 @@ +/* + * Copyright 2024, 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 "libdebuggerd/utility_host.h" + +#include + +#include + +#include + +using android::base::StringPrintf; + +#ifndef PR_MTE_TAG_SHIFT +#define PR_MTE_TAG_SHIFT 3 +#endif + +#ifndef PR_MTE_TAG_MASK +#define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT) +#endif + +#ifndef PR_MTE_TCF_ASYNC +#define PR_MTE_TCF_ASYNC (1UL << 2) +#endif + +#ifndef PR_MTE_TCF_SYNC +#define PR_MTE_TCF_SYNC (1UL << 1) +#endif + +#ifndef PR_PAC_APIAKEY +#define PR_PAC_APIAKEY (1UL << 0) +#endif + +#ifndef PR_PAC_APIBKEY +#define PR_PAC_APIBKEY (1UL << 1) +#endif + +#ifndef PR_PAC_APDAKEY +#define PR_PAC_APDAKEY (1UL << 2) +#endif + +#ifndef PR_PAC_APDBKEY +#define PR_PAC_APDBKEY (1UL << 3) +#endif + +#ifndef PR_PAC_APGAKEY +#define PR_PAC_APGAKEY (1UL << 4) +#endif + +#ifndef PR_TAGGED_ADDR_ENABLE +#define PR_TAGGED_ADDR_ENABLE (1UL << 0) +#endif + +#define DESCRIBE_FLAG(flag) \ + if (value & flag) { \ + desc += ", "; \ + desc += #flag; \ + value &= ~flag; \ + } + +static std::string describe_end(long value, std::string& desc) { + if (value) { + desc += StringPrintf(", unknown 0x%lx", value); + } + return desc.empty() ? "" : " (" + desc.substr(2) + ")"; +} + +std::string describe_tagged_addr_ctrl(long value) { + std::string desc; + DESCRIBE_FLAG(PR_TAGGED_ADDR_ENABLE); + DESCRIBE_FLAG(PR_MTE_TCF_SYNC); + DESCRIBE_FLAG(PR_MTE_TCF_ASYNC); + if (value & PR_MTE_TAG_MASK) { + desc += StringPrintf(", mask 0x%04lx", (value & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT); + value &= ~PR_MTE_TAG_MASK; + } + return describe_end(value, desc); +} + +std::string describe_pac_enabled_keys(long value) { + std::string desc; + DESCRIBE_FLAG(PR_PAC_APIAKEY); + DESCRIBE_FLAG(PR_PAC_APIBKEY); + DESCRIBE_FLAG(PR_PAC_APDAKEY); + DESCRIBE_FLAG(PR_PAC_APDBKEY); + DESCRIBE_FLAG(PR_PAC_APGAKEY); + return describe_end(value, desc); +} diff --git a/debuggerd/pbtombstone.cpp b/debuggerd/pbtombstone.cpp index 7527e31e1..dcb7c6c21 100644 --- a/debuggerd/pbtombstone.cpp +++ b/debuggerd/pbtombstone.cpp @@ -20,7 +20,7 @@ #include #include -#include +#include #include "tombstone.pb.h" diff --git a/debuggerd/proto/Android.bp b/debuggerd/proto/Android.bp index 7b9e780cc..70deb3cdd 100644 --- a/debuggerd/proto/Android.bp +++ b/debuggerd/proto/Android.bp @@ -38,6 +38,7 @@ cc_library_static { ramdisk_available: true, recovery_available: true, vendor_ramdisk_available: true, + host_supported: true, } java_library_static {