From 5fe9abc4316ef20c2c2fcae9806e7da544a9e018 Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Fri, 24 Mar 2023 15:54:56 -0700 Subject: [PATCH] Store leaked stacks in memory Signed-off-by: Alan Jowett --- libs/platform/user/ebpf_leak_detector.cpp | 20 +++++++++++++++----- libs/platform/user/ebpf_leak_detector.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/libs/platform/user/ebpf_leak_detector.cpp b/libs/platform/user/ebpf_leak_detector.cpp index 91f1bc0646..5f7ad0a2c8 100644 --- a/libs/platform/user/ebpf_leak_detector.cpp +++ b/libs/platform/user/ebpf_leak_detector.cpp @@ -5,6 +5,7 @@ #include "ebpf_symbol_decoder.h" #include +#include void _ebpf_leak_detector::register_allocation(uintptr_t address, size_t size) @@ -37,22 +38,31 @@ _ebpf_leak_detector::dump_leaks() { std::unique_lock lock(_mutex); for (auto& allocation : _allocations) { + std::ostringstream output; std::vector stack = _stack_hashes[allocation.second.stack_hash]; - std::cout << "Leak of " << allocation.second.size << " bytes at " << allocation.second.address << std::endl; + output << "Leak of " << allocation.second.size << " bytes at " << allocation.second.address << std::endl; + _in_memory_log.push_back(output.str()); + std::cout << output.str(); + output.str(""); std::string name; uint64_t displacement; std::optional line_number; std::optional file_name; for (auto address : stack) { if (_ebpf_decode_symbol(address, name, displacement, line_number, file_name) == EBPF_SUCCESS) { - std::cout << " " << name << " + " << displacement; + output << " " << name << " + " << displacement; if (line_number.has_value() && file_name.has_value()) { - std::cout << " (" << file_name.value() << ":" << line_number.value() << ")"; + output << " (" << file_name.value() << ":" << line_number.value() << ")"; } - std::cout << std::endl; + output << std::endl; } + _in_memory_log.push_back(output.str()); + std::cout << output.str(); + output.str(""); } - std::cout << std::endl; + _in_memory_log.push_back(output.str()); + std::cout << output.str(); + output.str(""); } // assert to make sure that a leaking test throws an exception thereby failing the test. diff --git a/libs/platform/user/ebpf_leak_detector.h b/libs/platform/user/ebpf_leak_detector.h index 11fc7cbb02..3c6d41f5cc 100644 --- a/libs/platform/user/ebpf_leak_detector.h +++ b/libs/platform/user/ebpf_leak_detector.h @@ -35,6 +35,7 @@ typedef class _ebpf_leak_detector std::unordered_map _allocations; std::mutex _mutex; const size_t _stack_depth = 32; + std::vector _in_memory_log; } ebpf_leak_detector_t; typedef std::unique_ptr ebpf_leak_detector_ptr; \ No newline at end of file