Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 <fcntl.h> |
| 31 | #include <pthread.h> |
| 32 | #include <stdatomic.h> |
| 33 | #include <stdint.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <sys/types.h> |
| 37 | |
| 38 | #include <mutex> |
| 39 | |
| 40 | #include <android-base/stringprintf.h> |
| 41 | |
| 42 | #include "Config.h" |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 43 | #include "DebugData.h" |
| 44 | #include "RecordData.h" |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 45 | #include "debug_disable.h" |
| 46 | #include "debug_log.h" |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 47 | |
| 48 | RecordEntry::RecordEntry() : tid_(gettid()) { |
| 49 | } |
| 50 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 51 | bool ThreadCompleteEntry::Write(int fd) const { |
| 52 | return dprintf(fd, "%d: thread_done 0x0\n", tid_) > 0; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 55 | AllocEntry::AllocEntry(void* pointer) : pointer_(pointer) {} |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 56 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 57 | MallocEntry::MallocEntry(void* pointer, size_t size) : AllocEntry(pointer), size_(size) {} |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 58 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 59 | bool MallocEntry::Write(int fd) const { |
| 60 | return dprintf(fd, "%d: malloc %p %zu\n", tid_, pointer_, size_) > 0; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 63 | FreeEntry::FreeEntry(void* pointer) : AllocEntry(pointer) {} |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 64 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 65 | bool FreeEntry::Write(int fd) const { |
| 66 | return dprintf(fd, "%d: free %p\n", tid_, pointer_) > 0; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | CallocEntry::CallocEntry(void* pointer, size_t nmemb, size_t size) |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 70 | : MallocEntry(pointer, size), nmemb_(nmemb) {} |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 71 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 72 | bool CallocEntry::Write(int fd) const { |
| 73 | return dprintf(fd, "%d: calloc %p %zu %zu\n", tid_, pointer_, nmemb_, size_) > 0; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ReallocEntry::ReallocEntry(void* pointer, size_t size, void* old_pointer) |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 77 | : MallocEntry(pointer, size), old_pointer_(old_pointer) {} |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 78 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 79 | bool ReallocEntry::Write(int fd) const { |
| 80 | return dprintf(fd, "%d: realloc %p %p %zu\n", tid_, pointer_, old_pointer_, size_) > 0; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 83 | // aligned_alloc, posix_memalign, memalign, pvalloc, valloc all recorded with this class. |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 84 | MemalignEntry::MemalignEntry(void* pointer, size_t size, size_t alignment) |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 85 | : MallocEntry(pointer, size), alignment_(alignment) {} |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 86 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 87 | bool MemalignEntry::Write(int fd) const { |
| 88 | return dprintf(fd, "%d: memalign %p %zu %zu\n", tid_, pointer_, alignment_, size_) > 0; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | struct ThreadData { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 92 | ThreadData(RecordData* record_data, ThreadCompleteEntry* entry) |
| 93 | : record_data(record_data), entry(entry) {} |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 94 | RecordData* record_data; |
| 95 | ThreadCompleteEntry* entry; |
| 96 | size_t count = 0; |
| 97 | }; |
| 98 | |
| 99 | static void ThreadKeyDelete(void* data) { |
| 100 | ThreadData* thread_data = reinterpret_cast<ThreadData*>(data); |
| 101 | |
| 102 | thread_data->count++; |
| 103 | |
| 104 | // This should be the last time we are called. |
| 105 | if (thread_data->count == 4) { |
| 106 | ScopedDisableDebugCalls disable; |
| 107 | |
| 108 | thread_data->record_data->AddEntryOnly(thread_data->entry); |
| 109 | delete thread_data; |
| 110 | } else { |
| 111 | pthread_setspecific(thread_data->record_data->key(), data); |
| 112 | } |
| 113 | } |
| 114 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 115 | RecordData* RecordData::record_obj_ = nullptr; |
| 116 | |
| 117 | void RecordData::WriteData(int, siginfo_t*, void*) { |
| 118 | // Dump from here, the function must not allocate so this is safe. |
| 119 | record_obj_->WriteEntries(); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 122 | void RecordData::WriteEntries() { |
| 123 | std::lock_guard<std::mutex> entries_lock(entries_lock_); |
| 124 | if (cur_index_ == 0) { |
| 125 | info_log("No alloc entries to write."); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 129 | int dump_fd = |
| 130 | open(dump_file_.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0755); |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 131 | if (dump_fd == -1) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 132 | error_log("Cannot create record alloc file %s: %s", dump_file_.c_str(), strerror(errno)); |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 133 | return; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 136 | for (size_t i = 0; i < cur_index_; i++) { |
| 137 | if (!entries_[i]->Write(dump_fd)) { |
| 138 | error_log("Failed to write record alloc information: %s", strerror(errno)); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | close(dump_fd); |
| 143 | |
| 144 | // Mark the entries dumped. |
| 145 | cur_index_ = 0U; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | RecordData::RecordData() { |
| 149 | pthread_key_create(&key_, ThreadKeyDelete); |
| 150 | } |
| 151 | |
| 152 | bool RecordData::Initialize(const Config& config) { |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 153 | record_obj_ = this; |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 154 | struct sigaction64 dump_act = {}; |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 155 | dump_act.sa_sigaction = RecordData::WriteData; |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 156 | dump_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 157 | if (sigaction64(config.record_allocs_signal(), &dump_act, nullptr) != 0) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 158 | error_log("Unable to set up record dump signal function: %s", strerror(errno)); |
| 159 | return false; |
| 160 | } |
| 161 | pthread_setspecific(key_, nullptr); |
| 162 | |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 163 | if (config.options() & VERBOSE) { |
| 164 | info_log("%s: Run: 'kill -%d %d' to dump the allocation records.", getprogname(), |
| 165 | config.record_allocs_signal(), getpid()); |
| 166 | } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 167 | |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 168 | entries_.resize(config.record_allocs_num_entries()); |
| 169 | cur_index_ = 0U; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 170 | dump_file_ = config.record_allocs_file(); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | RecordData::~RecordData() { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 176 | pthread_key_delete(key_); |
| 177 | } |
| 178 | |
| 179 | void RecordData::AddEntryOnly(const RecordEntry* entry) { |
Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame^] | 180 | std::lock_guard<std::mutex> entries_lock(entries_lock_); |
| 181 | if (cur_index_ == entries_.size()) { |
| 182 | // Maxed out, throw the entry away. |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | entries_[cur_index_++].reset(entry); |
| 187 | if (cur_index_ == entries_.size()) { |
| 188 | info_log("Maximum number of records added, all new operations will be dropped."); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | void RecordData::AddEntry(const RecordEntry* entry) { |
| 193 | void* data = pthread_getspecific(key_); |
| 194 | if (data == nullptr) { |
| 195 | ThreadData* thread_data = new ThreadData(this, new ThreadCompleteEntry()); |
| 196 | pthread_setspecific(key_, thread_data); |
| 197 | } |
| 198 | |
| 199 | AddEntryOnly(entry); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 200 | } |