Skip to content

Commit

Permalink
Test backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNicker committed Oct 20, 2023
1 parent 2330516 commit ddd9bd8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ project(FreeTypeWrapper)
option(FREETYPE_WRAPPER_BUILD_SAMPLES "build freetype wrapper sample" ON)
option(FREETYPE_WRAPPER_BUILD_FRIBIDI "build fribidi" ON)

add_link_options(-rdynamic)

if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
add_compile_definitions(NOMINMAX _CRT_SECURE_NO_WARNINGS)
Expand Down
18 changes: 14 additions & 4 deletions Test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SOFTWARE.
#include <LLUtils/Colors.h>
#include <LLUtils/Exception.h>
#include "xxh3.h"
#include "backtrace.h"

std::filesystem::path folderToSaveFiles = "./testImages/";
#if LLUTILS_PLATFORM == LLUTILS_PLATFORM_WIN32
Expand Down Expand Up @@ -74,10 +75,11 @@ void runTest(FreeType::FreeTypeConnector& freeType, FreeType::TextCreateParams f
using namespace FreeType;
using namespace LLUtils;
FreeTypeConnector::Bitmap textBitmap;

freeType.CreateBitmap(freetypeParams, textBitmap, nullptr);

throw std::logic_error("234");
[[maybe_unused]] auto hash = XXH3_64bits(static_cast<const void*>(textBitmap.buffer.data()), textBitmap.height * textBitmap.rowPitch);

if (testParams.saveToFile)
SaveToFile(textBitmap, testParams.fileName);

Expand All @@ -86,8 +88,14 @@ void runTest(FreeType::FreeTypeConnector& freeType, FreeType::TextCreateParams f

}

void on_terminate()
{
std::cout << backtrace() << std::endl;
}

int runtests()
{
std::set_terminate(on_terminate);
using namespace FreeType;
using namespace LLUtils;
FreeTypeConnector freeType;
Expand Down Expand Up @@ -280,15 +288,17 @@ int runtests()
int main()
{

try
//try
{
if (runtests() == 0)
std::cout << "All tests passed successfully.";


}
catch (...)
//catch (...)
{
std::cout << "One or more of the test have failed.";
//std::cout << backtrace(0) << std::endl;
return 1;
}

Expand Down
67 changes: 67 additions & 0 deletions Test/backtrace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <execinfo.h> /* backtrace, backtrace_symbols */
#include <stdio.h> /* printf */
#include <dlfcn.h> // for dladdr
#include <cxxabi.h> // for __cxa_demangle
#include <elf.h>
#include <cstdio>
#include <string>
#include <sstream>
#include <iostream>

// This function produces a stack backtrace with demangled function & method names.
std::string backtrace(int skip = 1)
{
void *callstack[128];
const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]);
char buf[1024];
int nFrames = backtrace(callstack, nMaxFrames);
char **symbols = backtrace_symbols(callstack, nFrames);

std::ostringstream trace_buf;
for (int i = skip; i < nFrames; i++) {
Dl_info info;
Elf64_Sym* sym;
if (dladdr1(callstack[i], &info,(void**)&sym ,RTLD_DL_SYMENT)) {
char *demangled = NULL;
int status{};
size_t size{};
demangled = abi::__cxa_demangle(info.dli_sname, nullptr, &size, &status);
std::string functionName;
if (status == 0)
{
auto buffer = std::make_unique<char[]>(size);
functionName = abi::__cxa_demangle(info.dli_sname, buffer.get(), &size, &status);
}
else
{
if (info.dli_sname != nullptr)
functionName = info.dli_sname;
}




std::snprintf(
buf,
sizeof(buf),
"%-3d %*p %s + %zd\n",
i,
(int)(2 + sizeof(void*) * 2),
callstack[i],
functionName.c_str(),
(char *)callstack[i] - (char *)info.dli_saddr
);
free(demangled);
} else {
std::snprintf(buf, sizeof(buf), "%-3d %*p\n",
i, (int)(2 + sizeof(void*) * 2), callstack[i]);
}
trace_buf << buf;
std::snprintf(buf, sizeof(buf), "%s\n", symbols[i]);
trace_buf << buf;
}
free(symbols);
if (nFrames == nMaxFrames)
trace_buf << "[truncated]\n";
return trace_buf.str();
}

0 comments on commit ddd9bd8

Please sign in to comment.