1 /*
2  * Copyright 2016, 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 <signal.h>
20 #include <stdint.h>
21 #include <sys/ucontext.h>
22 #include <unistd.h>
23 
24 #include "dump_type.h"
25 
26 // Sockets in the ANDROID_SOCKET_NAMESPACE_RESERVED namespace.
27 // Both sockets are SOCK_SEQPACKET sockets, so no explicit length field is needed.
28 constexpr char kTombstonedCrashSocketName[] = "tombstoned_crash";
29 constexpr char kTombstonedJavaTraceSocketName[] = "tombstoned_java_trace";
30 constexpr char kTombstonedInterceptSocketName[] = "tombstoned_intercept";
31 
32 enum class CrashPacketType : uint8_t {
33   // Initial request from crash_dump.
34   kDumpRequest = 0,
35 
36   // Notification of a completed crash dump.
37   // Sent after a dump is completed and the process has been untraced, but
38   // before it has been resumed with SIGCONT.
39   kCompletedDump,
40 
41   // Responses to kRequest.
42   // kPerformDump sends along an output fd via cmsg(3).
43   kPerformDump = 128,
44   kAbortDump,
45 };
46 
47 struct DumpRequest {
48   DebuggerdDumpType dump_type;
49   int32_t pid;
50 };
51 
52 // The full packet must always be written, regardless of whether the union is used.
53 struct TombstonedCrashPacket {
54   CrashPacketType packet_type;
55   union {
56     DumpRequest dump_request;
57   } packet;
58 };
59 
60 // Comes with a file descriptor via SCM_RIGHTS.
61 // This packet should be sent before an actual dump happens.
62 struct InterceptRequest {
63   DebuggerdDumpType dump_type;
64   int32_t pid;
65 };
66 
67 enum class InterceptStatus : uint8_t {
68   // Returned when an intercept of a different type has already been
69   // registered (and is active) for a given PID.
70   kFailedAlreadyRegistered,
71   // Returned in all other failure cases.
72   kFailed,
73   kStarted,
74   kRegistered,
75 };
76 
77 // Sent either immediately upon failure, or when the intercept has been used.
78 struct InterceptResponse {
79   InterceptStatus status;
80   char error_message[127];  // always null-terminated
81 };
82 
83 // Sent from handler to crash_dump via pipe.
84 struct __attribute__((__packed__)) CrashInfoHeader {
85   uint32_t version;
86 };
87 
88 struct __attribute__((__packed__)) CrashInfoDataStatic {
89   siginfo_t siginfo;
90   ucontext_t ucontext;
91   uintptr_t abort_msg_address;
92 };
93 
94 struct __attribute__((__packed__)) CrashInfoDataDynamic : public CrashInfoDataStatic {
95   uintptr_t fdsan_table_address;
96   uintptr_t gwp_asan_state;
97   uintptr_t gwp_asan_metadata;
98   uintptr_t scudo_stack_depot;
99   uintptr_t scudo_region_info;
100   uintptr_t scudo_ring_buffer;
101 };
102 
103 struct __attribute__((__packed__)) CrashInfo {
104   CrashInfoHeader header;
105   union {
106     CrashInfoDataStatic s;
107     CrashInfoDataDynamic d;
108   } data;
109 };
110