blob: dd87ed840e0e479bc5d9ef70839d178567ccdad4 [file] [log] [blame]
Ryan Savitski175c8862020-01-02 19:54:57 +00001/*
2 * Copyright (C) 2020 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#if defined(LIBC_STATIC)
30#error This file should not be compiled for static targets.
31#endif
32
33#include <signal.h>
34
35#include <async_safe/log.h>
36#include <platform/bionic/malloc.h>
37#include <platform/bionic/reserved_signals.h>
38
39#include "malloc_heapprofd.h"
40
41// This file defines the handler for the reserved signal sent by the Android
42// platform's profilers. The accompanying signal value discriminates between
43// specific requestors:
44// 0: heapprofd heap profiler.
45static constexpr int kHeapprofdSignalValue = 0;
46
47static void HandleProfilingSignal(int, siginfo_t*, void*);
48
49// Called during dynamic libc preinit.
50__LIBC_HIDDEN__ void __libc_init_profiling_handlers() {
51 struct sigaction action = {};
52 action.sa_flags = SA_SIGINFO | SA_RESTART;
53 action.sa_sigaction = HandleProfilingSignal;
54 sigaction(BIONIC_SIGNAL_PROFILER, &action, nullptr);
55}
56
57static void HandleProfilingSignal(int /*signal_number*/, siginfo_t* info, void* /*ucontext*/) {
58 if (info->si_code != SI_QUEUE)
59 return;
60
61 int signal_value = info->si_value.sival_int;
62 async_safe_format_log(ANDROID_LOG_WARN, "libc", "%s: received profiling signal with si_value: %d",
63 getprogname(), signal_value);
64
65 // Proceed only if the process is considered profileable.
66 bool profileable = false;
67 android_mallopt(M_GET_PROCESS_PROFILEABLE, &profileable, sizeof(profileable));
68 if (!profileable) {
69 async_safe_write_log(ANDROID_LOG_ERROR, "libc", "profiling signal rejected (not profileable)");
70 return;
71 }
72
73 if (signal_value == kHeapprofdSignalValue) {
74 HandleHeapprofdSignal();
75 } else {
76 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "unrecognized profiling signal si_value: %d",
77 signal_value);
78 }
79}