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 <bionic/reserved_signals.h>
20 #include <signal.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <sys/cdefs.h>
24 #include <sys/system_properties.h>
25 #include <sys/types.h>
26 
27 __BEGIN_DECLS
28 
29 // Forward declare these classes so not everyone has to include GWP-ASan
30 // headers.
31 namespace gwp_asan {
32 struct AllocatorState;
33 struct AllocationMetadata;
34 };  // namespace gwp_asan
35 
36 // When updating this data structure, CrashInfoDataDynamic and the code in
37 // ReadCrashInfo() must also be updated.
38 struct __attribute__((packed)) debugger_process_info {
39   void* abort_msg;
40   void* fdsan_table;
41   const gwp_asan::AllocatorState* gwp_asan_state;
42   const gwp_asan::AllocationMetadata* gwp_asan_metadata;
43   const char* scudo_stack_depot;
44   const char* scudo_region_info;
45   const char* scudo_ring_buffer;
46   size_t scudo_ring_buffer_size;
47   bool recoverable_gwp_asan_crash;
48 };
49 
50 // GWP-ASan calbacks to support the recoverable mode. Separate from the
51 // debuggerd_callbacks_t because these values aren't available at debuggerd_init
52 // time, and have to be synthesized on request.
53 typedef struct {
54   bool (*debuggerd_needs_gwp_asan_recovery)(void* fault_addr);
55   void (*debuggerd_gwp_asan_pre_crash_report)(void* fault_addr);
56   void (*debuggerd_gwp_asan_post_crash_report)(void* fault_addr);
57 } gwp_asan_callbacks_t;
58 
59 // These callbacks are called in a signal handler, and thus must be async signal safe.
60 // If null, the callbacks will not be called.
61 typedef struct {
62   debugger_process_info (*get_process_info)();
63   gwp_asan_callbacks_t (*get_gwp_asan_callbacks)();
64   void (*post_dump)();
65 } debuggerd_callbacks_t;
66 
67 void debuggerd_init(debuggerd_callbacks_t* callbacks);
68 bool debuggerd_handle_signal(int signal_number, siginfo_t* info, void* context);
69 
70 // DEBUGGER_ACTION_DUMP_TOMBSTONE and DEBUGGER_ACTION_DUMP_BACKTRACE are both
71 // triggered via BIONIC_SIGNAL_DEBUGGER. The debugger_action_t is sent via si_value
72 // using sigqueue(2) or equivalent. If no si_value is specified (e.g. if the
73 // signal is sent by kill(2)), the default behavior is to print the backtrace
74 // to the log.
75 #define DEBUGGER_SIGNAL BIONIC_SIGNAL_DEBUGGER
76 
debuggerd_register_handlers(struct sigaction * action)77 static void __attribute__((__unused__)) debuggerd_register_handlers(struct sigaction* action) {
78   bool enabled = true;
79 #if ANDROID_DEBUGGABLE
80   char value[PROP_VALUE_MAX] = "";
81   enabled = !(__system_property_get("debug.debuggerd.disable", value) > 0 && !strcmp(value, "1"));
82 #endif
83   if (enabled) {
84     sigaction(SIGABRT, action, nullptr);
85     sigaction(SIGBUS, action, nullptr);
86     sigaction(SIGFPE, action, nullptr);
87     sigaction(SIGILL, action, nullptr);
88     sigaction(SIGSEGV, action, nullptr);
89     sigaction(SIGSTKFLT, action, nullptr);
90     sigaction(SIGSYS, action, nullptr);
91     sigaction(SIGTRAP, action, nullptr);
92   }
93 
94   sigaction(BIONIC_SIGNAL_DEBUGGER, action, nullptr);
95 }
96 
97 __END_DECLS
98