Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 47 | TrackData::TrackData(DebugData* debug_data) : OptionData(debug_data) { |
| 48 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 49 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 50 | void TrackData::GetList(std::vector<const Header*>* list) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 51 | for (const auto& header : headers_) { |
| 52 | list->push_back(header); |
| 53 | } |
| 54 | |
| 55 | // Sort by the size of the allocation. |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 56 | std::sort(list->begin(), list->end(), [](const Header* a, const Header* b) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 57 | if (a->size == b->size) return a < b; |
| 58 | return a->size > b->size; |
| 59 | }); |
| 60 | } |
| 61 | |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 62 | void TrackData::Add(const Header* header, bool backtrace_found) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 63 | pthread_mutex_lock(&mutex_); |
| 64 | if (backtrace_found) { |
| 65 | total_backtrace_allocs_++; |
| 66 | } |
| 67 | headers_.insert(header); |
| 68 | pthread_mutex_unlock(&mutex_); |
| 69 | } |
| 70 | |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 71 | void TrackData::Remove(const Header* header, bool backtrace_found) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 72 | pthread_mutex_lock(&mutex_); |
| 73 | headers_.erase(header); |
| 74 | if (backtrace_found) { |
| 75 | total_backtrace_allocs_--; |
| 76 | } |
| 77 | pthread_mutex_unlock(&mutex_); |
| 78 | } |
| 79 | |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 80 | bool TrackData::Contains(const Header* header) { |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 81 | pthread_mutex_lock(&mutex_); |
| 82 | bool found = headers_.count(header); |
| 83 | pthread_mutex_unlock(&mutex_); |
| 84 | return found; |
| 85 | } |
| 86 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 87 | void TrackData::DisplayLeaks() { |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 88 | std::vector<const Header*> list; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 89 | GetList(&list); |
| 90 | |
| 91 | size_t track_count = 0; |
| 92 | for (const auto& header : list) { |
| 93 | error_log("+++ %s leaked block of size %zu at %p (leak %zu of %zu)", getprogname(), |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 94 | header->real_size(), debug_->GetPointer(header), ++track_count, list.size()); |
| 95 | if (debug_->config().options & BACKTRACE) { |
| 96 | BacktraceHeader* back_header = debug_->GetAllocBacktrace(header); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 97 | if (back_header->num_frames > 0) { |
| 98 | error_log("Backtrace at time of allocation:"); |
| 99 | backtrace_log(&back_header->frames[0], back_header->num_frames); |
| 100 | } |
| 101 | } |
| 102 | g_dispatch->free(header->orig_pointer); |
| 103 | } |
| 104 | } |
| 105 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 106 | void TrackData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size, |
| 107 | size_t* total_memory, size_t* backtrace_size) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 108 | ScopedPthreadMutexLocker scoped(&mutex_); |
| 109 | |
| 110 | if (headers_.size() == 0 || total_backtrace_allocs_ == 0) { |
| 111 | return; |
| 112 | } |
| 113 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 114 | *backtrace_size = debug_->config().backtrace_frames; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 115 | *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size; |
| 116 | *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, total_backtrace_allocs_)); |
| 117 | if (*info == nullptr) { |
| 118 | return; |
| 119 | } |
| 120 | *overall_size = *info_size * total_backtrace_allocs_; |
| 121 | |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 122 | std::vector<const Header*> list; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 123 | GetList(&list); |
| 124 | |
| 125 | uint8_t* data = *info; |
| 126 | for (const auto& header : list) { |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 127 | BacktraceHeader* back_header = debug_->GetAllocBacktrace(header); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 128 | if (back_header->num_frames > 0) { |
| 129 | memcpy(data, &header->size, sizeof(size_t)); |
| 130 | memcpy(&data[sizeof(size_t)], &back_header->num_frames, sizeof(size_t)); |
| 131 | memcpy(&data[2 * sizeof(size_t)], &back_header->frames[0], |
| 132 | back_header->num_frames * sizeof(uintptr_t)); |
| 133 | |
| 134 | *total_memory += header->real_size(); |
| 135 | |
| 136 | data += *info_size; |
| 137 | } |
| 138 | } |
| 139 | } |