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 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 };
47
48 // These callbacks are called in a signal handler, and thus must be async signal safe.
49 // If null, the callbacks will not be called.
50 typedef struct {
51 debugger_process_info (*get_process_info)();
52 void (*post_dump)();
53 } debuggerd_callbacks_t;
54
55 void debuggerd_init(debuggerd_callbacks_t* callbacks);
56
57 // DEBUGGER_ACTION_DUMP_TOMBSTONE and DEBUGGER_ACTION_DUMP_BACKTRACE are both
58 // triggered via BIONIC_SIGNAL_DEBUGGER. The debugger_action_t is sent via si_value
59 // using sigqueue(2) or equivalent. If no si_value is specified (e.g. if the
60 // signal is sent by kill(2)), the default behavior is to print the backtrace
61 // to the log.
62 #define DEBUGGER_SIGNAL BIONIC_SIGNAL_DEBUGGER
63
debuggerd_register_handlers(struct sigaction * action)64 static void __attribute__((__unused__)) debuggerd_register_handlers(struct sigaction* action) {
65 char value[PROP_VALUE_MAX] = "";
66 bool enabled =
67 !(__system_property_get("ro.debuggable", value) > 0 && !strcmp(value, "1") &&
68 __system_property_get("debug.debuggerd.disable", value) > 0 && !strcmp(value, "1"));
69 if (enabled) {
70 sigaction(SIGABRT, action, nullptr);
71 sigaction(SIGBUS, action, nullptr);
72 sigaction(SIGFPE, action, nullptr);
73 sigaction(SIGILL, action, nullptr);
74 sigaction(SIGSEGV, action, nullptr);
75 sigaction(SIGSTKFLT, action, nullptr);
76 sigaction(SIGSYS, action, nullptr);
77 sigaction(SIGTRAP, action, nullptr);
78 }
79
80 sigaction(BIONIC_SIGNAL_DEBUGGER, action, nullptr);
81 }
82
83 __END_DECLS
84