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