| 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> |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 31 | #include <inttypes.h> |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 32 | #include <pthread.h> |
| 33 | #include <stdatomic.h> |
| 34 | #include <stdint.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <sys/types.h> |
| 38 | |
| 39 | #include <mutex> |
| 40 | |
| 41 | #include <android-base/stringprintf.h> |
| Christopher Ferris | e39602c | 2024-11-02 05:02:43 +0000 | [diff] [blame] | 42 | #include <memory_trace/MemoryTrace.h> |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 43 | |
| 44 | #include "Config.h" |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 45 | #include "DebugData.h" |
| Christopher Ferris | e39602c | 2024-11-02 05:02:43 +0000 | [diff] [blame] | 46 | #include "Nanotime.h" |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 47 | #include "RecordData.h" |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 48 | #include "debug_disable.h" |
| 49 | #include "debug_log.h" |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 50 | |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 51 | struct ThreadData { |
| Christopher Ferris | e39602c | 2024-11-02 05:02:43 +0000 | [diff] [blame] | 52 | ThreadData(RecordData* record_data) : record_data(record_data) {} |
| 53 | |
| 54 | RecordData* record_data = nullptr; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 55 | size_t count = 0; |
| 56 | }; |
| 57 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 58 | void RecordData::ThreadKeyDelete(void* data) { |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 59 | ThreadData* thread_data = reinterpret_cast<ThreadData*>(data); |
| 60 | |
| 61 | thread_data->count++; |
| 62 | |
| 63 | // This should be the last time we are called. |
| 64 | if (thread_data->count == 4) { |
| 65 | ScopedDisableDebugCalls disable; |
| 66 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 67 | memory_trace::Entry* entry = thread_data->record_data->InternalReserveEntry(); |
| 68 | if (entry != nullptr) { |
| 69 | *entry = memory_trace::Entry{ |
| 70 | .tid = gettid(), .type = memory_trace::THREAD_DONE, .end_ns = Nanotime()}; |
| 71 | } |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 72 | delete thread_data; |
| 73 | } else { |
| 74 | pthread_setspecific(thread_data->record_data->key(), data); |
| 75 | } |
| 76 | } |
| 77 | |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 78 | RecordData* RecordData::record_obj_ = nullptr; |
| 79 | |
| 80 | void RecordData::WriteData(int, siginfo_t*, void*) { |
| 81 | // Dump from here, the function must not allocate so this is safe. |
| 82 | record_obj_->WriteEntries(); |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 85 | void RecordData::WriteEntriesOnExit() { |
| 86 | if (record_obj_ == nullptr) return; |
| 87 | |
| 88 | // Append the current pid to the file name to avoid multiple processes |
| 89 | // writing to the same file. |
| 90 | std::string file(record_obj_->file()); |
| 91 | file += "." + std::to_string(getpid()); |
| 92 | record_obj_->WriteEntries(file); |
| 93 | } |
| 94 | |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 95 | void RecordData::WriteEntries() { |
| Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 96 | WriteEntries(file_); |
| 97 | } |
| 98 | |
| 99 | void RecordData::WriteEntries(const std::string& file) { |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 100 | std::lock_guard<std::mutex> entries_lock(entries_lock_); |
| 101 | if (cur_index_ == 0) { |
| 102 | info_log("No alloc entries to write."); |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
| Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 106 | int dump_fd = open(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] | 107 | if (dump_fd == -1) { |
| Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 108 | error_log("Cannot create record alloc file %s: %s", file.c_str(), strerror(errno)); |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 109 | return; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 112 | for (size_t i = 0; i < cur_index_; i++) { |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 113 | if (entries_[i].type == memory_trace::UNKNOWN) { |
| 114 | // This can happen if an entry was reserved but not filled in due to some |
| 115 | // type of error during the operation. |
| 116 | continue; |
| 117 | } |
| Christopher Ferris | e39602c | 2024-11-02 05:02:43 +0000 | [diff] [blame] | 118 | if (!memory_trace::WriteEntryToFd(dump_fd, entries_[i])) { |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 119 | error_log("Failed to write record alloc information: %s", strerror(errno)); |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | close(dump_fd); |
| 124 | |
| 125 | // Mark the entries dumped. |
| 126 | cur_index_ = 0U; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | RecordData::RecordData() { |
| 130 | pthread_key_create(&key_, ThreadKeyDelete); |
| 131 | } |
| 132 | |
| 133 | bool RecordData::Initialize(const Config& config) { |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 134 | record_obj_ = this; |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 135 | struct sigaction64 dump_act = {}; |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 136 | dump_act.sa_sigaction = RecordData::WriteData; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 137 | dump_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 138 | if (sigaction64(config.record_allocs_signal(), &dump_act, nullptr) != 0) { |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 139 | error_log("Unable to set up record dump signal function: %s", strerror(errno)); |
| 140 | return false; |
| 141 | } |
| 142 | pthread_setspecific(key_, nullptr); |
| 143 | |
| Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 144 | if (config.options() & VERBOSE) { |
| 145 | info_log("%s: Run: 'kill -%d %d' to dump the allocation records.", getprogname(), |
| 146 | config.record_allocs_signal(), getpid()); |
| 147 | } |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 148 | |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 149 | entries_.resize(config.record_allocs_num_entries()); |
| 150 | cur_index_ = 0U; |
| Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 151 | file_ = config.record_allocs_file(); |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | RecordData::~RecordData() { |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 157 | pthread_key_delete(key_); |
| 158 | } |
| 159 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 160 | memory_trace::Entry* RecordData::InternalReserveEntry() { |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 161 | std::lock_guard<std::mutex> entries_lock(entries_lock_); |
| 162 | if (cur_index_ == entries_.size()) { |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 163 | return nullptr; |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 166 | memory_trace::Entry* entry = &entries_[cur_index_]; |
| 167 | entry->type = memory_trace::UNKNOWN; |
| 168 | if (++cur_index_ == entries_.size()) { |
| Christopher Ferris | 06993a6 | 2022-09-07 18:19:45 -0700 | [diff] [blame] | 169 | 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] | 170 | } |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 171 | return entry; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 174 | memory_trace::Entry* RecordData::ReserveEntry() { |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 175 | void* data = pthread_getspecific(key_); |
| 176 | if (data == nullptr) { |
| Christopher Ferris | e39602c | 2024-11-02 05:02:43 +0000 | [diff] [blame] | 177 | ThreadData* thread_data = new ThreadData(this); |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 178 | pthread_setspecific(key_, thread_data); |
| 179 | } |
| 180 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame^] | 181 | return InternalReserveEntry(); |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 182 | } |