Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <signal.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #include <private/bionic_macros.h> |
| 37 | |
| 38 | #include "BacktraceData.h" |
| 39 | #include "Config.h" |
| 40 | #include "DebugData.h" |
| 41 | #include "debug_log.h" |
| 42 | #include "malloc_debug.h" |
| 43 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 44 | static void ToggleBacktraceEnable(int, siginfo_t*, void*) { |
| 45 | g_debug->backtrace->ToggleBacktraceEnabled(); |
| 46 | } |
| 47 | |
| 48 | static void EnableDump(int, siginfo_t*, void*) { |
| 49 | g_debug->backtrace->EnableDumping(); |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | BacktraceData::BacktraceData(DebugData* debug_data, const Config& config, size_t* offset) |
| 53 | : OptionData(debug_data) { |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 54 | size_t hdr_len = sizeof(BacktraceHeader) + sizeof(uintptr_t) * config.backtrace_frames(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 55 | alloc_offset_ = *offset; |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 56 | *offset += __BIONIC_ALIGN(hdr_len, MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 59 | bool BacktraceData::Initialize(const Config& config) { |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 60 | enabled_ = config.backtrace_enabled(); |
| 61 | if (config.backtrace_enable_on_signal()) { |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame^] | 62 | struct sigaction64 enable_act = {}; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 63 | enable_act.sa_sigaction = ToggleBacktraceEnable; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 64 | enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame^] | 65 | if (sigaction64(config.backtrace_signal(), &enable_act, nullptr) != 0) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 66 | error_log("Unable to set up backtrace signal enable function: %s", strerror(errno)); |
| 67 | return false; |
| 68 | } |
| 69 | info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(), |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 70 | config.backtrace_signal(), getpid()); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 71 | } |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 72 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame^] | 73 | struct sigaction64 act = {}; |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 74 | act.sa_sigaction = EnableDump; |
| 75 | act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame^] | 76 | if (sigaction64(config.backtrace_dump_signal(), &act, nullptr) != 0) { |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 77 | error_log("Unable to set up backtrace dump signal function: %s", strerror(errno)); |
| 78 | return false; |
| 79 | } |
| 80 | info_log("%s: Run: 'kill -%d %d' to dump the backtrace.", getprogname(), |
| 81 | config.backtrace_dump_signal(), getpid()); |
| 82 | |
| 83 | dump_ = false; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 84 | return true; |
| 85 | } |