blob: 26442e8ca730d3b682f073fef70fb8a53c6bbbbc [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
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
44BacktraceData::BacktraceData(const Config& config, size_t* offset) {
Christopher Ferris7993b802016-01-28 18:35:05 -080045 size_t hdr_len = sizeof(BacktraceHeader) + sizeof(uintptr_t) * config.backtrace_frames;
Christopher Ferris63860cb2015-11-16 17:30:32 -080046 alloc_offset_ = *offset;
Christopher Ferris39b952c2016-02-11 15:51:31 -080047 *offset += BIONIC_ALIGN(hdr_len, MINIMUM_ALIGNMENT_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -080048}
49
50static BacktraceData* g_backtrace_data = nullptr;
51
52static void EnableToggle(int, siginfo_t*, void*) {
53 if (g_backtrace_data->enabled()) {
54 g_backtrace_data->set_enabled(false);
Shibin George297034e2016-09-06 21:58:22 +053055 info_log("%s:%d backtrace disabled.", getprogname(), getpid());
Christopher Ferris63860cb2015-11-16 17:30:32 -080056 } else {
57 g_backtrace_data->set_enabled(true);
Shibin George297034e2016-09-06 21:58:22 +053058 info_log("%s:%d backtrace enabled.", getprogname(), getpid());
Christopher Ferris63860cb2015-11-16 17:30:32 -080059 }
60}
61
62bool BacktraceData::Initialize(const Config& config) {
63 enabled_ = config.backtrace_enabled;
64 if (config.backtrace_enable_on_signal) {
65 g_backtrace_data = this;
66
67 struct sigaction enable_act;
68 memset(&enable_act, 0, sizeof(enable_act));
69
70 enable_act.sa_sigaction = EnableToggle;
71 enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
72 sigemptyset(&enable_act.sa_mask);
73 if (sigaction(config.backtrace_signal, &enable_act, nullptr) != 0) {
74 error_log("Unable to set up backtrace signal enable function: %s", strerror(errno));
75 return false;
76 }
77 info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(),
78 config.backtrace_signal, getpid());
79 }
80 return true;
81}