blob: e5260a9171a6ee415a377b28f39e3e2d11186f71 [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
47void TrackData::GetList(std::vector<Header*>* list) {
48 ScopedDisableDebugCalls disable;
49
50 for (const auto& header : headers_) {
51 list->push_back(header);
52 }
53
54 // Sort by the size of the allocation.
55 std::sort(list->begin(), list->end(), [](Header* a, Header* b) {
56 if (a->size == b->size) return a < b;
57 return a->size > b->size;
58 });
59}
60
61void TrackData::Add(Header* header, bool backtrace_found) {
62 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
72void TrackData::Remove(Header* header, bool backtrace_found) {
73 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
83void TrackData::DisplayLeaks(DebugData& debug) {
84 ScopedDisableDebugCalls disable;
85
86 std::vector<Header*> list;
87 GetList(&list);
88
89 size_t track_count = 0;
90 for (const auto& header : list) {
91 error_log("+++ %s leaked block of size %zu at %p (leak %zu of %zu)", getprogname(),
92 header->real_size(), debug.GetPointer(header), ++track_count, list.size());
93 if (debug.config().options & BACKTRACE) {
94 BacktraceHeader* back_header = debug.GetAllocBacktrace(header);
95 if (back_header->num_frames > 0) {
96 error_log("Backtrace at time of allocation:");
97 backtrace_log(&back_header->frames[0], back_header->num_frames);
98 }
99 }
100 g_dispatch->free(header->orig_pointer);
101 }
102}
103
104void TrackData::GetInfo(DebugData& debug, uint8_t** info, size_t* overall_size,
105 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
106 ScopedPthreadMutexLocker scoped(&mutex_);
107
108 if (headers_.size() == 0 || total_backtrace_allocs_ == 0) {
109 return;
110 }
111
112 *backtrace_size = debug.config().backtrace_frames;
113 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
114 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, total_backtrace_allocs_));
115 if (*info == nullptr) {
116 return;
117 }
118 *overall_size = *info_size * total_backtrace_allocs_;
119
120 std::vector<Header*> list;
121 GetList(&list);
122
123 uint8_t* data = *info;
124 for (const auto& header : list) {
125 BacktraceHeader* back_header = debug.GetAllocBacktrace(header);
126 if (back_header->num_frames > 0) {
127 memcpy(data, &header->size, sizeof(size_t));
128 memcpy(&data[sizeof(size_t)], &back_header->num_frames, sizeof(size_t));
129 memcpy(&data[2 * sizeof(size_t)], &back_header->frames[0],
130 back_header->num_frames * sizeof(uintptr_t));
131
132 *total_memory += header->real_size();
133
134 data += *info_size;
135 }
136 }
137}