Add working execinfo backtrace, testing boost backtrace
Some checks failed
buildbot/Ascendent (FreeBSD) Build done.
buildbot/Ascendent (Alpine) Build done.

This commit is contained in:
S David 2022-08-24 18:46:30 -04:00
parent 09c4f04cf3
commit dcdc3ab3e0
7 changed files with 127 additions and 10 deletions

View File

@ -94,12 +94,12 @@ AC_LANG_POP
dnl Check if we have a libexecinfo.
dnl On non-glibc systems, using backtrace requires linking to libexecinfo.o
dnl AC_CHECK_HEADERS([execinfo.h])
dnl AC_CHECK_LIB([execinfo], [backtrace],
dnl [LIBS="${LIBS} -lexecinfo"; BOOST_FIND_HEADER([boost/stacktrace.hpp])])
dnl
dnl AC_CHECK_FUNCS([backtrace backtrace_symbols])
dnl
dnl AC_CHECK_LIB([pthread], [pthread_create])
AC_CHECK_HEADERS([execinfo.h])
AC_CHECK_LIB([execinfo], [backtrace],
[LIBS="${LIBS} -lexecinfo"; BOOST_FIND_HEADER([boost/stacktrace.hpp])])
AC_CHECK_FUNCS([backtrace backtrace_symbols])
AC_CHECK_LIB([pthread], [pthread_create])
AC_OUTPUT

View File

@ -4,7 +4,7 @@ if HAVE_COMPILEDB
@abs_top_srcdir@/compile_commands.json:
rm -f @abs_top_srcdir@/compile_commands.json
make clean
compiledb `basename ${MAKE}` -j 1 all
compiledb `basename ${MAKE}` -j 1 all check
ln @top_builddir@/compile_commands.json @abs_top_srcdir@
phony_targets+= vim-completion

88
src/engine/debuginfo.hpp Normal file
View File

@ -0,0 +1,88 @@
/* debuginfo.h
* Copyright © 2020-2022 Saul D. Beniquez
* License: Mozilla Public License v. 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "config.h"
//#include "engine/types.hpp"
#ifndef HAVE_EXECINFO_H
#include <execinfo.h>
#else
#include <boost/stacktrace.hpp>
#endif
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#ifdef QW_DEBUG //#region
#define debugprint(msg) std::cout << "#*!* " << msg << std::endl
#else
#define debugprint(msg) ;
#endif //#endregion
using stacktrace = std::vector<const std::string>;
std::ostream& operator<<(std::ostream& stream, stacktrace trace)
{
for ( auto& line : trace ) {
stream << line << '\n';
}
stream << std::flush;
return stream;
}
inline stacktrace
get_backtrace()
{
stacktrace result;
#ifndef HAVE_EXECINFO_H
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
// start at i = 1 because we don't want to see get_backtrace() in stack
for (i = 1; i < frames; ++i) {
result.push_back(strs[i]);
}
free(strs);
#else
std::stringstream buffer;
buffer << boost::stacktrace::stacktrace();
for (std::string line; std::getline(buffer, line, '\n'); ) {
result.push_back(line);
}
#endif
return result;
}
inline void
print_backtrace(std::exception& ex)
{
std::cout << "caught: " << ex.what() << std::endl;
//print_backtrace();
}
inline void
print_cmdline(int argc, char* argv[])
{
int i;
std::cout << "Command-line received" << std::endl;
for (i = 0; i < argc; ++i)
std::cout << argv[i] << " ";
std::cout << std::endl;
}
// clang-format off
// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :

View File

@ -9,8 +9,11 @@
#include <iostream>
#include "engine/debuginfo.hpp"
int main(int argc, char* argv[])
{
print_cmdline(argc, argv);
std::cout << "Hello World" << std::endl;
return 0;
}

View File

@ -19,7 +19,9 @@ app_test_SOURCES=
app_test_LDADD=
app_test_CXXFLAGS=
app_test_CPPFLAGS=
app_test_SOURCES+=src/tests/runtime.tests.cpp
app_test_SOURCES+=src/tests/debuginfo.tests.cpp
app_test_CXXFLAGS+= ${AM_CXXFLAGS} ${test_disabled_warnings}

View File

@ -0,0 +1,24 @@
/* debuginfo.tests.cpp
* Copyright © 2022 Saul D. Beniquez
* License: Mozilla Public License v. 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v.2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "test-includes.hpp"
#include "src/engine/debuginfo.hpp"
TEST_CASE("get_backtrace() produces something reasonable on the supported platforms", "test")
{
REQUIRE_NOTHROW(get_backtrace());
stacktrace stack_one = get_backtrace();
std::cout << stack_one << std::endl;
}
// clang-format off
// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :

View File

@ -10,7 +10,7 @@
#pragma once
#include <catch2/catch_test_macros.hpp>
#include <fakeit.hpp>
#include "fakeit.hpp"
#define BEGIN_TEST_SUITE(name)\
namespace {\