Skip to content

Commit

Permalink
WIP: Make it possible to use nwind-capi as alternative unwinding backend
Browse files Browse the repository at this point in the history
  • Loading branch information
milianw committed Sep 20, 2023
1 parent 8f62b71 commit f934a22
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 2 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ else()
set(QT_MIN_VERSION 5.10.0)
endif()

option(
HEAPTRACK_USE_NWIND_CAPI
"Use nwind_capi for unwind, see https://github.com/koute/not-perf"
OFF
)

option(
HEAPTRACK_USE_LIBUNWIND
"Define preferred unwind functionality - Libunwind as ON and unwind_tables as OFF."
Expand Down Expand Up @@ -151,7 +157,9 @@ if (ECM_ENABLE_SANITIZERS)
endif()

if (HEAPTRACK_BUILD_TRACK)
if (HEAPTRACK_USE_LIBUNWIND)
if (HEAPTRACK_USE_NWIND_CAPI)
find_package(nwind_capi REQUIRED)
elseif (HEAPTRACK_USE_LIBUNWIND)
find_package(Libunwind REQUIRED)
endif()

Expand Down
20 changes: 20 additions & 0 deletions cmake/Findnwind_capi.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-License-Identifier: MIT
#
# - Try to find nwind_capi library
#
# This will define
# NWIND_CAPI_FOUND
# NWIND_CAPI_INCLUDE_DIRS
# NWIND_CAPI_LIBRARIES
#

find_path(NWIND_CAPI_INCLUDE_DIRS NAMES nwind_capi.h)
find_library(NWIND_CAPI_LIBRARIES NAMES nwind_capi)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
NWIND_CAPI DEFAULT_MSG
NWIND_CAPI_LIBRARIES NWIND_CAPI_INCLUDE_DIRS
)

mark_as_advanced(NWIND_CAPI_INCLUDE_DIRS NWIND_CAPI_LIBRARIES)
6 changes: 5 additions & 1 deletion src/track/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ install(PROGRAMS ${PROJECT_BINARY_DIR}/${BIN_INSTALL_DIR}/heaptrack
DESTINATION ${BIN_INSTALL_DIR}
)

if (HEAPTRACK_USE_LIBUNWIND)
if (HEAPTRACK_USE_NWIND_CAPI)
add_library(heaptrack_unwind STATIC trace_nwind_capi.cpp)
target_include_directories(heaptrack_unwind PRIVATE ${NWIND_CAPI_INCLUDE_DIRS})
target_link_libraries(heaptrack_unwind PRIVATE ${NWIND_CAPI_LIBRARIES})
elseif (HEAPTRACK_USE_LIBUNWIND)
add_library(heaptrack_unwind STATIC trace_libunwind.cpp)
target_include_directories(heaptrack_unwind PRIVATE ${LIBUNWIND_INCLUDE_DIRS})
target_link_libraries(heaptrack_unwind PRIVATE ${LIBUNWIND_LIBRARIES})
Expand Down
1 change: 1 addition & 0 deletions src/track/libheaptrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ class HeapTrack
return;
}
s_data->moduleCacheDirty = true;
Trace::invalidateModuleCache();
}

void writeTimestamp()
Expand Down
1 change: 1 addition & 0 deletions src/track/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct Trace
}

static void setup();
static void invalidateModuleCache();

static void print();

Expand Down
4 changes: 4 additions & 0 deletions src/track/trace_libunwind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ void Trace::setup()
#endif
}

void Trace::invalidateModuleCache()
{
}

int Trace::unwind(void** data)
{
return unw_backtrace(data, MAX_SIZE);
Expand Down
88 changes: 88 additions & 0 deletions src/track/trace_nwind_capi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
SPDX-FileCopyrightText: 2014-2019 Milian Wolff <[email protected]>
SPDX-License-Identifier: LGPL-2.1-or-later
*/

/**
* @brief A libunwind based backtrace.
*/

#include "trace.h"

#include <cstdio>

#include <nwind_capi.h>

namespace {
struct AddressSpace
{
AddressSpace()
: handle(nwind_create_local_address_space())
{
nwind_local_address_space_use_shadow_stack(handle, 1);
}

~AddressSpace()
{
nwind_free_local_address_space(handle);
}

nwind_local_address_space* const handle;
};

nwind_local_address_space* addressSpace()
{
static AddressSpace addressSpace;
return addressSpace.handle;
}

struct UnwindContext
{
UnwindContext()
: handle(nwind_create_local_unwind_context())
{
}

~UnwindContext()
{
nwind_free_local_unwind_context(handle);
}

nwind_local_unwind_context* const handle;
};

nwind_local_unwind_context* unwindContext()
{
static thread_local UnwindContext unwindContext;
return unwindContext.handle;
}
}

void Trace::print()
{
Trace trace;
if (!trace.fill(1)) {
return;
}

unsigned frameNr = 0;
for (auto ip : trace) {
++frameNr;
fprintf(stderr, "#%-2u %p\n", frameNr, ip);
}
}

void Trace::setup()
{
}

void Trace::invalidateModuleCache()
{
nwind_reload_local_address_space(addressSpace());
}

int Trace::unwind(void** data)
{
return nwind_local_backtrace(addressSpace(), unwindContext(), data, MAX_SIZE);
}
4 changes: 4 additions & 0 deletions src/track/trace_unwind_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ void Trace::setup()
{
}

void Trace::invalidateModuleCache()
{
}

void Trace::print()
{
Trace trace;
Expand Down

0 comments on commit f934a22

Please sign in to comment.