Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 33 | #include <fcntl.h> |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 34 | #include <signal.h> |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 35 | #include <string.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/un.h> |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 40 | |
| 41 | #include <async_safe/log.h> |
| 42 | #include <platform/bionic/malloc.h> |
| 43 | #include <platform/bionic/reserved_signals.h> |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 44 | #include <private/ErrnoRestorer.h> |
| 45 | #include <private/ScopedFd.h> |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 46 | |
| 47 | #include "malloc_heapprofd.h" |
| 48 | |
| 49 | // This file defines the handler for the reserved signal sent by the Android |
| 50 | // platform's profilers. The accompanying signal value discriminates between |
| 51 | // specific requestors: |
| 52 | // 0: heapprofd heap profiler. |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 53 | // 1: traced_perf perf profiler. |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 54 | static constexpr int kHeapprofdSignalValue = 0; |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 55 | static constexpr int kTracedPerfSignalValue = 1; |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 56 | |
| 57 | static void HandleProfilingSignal(int, siginfo_t*, void*); |
| 58 | |
| 59 | // Called during dynamic libc preinit. |
| 60 | __LIBC_HIDDEN__ void __libc_init_profiling_handlers() { |
| 61 | struct sigaction action = {}; |
| 62 | action.sa_flags = SA_SIGINFO | SA_RESTART; |
| 63 | action.sa_sigaction = HandleProfilingSignal; |
| 64 | sigaction(BIONIC_SIGNAL_PROFILER, &action, nullptr); |
| 65 | } |
| 66 | |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 67 | static void HandleTracedPerfSignal(); |
| 68 | |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 69 | static void HandleProfilingSignal(int /*signal_number*/, siginfo_t* info, void* /*ucontext*/) { |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 70 | // Avoid clobbering errno. |
| 71 | ErrnoRestorer errno_restorer; |
| 72 | |
| 73 | if (info->si_code != SI_QUEUE) { |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 74 | return; |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 75 | } |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 76 | |
| 77 | int signal_value = info->si_value.sival_int; |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 78 | async_safe_format_log(ANDROID_LOG_INFO, "libc", "%s: received profiling signal with si_value: %d", |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 79 | getprogname(), signal_value); |
| 80 | |
| 81 | // Proceed only if the process is considered profileable. |
| 82 | bool profileable = false; |
| 83 | android_mallopt(M_GET_PROCESS_PROFILEABLE, &profileable, sizeof(profileable)); |
| 84 | if (!profileable) { |
| 85 | async_safe_write_log(ANDROID_LOG_ERROR, "libc", "profiling signal rejected (not profileable)"); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (signal_value == kHeapprofdSignalValue) { |
| 90 | HandleHeapprofdSignal(); |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 91 | } else if (signal_value == kTracedPerfSignalValue) { |
| 92 | HandleTracedPerfSignal(); |
Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 93 | } else { |
| 94 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "unrecognized profiling signal si_value: %d", |
| 95 | signal_value); |
| 96 | } |
| 97 | } |
Ryan Savitski | e953163 | 2020-01-06 16:51:05 +0000 | [diff] [blame^] | 98 | |
| 99 | // Open /proc/self/{maps,mem}, connect to traced_perf, send the fds over the |
| 100 | // socket. Everything happens synchronously within the signal handler. Socket |
| 101 | // is made non-blocking, and we do not retry. |
| 102 | static void HandleTracedPerfSignal() { |
| 103 | ScopedFd sock_fd{ socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0 /*protocol*/) }; |
| 104 | if (sock_fd.get() == -1) { |
| 105 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to create socket: %s", strerror(errno)); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | sockaddr_un saddr{ AF_UNIX, "/dev/socket/traced_perf" }; |
| 110 | size_t addrlen = sizeof(sockaddr_un); |
| 111 | if (connect(sock_fd.get(), reinterpret_cast<const struct sockaddr*>(&saddr), addrlen) == -1) { |
| 112 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to connect to traced_perf socket: %s", |
| 113 | strerror(errno)); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | ScopedFd maps_fd{ open("/proc/self/maps", O_RDONLY | O_CLOEXEC) }; |
| 118 | if (maps_fd.get() == -1) { |
| 119 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to open /proc/self/maps: %s", |
| 120 | strerror(errno)); |
| 121 | return; |
| 122 | } |
| 123 | ScopedFd mem_fd{ open("/proc/self/mem", O_RDONLY | O_CLOEXEC) }; |
| 124 | if (mem_fd.get() == -1) { |
| 125 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to open /proc/self/mem: %s", |
| 126 | strerror(errno)); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | // Send 1 byte with auxiliary data carrying two fds. |
| 131 | int send_fds[2] = { maps_fd.get(), mem_fd.get() }; |
| 132 | int num_fds = 2; |
| 133 | char iobuf[1] = {}; |
| 134 | msghdr msg_hdr = {}; |
| 135 | iovec iov = { reinterpret_cast<void*>(iobuf), sizeof(iobuf) }; |
| 136 | msg_hdr.msg_iov = &iov; |
| 137 | msg_hdr.msg_iovlen = 1; |
| 138 | alignas(cmsghdr) char control_buf[256] = {}; |
| 139 | const auto raw_ctl_data_sz = num_fds * sizeof(int); |
| 140 | const size_t control_buf_len = static_cast<size_t>(CMSG_SPACE(raw_ctl_data_sz)); |
| 141 | msg_hdr.msg_control = control_buf; |
| 142 | msg_hdr.msg_controllen = control_buf_len; // used by CMSG_FIRSTHDR |
| 143 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg_hdr); |
| 144 | cmsg->cmsg_level = SOL_SOCKET; |
| 145 | cmsg->cmsg_type = SCM_RIGHTS; |
| 146 | cmsg->cmsg_len = static_cast<size_t>(CMSG_LEN(raw_ctl_data_sz)); |
| 147 | memcpy(CMSG_DATA(cmsg), send_fds, num_fds * sizeof(int)); |
| 148 | |
| 149 | if (sendmsg(sock_fd.get(), &msg_hdr, 0) == -1) { |
| 150 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to sendmsg: %s", strerror(errno)); |
| 151 | } |
| 152 | } |