blob: 7c4cfbf976123b74d31c4b0837f44288b643c203 [file] [log] [blame]
Josh Gao97271922019-11-06 13:15:00 -08001/*
2 * Copyright (C) 2019 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 <inttypes.h>
30
31#include <array>
32#include <mutex>
33#include <vector>
34
35#include <android/fdtrack.h>
36
37#include <android-base/no_destructor.h>
38#include <android-base/thread_annotations.h>
39#include <async_safe/log.h>
40#include <bionic/reserved_signals.h>
41#include <unwindstack/LocalUnwinder.h>
42
43struct FdEntry {
44 std::mutex mutex;
45 std::vector<unwindstack::LocalFrameData> backtrace GUARDED_BY(mutex);
46};
47
48extern "C" void fdtrack_dump();
49static void fd_hook(android_fdtrack_event* event);
50
51// Backtraces for the first 4k file descriptors ought to be enough to diagnose an fd leak.
52static constexpr size_t kFdTableSize = 4096;
53static constexpr size_t kStackDepth = 10;
54
55static bool installed = false;
56static std::array<FdEntry, kFdTableSize> stack_traces;
57static unwindstack::LocalUnwinder& Unwinder() {
58 static android::base::NoDestructor<unwindstack::LocalUnwinder> unwinder;
59 return *unwinder.get();
60}
61
62__attribute__((constructor)) static void ctor() {
63 signal(BIONIC_SIGNAL_FDTRACK, [](int) { fdtrack_dump(); });
64 if (Unwinder().Init()) {
65 android_fdtrack_hook_t expected = nullptr;
66 installed = android_fdtrack_compare_exchange_hook(&expected, &fd_hook);
67 }
68}
69
70__attribute__((destructor)) static void dtor() {
71 if (installed) {
72 android_fdtrack_hook_t expected = &fd_hook;
73 android_fdtrack_compare_exchange_hook(&expected, nullptr);
74 }
75}
76
77FdEntry* GetFdEntry(int fd) {
78 if (fd >= 0 && fd < static_cast<int>(kFdTableSize)) {
79 return &stack_traces[fd];
80 }
81 return nullptr;
82}
83
84static void fd_hook(android_fdtrack_event* event) {
85 if (event->type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) {
86 if (FdEntry* entry = GetFdEntry(event->fd); entry) {
87 std::lock_guard<std::mutex> lock(entry->mutex);
88 entry->backtrace.clear();
89 Unwinder().Unwind(&entry->backtrace, kStackDepth);
90 }
91 } else if (event->type == ANDROID_FDTRACK_EVENT_TYPE_CLOSE) {
92 if (FdEntry* entry = GetFdEntry(event->fd); entry) {
93 std::lock_guard<std::mutex> lock(entry->mutex);
94 entry->backtrace.clear();
95 }
96 }
97}
98
99void fdtrack_dump() {
100 if (!installed) {
101 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fdtrack not installed");
102 } else {
103 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fdtrack dumping...");
104 }
105
106 bool prev = android_fdtrack_set_enabled(false);
107 for (int fd = 0; fd < static_cast<int>(stack_traces.size()); ++fd) {
108 FdEntry* entry = GetFdEntry(fd);
109 if (!entry) {
110 continue;
111 }
112
113 std::lock_guard<std::mutex> lock(entry->mutex);
114 if (entry->backtrace.empty()) {
115 continue;
116 }
117
118 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fd %d:", fd);
119 const size_t frame_skip = 2;
120 for (size_t i = frame_skip; i < entry->backtrace.size(); ++i) {
121 auto& frame = entry->backtrace[i];
122 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", " %zu: %s+%" PRIu64, i - frame_skip,
123 frame.function_name.c_str(), frame.function_offset);
124 }
125 }
126 android_fdtrack_set_enabled(prev);
127}