Skip to content

Commit fdcce86

Browse files
authored
fixes #10 - more portable linking of libbfd (#11)
* more portable linking of libbfd * add optional linux dependency binutils-dev to build script
1 parent 1544bd0 commit fdcce86

File tree

5 files changed

+16
-31
lines changed

5 files changed

+16
-31
lines changed

CMakeLists.txt

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Project
22
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
33
project(heapusage VERSION 1.0 LANGUAGES C CXX)
4-
set(COMMON_FLAGS "-funwind-tables -g -Wall -Wextra -Wpedantic -Wshadow -Wpointer-arith -Wcast-qual \
5-
-Wno-missing-braces -Wswitch-default -Wcast-align -Wunreachable-code -Wundef \
6-
-Wuninitialized")
4+
set(COMMON_FLAGS "-funwind-tables -g -Wall -Wextra -Wpedantic -Wshadow -Wpointer-arith \
5+
-Wcast-qual -Wno-missing-braces -Wswitch-default -Wcast-align \
6+
-Wunreachable-code -Wuninitialized")
77
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 ${COMMON_FLAGS} -Wstrict-prototypes \
88
-Wmissing-prototypes")
99
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_FLAGS}")
@@ -13,22 +13,19 @@ set(CMAKE_CXX_STANDARD 11)
1313
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1414
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1515

16-
# Optional dependency - binutils / binutils-dev - for filename/line-numbers in callstacks
17-
include(FindPackageHandleStandardArgs)
18-
find_path(BFD_INCLUDE_PATH bfd.h PATH /usr/include /usr/local/include /usr/local/opt/*/include)
19-
find_library(BFD_LIBRARIES bfd PATH /lib /usr/lib /usr/local/lib /usr/local/opt/*/lib)
20-
find_package_handle_standard_args(BFD DEFAULT_MSG BFD_LIBRARIES BFD_INCLUDE_PATH)
21-
if(BFD_FOUND)
22-
set(EXTRA_LIBRARIES "${BFD_LIBRARIES}")
23-
include_directories("ext/backward-cpp" ${BFD_INCLUDE_DIR})
24-
add_definitions(-DHAS_BFD)
25-
endif()
26-
2716
# Library
2817
add_library(heapusage SHARED src/humain.cpp src/hulog.cpp src/humalloc.cpp)
2918
target_compile_features(heapusage PRIVATE cxx_variadic_templates)
3019
install(TARGETS heapusage LIBRARY DESTINATION lib)
31-
target_link_libraries(heapusage ${EXTRA_LIBRARIES} pthread dl)
20+
target_link_libraries(heapusage pthread dl)
21+
22+
# Dependency backward-cpp providing more detailed stacktraces on Linux, when
23+
# either of the following is present (only ONE needed):
24+
# libbfd - sudo apt install binutils-dev
25+
# libdw - sudo apt install libdw-dev
26+
# libdwarf - sudo apt install libdwarf-dev
27+
add_subdirectory(ext/backward-cpp)
28+
add_backward(heapusage)
3229

3330
# Utility
3431
configure_file(src/heapusage ${CMAKE_CURRENT_BINARY_DIR}/heapusage COPYONLY)

make.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if [[ "${DEPS}" == "1" ]]; then
7070
if [ "${OS}" == "Linux" ]; then
7171
DISTRO="$(lsb_release -i | awk -F':\t' '{print $2}')"
7272
if [[ "${DISTRO}" == "Ubuntu" ]]; then
73-
true || exiterr "deps failed (linux), exiting."
73+
sudo apt update && sudo apt -y install build-essential cmake binutils-dev || exiterr "deps failed (linux), exiting."
7474
else
7575
exiterr "deps failed (unsupported linux distro ${DISTRO}), exiting."
7676
fi

src/heapusage

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ showusage()
4646

4747
showversion()
4848
{
49-
echo "heapusage v2.01"
49+
echo "heapusage v2.02"
5050
echo ""
5151
echo "Copyright (C) 2017-2021 Kristofer Berggren"
5252
echo ""

src/heapusage.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
2-
.TH HEAPUSAGE "1" "November 2021" "heapusage v2.01" "User Commands"
2+
.TH HEAPUSAGE "1" "December 2021" "heapusage v2.02" "User Commands"
33
.SH NAME
44
heapusage \- find memory leaks in applications
55
.SH SYNOPSIS

src/hulog.cpp

+1-13
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,11 @@
2626
#include <string>
2727
#include <vector>
2828

29-
#ifdef HAS_BFD
30-
/* Silence warnings */
31-
#define BACKWARD_HAS_LIBUNWIND 0
32-
#define BACKWARD_HAS_UNWIND 0
33-
#define BACKWARD_HAS_BACKTRACE 1
34-
#define BACKWARD_HAS_BACKTRACE_SYMBOL 1
35-
#define BACKWARD_HAS_DW 0
36-
#define BACKWARD_HAS_DWARF 0
3729
#if defined(__APPLE__)
3830
#define _XOPEN_SOURCE 0
3931
#define _POSIX_C_SOURCE 0
4032
#endif
41-
42-
/* Backward cpp with bfd / binutils */
43-
#define BACKWARD_HAS_BFD 1
4433
#include "backward.hpp"
45-
#endif
4634

4735
#include "hulog.h"
4836
#include "humain.h"
@@ -499,7 +487,7 @@ static std::string addr_to_symbol(void *addr)
499487
}
500488
else
501489
{
502-
#ifdef HAS_BFD
490+
#if (BACKWARD_HAS_BFD == 1) || (BACKWARD_HAS_DW == 1) || (BACKWARD_HAS_DWARF == 1)
503491
backward::TraceResolver trace_resolver;
504492
trace_resolver.load_addresses(&addr, 1);
505493
backward::Trace trace(addr, 0);

0 commit comments

Comments
 (0)