blob: 41ab5ac4ea4ee9d3b905e91961e81cedcd75f53a [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
2 * Copyright (C) 2015 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 <pthread.h>
30#include <stdint.h>
31#include <stdlib.h>
32
33#include <algorithm>
34#include <vector>
35
36#include <private/ScopedPthreadMutexLocker.h>
37
38#include "backtrace.h"
39#include "BacktraceData.h"
40#include "Config.h"
41#include "DebugData.h"
42#include "debug_disable.h"
43#include "debug_log.h"
44#include "malloc_debug.h"
45#include "TrackData.h"
46
Colin Crossc40577f2016-01-29 12:48:18 -080047void TrackData::GetList(std::vector<const Header*>* list) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080048 ScopedDisableDebugCalls disable;
49
50 for (const auto& header : headers_) {
51 list->push_back(header);
52 }
53
54 // Sort by the size of the allocation.
Colin Crossc40577f2016-01-29 12:48:18 -080055 std::sort(list->begin(), list->end(), [](const Header* a, const Header* b) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080056 if (a->size == b->size) return a < b;
57 return a->size > b->size;
58 });
59}
60
Colin Crossc40577f2016-01-29 12:48:18 -080061void TrackData::Add(const Header* header, bool backtrace_found) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080062 ScopedDisableDebugCalls disable;
63
64 pthread_mutex_lock(&mutex_);
65 if (backtrace_found) {
66 total_backtrace_allocs_++;
67 }
68 headers_.insert(header);
69 pthread_mutex_unlock(&mutex_);
70}
71
Colin Crossc40577f2016-01-29 12:48:18 -080072void TrackData::Remove(const Header* header, bool backtrace_found) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080073 ScopedDisableDebugCalls disable;
74
75 pthread_mutex_lock(&mutex_);
76 headers_.erase(header);
77 if (backtrace_found) {
78 total_backtrace_allocs_--;
79 }
80 pthread_mutex_unlock(&mutex_);
81}
82
Colin Crossc40577f2016-01-29 12:48:18 -080083bool TrackData::Contains(const Header* header) {
84 ScopedDisableDebugCalls disable;
85
86 pthread_mutex_lock(&mutex_);
87 bool found = headers_.count(header);
88 pthread_mutex_unlock(&mutex_);
89 return found;
90}
91
Christopher Ferris63860cb2015-11-16 17:30:32 -080092void TrackData::DisplayLeaks(DebugData& debug) {
93 ScopedDisableDebugCalls disable;
94
Colin Crossc40577f2016-01-29 12:48:18 -080095 std::vector<const Header*> list;
Christopher Ferris63860cb2015-11-16 17:30:32 -080096 GetList(&list);
97
98 size_t track_count = 0;
99 for (const auto& header : list) {
100 error_log("+++ %s leaked block of size %zu at %p (leak %zu of %zu)", getprogname(),
101 header->real_size(), debug.GetPointer(header), ++track_count, list.size());
102 if (debug.config().options & BACKTRACE) {
103 BacktraceHeader* back_header = debug.GetAllocBacktrace(header);
104 if (back_header->num_frames > 0) {
105 error_log("Backtrace at time of allocation:");
106 backtrace_log(&back_header->frames[0], back_header->num_frames);
107 }
108 }
109 g_dispatch->free(header->orig_pointer);
110 }
111}
112
113void TrackData::GetInfo(DebugData& debug, uint8_t** info, size_t* overall_size,
114 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
115 ScopedPthreadMutexLocker scoped(&mutex_);
116
117 if (headers_.size() == 0 || total_backtrace_allocs_ == 0) {
118 return;
119 }
120
121 *backtrace_size = debug.config().backtrace_frames;
122 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
123 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, total_backtrace_allocs_));
124 if (*info == nullptr) {
125 return;
126 }
127 *overall_size = *info_size * total_backtrace_allocs_;
128
Colin Crossc40577f2016-01-29 12:48:18 -0800129 std::vector<const Header*> list;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800130 GetList(&list);
131
132 uint8_t* data = *info;
133 for (const auto& header : list) {
134 BacktraceHeader* back_header = debug.GetAllocBacktrace(header);
135 if (back_header->num_frames > 0) {
136 memcpy(data, &header->size, sizeof(size_t));
137 memcpy(&data[sizeof(size_t)], &back_header->num_frames, sizeof(size_t));
138 memcpy(&data[2 * sizeof(size_t)], &back_header->frames[0],
139 back_header->num_frames * sizeof(uintptr_t));
140
141 *total_memory += header->real_size();
142
143 data += *info_size;
144 }
145 }
146}
Shibin George297034e2016-09-06 21:58:22 +0530147
148// Leak Patch Begin
149#include <errno.h>
150#include <signal.h>
151#include <sys/types.h>
152#include <unistd.h>
153
154#define DEBUG_SIGNAL SIGWINCH
155#define DEBUG_MAX_STACK (30)
156extern DebugData* g_debug;
157
158struct LeakHeader {
159 size_t size;
160 size_t count;
161 BacktraceHeader* back_header;
162} __attribute__((packed));
163
164LeakHeader* SearchLeakHeader(LeakHeader* list, BacktraceHeader* back_header, int records) {
165 LeakHeader* leakHeader = list;
166
167 //error_log("SearchLeakHeader list = %p, back_header = %p", list, back_header);
168 //error_log("SearchLeakHeader num_frames = %zu, record = %d", back_header->num_frames, records);
169
170 for (int i = 0; i < records; i++) {
171 //error_log("SearchLeakHeader leakHeader = %p, back_header = %p, i = %d", leakHeader, leakHeader->back_header, i);
172 //error_log("SearchLeakHeader num_frames = %zu", leakHeader->back_header->num_frames);
173
174 if ((leakHeader->back_header->num_frames == back_header->num_frames) &&
175 (!memcmp(leakHeader->back_header->frames, back_header->frames, back_header->num_frames * sizeof(uintptr_t)))) {
176 // error_log("SearchLeakHeader leakHeader = %p", leakHeader);
177 return leakHeader;
178 }
179 leakHeader ++;
180 }
181
182 return nullptr;
183}
184
185int CompareHeader( const void *a ,const void *b) {
186 return (*(LeakHeader *)a).size > (*(LeakHeader *)b).size ? -1 : 1;
187}
188
189void TrackData::DumpLeaks(DebugData& debug) {
190 ScopedDisableDebugCalls disable;
191
192 if (headers_.size() == 0 || total_backtrace_allocs_ == 0) {
193 return;
194 }
195
196 if (!(debug.config().options & BACKTRACE)) {
197 return;
198 }
199
200 pthread_mutex_lock(&mutex_);
201
202 int records = 0;
203 int top = 0;
204 size_t total_size = 0;
205 BacktraceHeader* back_header;
206 LeakHeader* list = static_cast<LeakHeader*>(g_dispatch->calloc(total_backtrace_allocs_, sizeof(LeakHeader)));
207 LeakHeader* leakHeader = list;
208 LeakHeader* findHeader = nullptr;
209
210 error_log("+++ %s leaked memory dumping started +++", getprogname());
211 //error_log("DumpLeaks list = %p, total_backtrace_allocs_ = %zu", list, total_backtrace_allocs_);
212
213 for (const auto& header : headers_) {
214 back_header = debug.GetAllocBacktrace(header);
215 if(back_header->num_frames == 0) {
216 continue;
217 }
218
219 findHeader = SearchLeakHeader(list, back_header, records);
220
221 // error_log("findHeader = %p", findHeader);
222
223 total_size += header->real_size();
224 if (findHeader != nullptr) {
225 findHeader->size += header->real_size();
226 findHeader->count ++;
227 } else {
228 records ++;
229 leakHeader->size = header->real_size();
230 leakHeader->count = 1;
231 leakHeader->back_header = back_header;
232 leakHeader ++;
233 }
234 }
235
236 //error_log("+++ %s leaked memory sorting started +++", getprogname());
237 qsort(list, records, sizeof(LeakHeader), CompareHeader);
238 //error_log("+++ %s leaked memory sorting ended +++", getprogname());
239
240 leakHeader = list;
241
242 if (records > DEBUG_MAX_STACK) {
243 top = DEBUG_MAX_STACK; // Only print DEBUG_MAX_STACK records.
244 } else {
245 top = records;
246 }
247
248 error_log("+++ total size: %d K, total records: %d, top records: %d +++", (int)(total_size / 1024), records, top);
249
250 for (int i = 0; i < top; i++) {
251 error_log("+++ Backtrace at time of allocation: total size %zu (leak times: %zu, avg size: %zu) +++",
252 leakHeader->size, leakHeader->count, leakHeader->size / leakHeader->count);
253 //error_log("num_frames: %zu frames: %zu", leakHeader->back_header->num_frames, leakHeader->back_header->frames[0]);
254 backtrace_log(&leakHeader->back_header->frames[0], leakHeader->back_header->num_frames);
255 leakHeader ++;
256 }
257
258 error_log("+++ %s leaked memory dumping ended +++", getprogname());
259
260 g_dispatch->free(list);
261
262 pthread_mutex_unlock(&mutex_);
263}
264
265static void* PrintLeaks(void *arg) {
266 //g_debug->track->DisplayLeaks(*g_debug);
267
268 info_log("PrintLeaks::%s:%d backtrace printing. Thread = %s", getprogname(), getpid(), (char*)arg);
269
270 g_debug->track->DumpLeaks(*g_debug);
271
272 return nullptr;
273}
274
275static void PrintLeaksSignal(int, siginfo_t*, void*) {
276 if (!(g_debug->config().options & BACKTRACE) || !(g_debug->config().options & LEAK_TRACK)) {
277 error_log("PrintLeaksSignal::%s:%d backtrace hasn't been enabled.", getprogname(), getpid());
278 return;
279 }
280
281 int err;
282 pthread_t ntid;
283 pthread_attr_t attributes;
284 pthread_attr_init(&attributes);
285 err = pthread_create(&ntid, &attributes, PrintLeaks, (void*)"PrintLeaks Thread");
286 if (err != 0) {
287 error_log("PrintLeaksSignal::%s:%d can't create PrintLeaks thread.", getprogname(), getpid());
288 }
289}
290
291TrackData::TrackData() {
292 struct sigaction enable_act;
293 memset(&enable_act, 0, sizeof(enable_act));
294
295 enable_act.sa_sigaction = PrintLeaksSignal;
296 enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
297 sigemptyset(&enable_act.sa_mask);
298 if (sigaction(DEBUG_SIGNAL, &enable_act, nullptr) != 0) {
299 error_log("Unable to set up memory leak signal function: %s", strerror(errno));
300 return;
301 }
302 info_log("%s: Run: 'kill -28 <pid of process>' to print memory leak information.", getprogname());
303}
304// Leack Patch End