From 748618d97d3cec1cf5b011c2ec421732049a79be Mon Sep 17 00:00:00 2001 From: Bojun Seo Date: Mon, 6 Nov 2023 10:11:38 +0900 Subject: [PATCH] stacktrace: Implement is_ignored function is_ignored function can be used to check whether the input `report` contains any ignore string. Ignore string can be read from the given file. Each line of the file is considered as ignore string. Ignore string could be function name, library name as well as something you want. Ignore file example: $ cat ignore.txt libfoo bar baz Signed-off-by: Bojun Seo --- src/stacktrace.cc | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/stacktrace.cc b/src/stacktrace.cc index 6f14552..1b6309c 100644 --- a/src/stacktrace.cc +++ b/src/stacktrace.cc @@ -17,7 +17,6 @@ #include #include #include - #include #include "compiler.h" @@ -33,9 +32,40 @@ std::map stackmap; std::map addrmap; +std::vector ignorevec; +bool ignorevec_initialized = false; std::recursive_mutex container_mutex; +static void lazyinit_ignorevec() +{ + if (ignorevec_initialized) + return; + + opts.ignore = getenv("HEAPTRACE_IGNORE"); + if (opts.ignore) { + std::ifstream file(opts.ignore); + if (file.is_open()) { + std::string line; + while (std::getline(file, line)) { + ignorevec.push_back(line); + } + file.close(); + } + else { + pr_out("Failed to open file %s\n", opts.ignore); + } + } + ignorevec_initialized = true; +} + +static bool is_ignored(const std::string &report) +{ + lazyinit_ignorevec(); + return std::any_of(ignorevec.begin(), ignorevec.end(), [&report](const std::string& s) + { return report.find(s) != std::string::npos; }); +} + // record_backtrace() is defined in stacktrace.h as an inline function. void __record_backtrace(size_t size, void *addr, stack_trace_t &stack_trace, int nptrs) {