1 /*
2  * Copyright 2008, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <inttypes.h>
20 #include <signal.h>
21 #include <stdarg.h>
22 #include <stdbool.h>
23 #include <sys/types.h>
24 
25 #include <string>
26 
27 #include <android-base/macros.h>
28 
29 struct log_t {
30   // Tombstone file descriptor.
31   int tfd;
32   // Data to be sent to the Activity Manager.
33   std::string* amfd_data;
34   // The tid of the thread that crashed.
35   pid_t crashed_tid;
36   // The tid of the thread we are currently working with.
37   pid_t current_tid;
38   // logd daemon crash, can block asking for logcat data, allow suppression.
39   bool should_retrieve_logcat;
40 
log_tlog_t41   log_t()
42       : tfd(-1),
43         amfd_data(nullptr),
44         crashed_tid(-1),
45         current_tid(-1),
46         should_retrieve_logcat(true) {}
47 };
48 
49 // List of types of logs to simplify the logging decision in _LOG
50 enum logtype {
51   HEADER,
52   THREAD,
53   REGISTERS,
54   BACKTRACE,
55   MAPS,
56   MEMORY,
57   STACK,
58   LOGS,
59   OPEN_FILES
60 };
61 
62 #if defined(__LP64__)
63 #define PRIPTR "016" PRIx64
64 typedef uint64_t word_t;
65 #else
66 #define PRIPTR "08" PRIx64
67 typedef uint32_t word_t;
68 #endif
69 
70 // Log information onto the tombstone.
71 void _LOG(log_t* log, logtype ltype, const char* fmt, ...) __attribute__((format(printf, 3, 4)));
72 void _VLOG(log_t* log, logtype ltype, const char* fmt, va_list ap);
73 
74 namespace unwindstack {
75 class AndroidUnwinder;
76 class Memory;
77 struct AndroidUnwinderData;
78 }
79 
80 void log_backtrace(log_t* log, unwindstack::AndroidUnwinder* unwinder,
81                    unwindstack::AndroidUnwinderData& data, const char* prefix);
82 
83 ssize_t dump_memory(void* out, size_t len, uint8_t* tags, size_t tags_len, uint64_t* addr,
84                     unwindstack::Memory* memory);
85 void dump_memory(log_t* log, unwindstack::Memory* backtrace, uint64_t addr, const std::string&);
86 
87 void drop_capabilities();
88 
89 bool signal_has_sender(const siginfo_t*, pid_t caller_pid);
90 bool signal_has_si_addr(const siginfo_t*);
91 void get_signal_sender(char* buf, size_t n, const siginfo_t*);
92 const char* get_signame(const siginfo_t*);
93 const char* get_sigcode(const siginfo_t*);
94 
95 // Number of bytes per MTE granule.
96 constexpr size_t kTagGranuleSize = 16;
97 
98 // Number of rows and columns to display in an MTE tag dump.
99 constexpr size_t kNumTagColumns = 16;
100 constexpr size_t kNumTagRows = 16;
101