From e82102ec9194ea83092579a38e9a2bda00d9bc12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tr=C3=A1vn=C3=AD=C4=8Dek?= <jan.travnicek@fit.cvut.cz> Date: Fri, 28 Jan 2022 15:36:02 +0100 Subject: [PATCH] clang-tidy: fix simpleStacktrace issues --- alib2common/src/debug/simpleStacktrace.cpp | 36 +++++++++------------- alib2common/src/debug/simpleStacktrace.h | 3 +- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/alib2common/src/debug/simpleStacktrace.cpp b/alib2common/src/debug/simpleStacktrace.cpp index bb59178c87..91dbfa24b2 100644 --- a/alib2common/src/debug/simpleStacktrace.cpp +++ b/alib2common/src/debug/simpleStacktrace.cpp @@ -11,9 +11,10 @@ #include <link.h> #include <cstring> -#include <sstream> +#include <ext/sstream> #include <string> #include <map> +#include <filesystem> namespace ext { @@ -26,25 +27,15 @@ int callback(struct dl_phdr_info *info, size_t, void * data) { } #if defined DEBUG && defined BACKTRACE - void simpleStacktrace ( ext::ostream & out, unsigned int max_frames ) { - char linkname[512]; /* /proc/exe */ - char buf[512]; - pid_t pid; - int ret; - + void simpleStacktrace ( ext::ostream & out, int max_frames ) { /* Get our PID and build the name of the link in /proc */ - pid = getpid(); - snprintf(linkname, sizeof(linkname), "/proc/%i/exe", pid); + pid_t pid = getpid(); + auto linkname = std::filesystem::path("/proc") / std::to_string(pid) / "exe"; /* Now read the symbolic link */ - ret = readlink(linkname, buf, 512); - if ( ret < 0 ) { - strcpy(buf, "[UNKNOWN]"); - } else { - buf[ret] = 0; - } + std::filesystem::path procname = std::filesystem::read_symlink ( linkname ); - out << "stack trace for process " << buf << " (PID:" << pid << "):"<< std::endl; + out << "stack trace for process " << procname << " (PID:" << pid << "):" << std::endl; // storage array for stack trace address data void** addrlist = static_cast < void * * > ( malloc ( ( max_frames + 1 ) * sizeof ( * addrlist ) ) ); @@ -63,8 +54,8 @@ int callback(struct dl_phdr_info *info, size_t, void * data) { char** symbollist = backtrace_symbols(addrlist, addrlen); // allocate string which will be filled with the demangled function name - size_t funcnamesize = 256; - char* funcname = static_cast < char * > ( malloc ( funcnamesize * sizeof ( * funcname ) ) ); + size_t funcnamesize = 0; + char* funcname = nullptr; std::map<std::string, long> dlToBaseAddress; @@ -73,7 +64,10 @@ int callback(struct dl_phdr_info *info, size_t, void * data) { // iterate over the returned symbol lines. skip the first, it is the // address of this function. for (int i = 1; i < addrlen; ++i) { - char *begin_name = 0, *begin_offset = 0, *end_offset = 0, *addr_offset = 0; + char *begin_name = 0; + char *begin_offset = 0; + char *end_offset = 0; + char *addr_offset = 0; // find parentheses and +address offset surrounding the mangled name: // module(function+0x15c) [0x8048a6d] @@ -114,7 +108,7 @@ int callback(struct dl_phdr_info *info, size_t, void * data) { // __cxa_demangle(): int status; - char* demangled = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status); + char* demangled = abi::__cxa_demangle(begin_name, nullptr, &funcnamesize, &status); if (status == 0) { funcname = demangled; // use possibly realloc()-ed string out << " " << symbollist[i] << " : " << funcname << "+" << begin_offset << " " << begin_name << " [0x" << addr_offset_by_dl.c_str() << "; @ " << addr_offset << "]" << std::endl; @@ -133,7 +127,7 @@ int callback(struct dl_phdr_info *info, size_t, void * data) { free(addrlist); } #else - void simpleStacktrace ( std::ostream &, unsigned int ) { + void simpleStacktrace ( std::ostream &, int ) { } #endif diff --git a/alib2common/src/debug/simpleStacktrace.h b/alib2common/src/debug/simpleStacktrace.h index ac613c73c3..9d84e9552d 100644 --- a/alib2common/src/debug/simpleStacktrace.h +++ b/alib2common/src/debug/simpleStacktrace.h @@ -11,7 +11,8 @@ namespace ext { * \brief * Analyzes the backtrace (stack trace) and creates its sring representation, one function call per line. */ -void simpleStacktrace ( ext::ostream & out, unsigned int max_frames = 1000); + +void simpleStacktrace ( ext::ostream & out, int max_frames = 1000); // NOLINT(readability-magic-numbers) } /* namespace ext */ -- GitLab