blob: 5c550c0f26e19de4a94930752ae8b4771b6aba33 [file] [log] [blame]
Christopher Ferris7bd01782016-04-20 12:30:58 -07001/*
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 Ferris7bd01782016-04-20 12:30:58 -070043#include "DebugData.h"
44#include "RecordData.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080045#include "debug_disable.h"
46#include "debug_log.h"
Christopher Ferris7bd01782016-04-20 12:30:58 -070047
48RecordEntry::RecordEntry() : tid_(gettid()) {
49}
50
51std::string ThreadCompleteEntry::GetString() const {
52 return android::base::StringPrintf("%d: thread_done 0x0\n", tid_);
53}
54
Christopher Ferris4da25032018-03-07 13:38:48 -080055AllocEntry::AllocEntry(void* pointer) : pointer_(pointer) {}
Christopher Ferris7bd01782016-04-20 12:30:58 -070056
Christopher Ferris4da25032018-03-07 13:38:48 -080057MallocEntry::MallocEntry(void* pointer, size_t size) : AllocEntry(pointer), size_(size) {}
Christopher Ferris7bd01782016-04-20 12:30:58 -070058
59std::string MallocEntry::GetString() const {
60 return android::base::StringPrintf("%d: malloc %p %zu\n", tid_, pointer_, size_);
61}
62
Christopher Ferris4da25032018-03-07 13:38:48 -080063FreeEntry::FreeEntry(void* pointer) : AllocEntry(pointer) {}
Christopher Ferris7bd01782016-04-20 12:30:58 -070064
65std::string FreeEntry::GetString() const {
66 return android::base::StringPrintf("%d: free %p\n", tid_, pointer_);
67}
68
69CallocEntry::CallocEntry(void* pointer, size_t nmemb, size_t size)
Christopher Ferris4da25032018-03-07 13:38:48 -080070 : MallocEntry(pointer, size), nmemb_(nmemb) {}
Christopher Ferris7bd01782016-04-20 12:30:58 -070071
72std::string CallocEntry::GetString() const {
73 return android::base::StringPrintf("%d: calloc %p %zu %zu\n", tid_, pointer_, nmemb_, size_);
74}
75
76ReallocEntry::ReallocEntry(void* pointer, size_t size, void* old_pointer)
Christopher Ferris4da25032018-03-07 13:38:48 -080077 : MallocEntry(pointer, size), old_pointer_(old_pointer) {}
Christopher Ferris7bd01782016-04-20 12:30:58 -070078
79std::string ReallocEntry::GetString() const {
Christopher Ferris4da25032018-03-07 13:38:48 -080080 return android::base::StringPrintf("%d: realloc %p %p %zu\n", tid_, pointer_, old_pointer_, size_);
Christopher Ferris7bd01782016-04-20 12:30:58 -070081}
82
Christopher Ferriscae21a92018-02-05 18:14:55 -080083// aligned_alloc, posix_memalign, memalign, pvalloc, valloc all recorded with this class.
Christopher Ferris7bd01782016-04-20 12:30:58 -070084MemalignEntry::MemalignEntry(void* pointer, size_t size, size_t alignment)
Christopher Ferris4da25032018-03-07 13:38:48 -080085 : MallocEntry(pointer, size), alignment_(alignment) {}
Christopher Ferris7bd01782016-04-20 12:30:58 -070086
87std::string MemalignEntry::GetString() const {
Christopher Ferris4da25032018-03-07 13:38:48 -080088 return android::base::StringPrintf("%d: memalign %p %zu %zu\n", tid_, pointer_, alignment_, size_);
Christopher Ferris7bd01782016-04-20 12:30:58 -070089}
90
91struct ThreadData {
Christopher Ferris4da25032018-03-07 13:38:48 -080092 ThreadData(RecordData* record_data, ThreadCompleteEntry* entry)
93 : record_data(record_data), entry(entry) {}
Christopher Ferris7bd01782016-04-20 12:30:58 -070094 RecordData* record_data;
95 ThreadCompleteEntry* entry;
96 size_t count = 0;
97};
98
99static 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
115static void RecordDump(int, siginfo_t*, void*) {
116 // It's not necessarily safe to do the dump here, instead wait for the
117 // next allocation call to do the dump.
118 g_debug->record->SetToDump();
119}
120
121void RecordData::Dump() {
122 std::lock_guard<std::mutex> lock(dump_lock_);
123
124 // Make it so that no more entries can be added while dumping.
125 unsigned int last_entry_index = cur_index_.exchange(static_cast<unsigned int>(num_entries_));
126 if (dump_ == false) {
127 // Multiple Dump() calls from different threads, and we lost. Do nothing.
128 return;
129 }
130
131 // cur_index_ keeps getting incremented even if we hit the num_entries_.
132 // If that happens, cap the entries to dump by num_entries_.
133 if (last_entry_index > num_entries_) {
134 last_entry_index = num_entries_;
135 }
136
Christopher Ferris4da25032018-03-07 13:38:48 -0800137 int dump_fd =
138 open(dump_file_.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0755);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700139 if (dump_fd != -1) {
140 for (size_t i = 0; i < last_entry_index; i++) {
141 std::string line = entries_[i]->GetString();
142 ssize_t bytes = write(dump_fd, line.c_str(), line.length());
143 if (bytes == -1 || static_cast<size_t>(bytes) != line.length()) {
144 error_log("Failed to write record alloc information: %s", strerror(errno));
145 // Free all of the rest of the errors, we don't have any way
146 // to dump a partial list of the entries.
147 for (i++; i < last_entry_index; i++) {
148 delete entries_[i];
149 entries_[i] = nullptr;
150 }
151 break;
152 }
153 delete entries_[i];
154 entries_[i] = nullptr;
155 }
156 close(dump_fd);
157
158 // Mark the entries dumped.
159 cur_index_ = 0U;
160 } else {
161 error_log("Cannot create record alloc file %s: %s", dump_file_.c_str(), strerror(errno));
162 // Since we couldn't create the file, reset the entries dumped back
163 // to the original value.
164 cur_index_ = last_entry_index;
165 }
166
167 dump_ = false;
168}
169
170RecordData::RecordData() {
171 pthread_key_create(&key_, ThreadKeyDelete);
172}
173
174bool RecordData::Initialize(const Config& config) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800175 struct sigaction64 dump_act = {};
Christopher Ferris7bd01782016-04-20 12:30:58 -0700176 dump_act.sa_sigaction = RecordDump;
177 dump_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
Elliott Hughes3e235912018-02-01 14:21:51 -0800178 if (sigaction64(config.record_allocs_signal(), &dump_act, nullptr) != 0) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700179 error_log("Unable to set up record dump signal function: %s", strerror(errno));
180 return false;
181 }
182 pthread_setspecific(key_, nullptr);
183
Christopher Ferrisc328e442019-04-01 19:31:26 -0700184 if (config.options() & VERBOSE) {
185 info_log("%s: Run: 'kill -%d %d' to dump the allocation records.", getprogname(),
186 config.record_allocs_signal(), getpid());
187 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700188
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700189 num_entries_ = config.record_allocs_num_entries();
Christopher Ferris7bd01782016-04-20 12:30:58 -0700190 entries_ = new const RecordEntry*[num_entries_];
191 cur_index_ = 0;
192 dump_ = false;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700193 dump_file_ = config.record_allocs_file();
Christopher Ferris7bd01782016-04-20 12:30:58 -0700194
195 return true;
196}
197
198RecordData::~RecordData() {
Christopher Ferris4da25032018-03-07 13:38:48 -0800199 delete[] entries_;
Christopher Ferris7bd01782016-04-20 12:30:58 -0700200 pthread_key_delete(key_);
201}
202
203void RecordData::AddEntryOnly(const RecordEntry* entry) {
204 unsigned int entry_index = cur_index_.fetch_add(1);
205 if (entry_index < num_entries_) {
206 entries_[entry_index] = entry;
207 }
208}
209
210void RecordData::AddEntry(const RecordEntry* entry) {
211 void* data = pthread_getspecific(key_);
212 if (data == nullptr) {
213 ThreadData* thread_data = new ThreadData(this, new ThreadCompleteEntry());
214 pthread_setspecific(key_, thread_data);
215 }
216
217 AddEntryOnly(entry);
218
219 // Check to see if it's time to dump the entries.
220 if (dump_) {
221 Dump();
222 }
223}