Skip to content

Commit

Permalink
Added a function to get the call stack
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuponitskiy-duality committed Oct 2, 2023
1 parent 579e12b commit 5e4ee6b
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/core/include/utils/get-call-stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2023, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: contact@openfhe.org
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
#ifndef __GET_CALL_STACK_H__
#define __GET_CALL_STACK_H__

#if defined(__clang__) || defined(__GNUG__)
#include <string>
#include <vector>

/**
* @brief get_call_stack() is a function to get the call stack
* @return a vector with call stack (demangled function names)
*/
std::vector<std::string> get_call_stack();
#endif

#endif // __GET_CALL_STACK_H__

8 changes: 4 additions & 4 deletions src/core/lib/utils/demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
#if defined(__clang__) || defined(__GNUG__)
#include <cxxabi.h>

std::string demangle(const char* name) {
std::string demangle(const char* const name) {
int status = -1;
std::unique_ptr<char, void (*)(void*)> result{abi::__cxa_demangle(name, NULL, NULL, &status), std::free};
std::unique_ptr<char> result{abi::__cxa_demangle(name, NULL, NULL, &status)};

return (status == 0) ? result.get() : "UNKNOWN";
return (status == 0) ? result.get() : (std::string("Can not demangle symbol: ") + name);
}
#else
std::string demangle(const char* name) {
std::string demangle(const char* const name) {
return name;
}
#endif
87 changes: 87 additions & 0 deletions src/core/lib/utils/get-call-stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2023, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: contact@openfhe.org
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
#if defined(__clang__) || defined(__GNUG__)
#include "utils/get-call-stack.h"
#include "utils/demangle.h"

#include <execinfo.h>
#include <cxxabi.h>
#include <memory>

namespace {
enum { MAX_BACKTRACE_ADDRESSES = 512 };
}

static bool stringEmpty(const std::string& str) {
if(!str.length())
return true;

// str is not empty if it has any printable character
for(const char c : str) {
if(c >= 33 && c <= 126)
return false;
}

return true;
}

std::vector<std::string> get_call_stack() {
void* bt_buffer[MAX_BACKTRACE_ADDRESSES] = {NULL};
const int n = backtrace(bt_buffer, MAX_BACKTRACE_ADDRESSES);
if(n < 1) {
return std::vector<std::string>();
}
const std::unique_ptr<char*> symbols(backtrace_symbols(bt_buffer, n));

const size_t numSymbols = static_cast<size_t>(n);
std::vector<std::string> ret(numSymbols);
for( size_t i = 0; i < numSymbols; ++i ) {
std::string symbol(symbols.get()[i]);
// we need to get rid of anything that doesn't belong to the name
// Mangled symbol examples:
// ./lib/libOPENFHEcore.so.1(_Z14get_call_stackB5cxx11v+0x35) [0x7f1b5cdb91d5]
// ./unittest/pke_tests(_ZN8lbcrypto10FirstPrimeIN6intnat14NativeIntegerTImEEEET_mm+0x111) [0x5626d875c1d1]
// /lib/libOPENFHEpke.so.1(_ZNK8lbcrypto25ParameterGenerationBGVRNS15ParamsGenBGVRNSESt10shared_ptrINS_20CryptoParametersBaseINS_12DCRTPolyImplIN9bigintdyn9mubintvecINS4_5ubintImEEEEEEEEEjjjjjjjj+0x44a) [0x7f1b5cf6a09a]
// 1. we may have "+", so we search to find the last one to trim "symbol" from the right
size_t pos = symbol.find_last_of("+");
symbol = symbol.substr(0, pos);
// 2. find the last "(" which indicates the beginning of the actual mangled symbol enclosed in to "()"
pos = symbol.find_last_of("(");
size_t newLen = symbol.length() - pos;
std::string mangledName(symbol.substr(pos+1, newLen));

ret[i] = (stringEmpty(mangledName)) ? symbols.get()[i] : demangle(mangledName.c_str());
}

return ret;
}

#endif

0 comments on commit 5e4ee6b

Please sign in to comment.