From 6717b699179d970aacbdb0bad6074c7442cc3338 Mon Sep 17 00:00:00 2001 From: Jozsef Bakosi Date: Sun, 6 May 2018 09:59:23 -0600 Subject: [PATCH 1/6] Make cmake append libs dwarf and elf Without this they are not correctly included in BACKWARD_LIBRARIES even when they are found. --- BackwardConfig.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BackwardConfig.cmake b/BackwardConfig.cmake index 98b2863..3d9b777 100644 --- a/BackwardConfig.cmake +++ b/BackwardConfig.cmake @@ -133,7 +133,7 @@ else() endif() if (STACK_DETAILS_DWARF) - LIST(APPEND BACKWARD_LIBRARIES dwarf elf) + LIST(APPEND _BACKWARD_LIBRARIES dwarf elf) endif() endif() From dd57d9ed6dafffa34aaafb77bbdc65e8f79f91c3 Mon Sep 17 00:00:00 2001 From: Jozsef Bakosi Date: Sun, 6 May 2018 10:04:21 -0600 Subject: [PATCH 2/6] Find libdwarf.h on debian Debian installs libdwarf.h at /usr/include/libdwarf/. This change enables cmake finding this header on debian. --- BackwardConfig.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BackwardConfig.cmake b/BackwardConfig.cmake index 98b2863..8aec29f 100644 --- a/BackwardConfig.cmake +++ b/BackwardConfig.cmake @@ -72,7 +72,7 @@ if (${STACK_DETAILS_AUTO_DETECT}) LIBDL_INCLUDE_DIR LIBDL_LIBRARY) # find libdwarf - find_path(LIBDWARF_INCLUDE_DIR NAMES "libdwarf.h") + find_path(LIBDWARF_INCLUDE_DIR NAMES "libdwarf.h" PATH_SUFFIXES libdwarf) find_path(LIBELF_INCLUDE_DIR NAMES "libelf.h") find_path(LIBDL_INCLUDE_DIR NAMES "dlfcn.h") find_library(LIBDWARF_LIBRARY dwarf) From 157253254cc6364d6c20713a707c5e2ad8634487 Mon Sep 17 00:00:00 2001 From: Jozsef Bakosi Date: Sun, 6 May 2018 10:18:48 -0600 Subject: [PATCH 3/6] Export each definition individually This allows using each definition (resulting from BackwardConfig.cmake) separately, e.g., in conjunction with cmake's configure_file() command and thus can be defined in a cmake-generated source file before a build. Example: Config.h.in =========== #cmakedefine01 BACKWARD_HAS_UNWIND #cmakedefine01 BACKWARD_HAS_BACKTRACE #cmakedefine01 BACKWARD_HAS_BACKTRACE_SYMBOL #cmakedefine01 BACKWARD_HAS_DW #cmakedefine01 BACKWARD_HAS_BFD #cmakedefine01 BACKWARD_HAS_DWARF CMakeLists.txt: =============== include(BackwardConfig) configure_file( "${PROJECT_SOURCE_DIR}/Config.h.in" "${PROJECT_BINARY_DIR}/Config.h" ) SomeProjectFile.C: ================== #include "Config.h" // generated by cmake based on Config.h.in #include "backward.hpp" // backward-cpp now configured based on BackwardConfig.cmake using namespace backward; StackTrace st; st.load_here(32); Printer p; p.print(st); --- BackwardConfig.cmake | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/BackwardConfig.cmake b/BackwardConfig.cmake index 98b2863..5f198ba 100644 --- a/BackwardConfig.cmake +++ b/BackwardConfig.cmake @@ -180,6 +180,16 @@ set(BACKWARD_DEFINITIONS ${_BACKWARD_DEFINITIONS} CACHE INTERNAL "BACKWARD_DEFIN set(BACKWARD_LIBRARIES ${_BACKWARD_LIBRARIES} CACHE INTERNAL "BACKWARD_LIBRARIES") mark_as_advanced(BACKWARD_INCLUDE_DIRS BACKWARD_DEFINITIONS BACKWARD_LIBRARIES) +# Expand each definition in BACKWARD_DEFINITIONS to its own cmake var and export +# to outer scope +foreach(var ${BACKWARD_DEFINITIONS}) + string(REPLACE "=" ";" var_as_list ${var}) + list(GET var_as_list 0 var_name) + list(GET var_as_list 1 var_value) + set(${var_name} ${var_value}) + mark_as_advanced(${var_name}) +endforeach() + if (NOT TARGET Backward::Backward) add_library(Backward::Backward INTERFACE IMPORTED) set_target_properties(Backward::Backward PROPERTIES From 0b6b222bb6e302ae4ec98875ac1ed1c5787cd08a Mon Sep 17 00:00:00 2001 From: Pedro Navarro Date: Mon, 7 May 2018 10:02:51 -0700 Subject: [PATCH 4/6] Updated README.md with libelf/libdwarf info Added information about libdwarf/libelf and additional API compatible implementations that could potentially be used. --- README.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c2db43..0eaab31 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,29 @@ Of course you can simply add the define (`-DBACKWARD_HAS_...=1`) and the linkage details in your build system and even auto-detect which library is installed, it's up to you. -That'ss it, you are all set, you should be getting nice stack traces like the +#### [libdwarf](https://sourceforge.net/projects/libdwarf/) and [libelf](http://www.mr511.de/software/english.html) + + apt-get install libdwarf-dev (or equivalent) + +And do not forget to link with the lib and inform Backward to use it: + + #define BACKWARD_HAS_DWARF 1 + +There are several alternative implementations of libdwarf and libelf that +are API compatible so it's possible, although it hasn't been tested, to +replace the ones used when developing backward (in bold, below): + +* **_libelf_** by [Michael "Tired" Riepe](http://www.mr511.de/software/english.html) +* **_libdwarf_** by [David Anderson](https://www.prevanders.net/dwarf.html) +* libelf from [elfutils](https://fedorahosted.org/elfutils/) +* libelf and libdwarf from FreeBSD's [ELF Tool Chain](https://sourceforge.net/p/elftoolchain/wiki/Home/) project + + +Of course you can simply add the define (`-DBACKWARD_HAS_...=1`) and the +linkage details in your build system and even auto-detect which library is +installed, it's up to you. + +That's it, you are all set, you should be getting nice stack traces like the one at the beginning of this document. ## API From 812e64f6bb41d09f2dd823590b4305fe7817ec63 Mon Sep 17 00:00:00 2001 From: Pierre Kestener Date: Wed, 9 May 2018 10:51:36 +0200 Subject: [PATCH 5/6] try to fix add_backward macro --- BackwardConfig.cmake | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/BackwardConfig.cmake b/BackwardConfig.cmake index 5650437..6cff86c 100644 --- a/BackwardConfig.cmake +++ b/BackwardConfig.cmake @@ -169,10 +169,8 @@ list(APPEND _BACKWARD_INCLUDE_DIRS ${BACKWARD_INCLUDE_DIR}) macro(add_backward target) target_include_directories(${target} PRIVATE ${BACKWARD_INCLUDE_DIRS}) - set_property(TARGET ${target} APPEND PROPERTY COMPILE_DEFINITIONS ${_BACKWARD_DEFINITIONS}) - if(BACKWARD_HAS_EXTERNAL_LIBRARIES) - set_property(TARGET ${target} APPEND PROPERTY LINK_LIBRARIES ${_BACKWARD_LIBRARIES}) - endif() + set_property(TARGET ${target} APPEND PROPERTY COMPILE_DEFINITIONS ${BACKWARD_DEFINITIONS}) + set_property(TARGET ${target} APPEND PROPERTY LINK_LIBRARIES ${BACKWARD_LIBRARIES}) endmacro() set(BACKWARD_INCLUDE_DIRS ${_BACKWARD_INCLUDE_DIRS} CACHE INTERNAL "_BACKWARD_INCLUDE_DIRS") From e304e655f65d760b504b3cdcfde1bd78f78bb547 Mon Sep 17 00:00:00 2001 From: Alexei Drake Date: Thu, 28 Jun 2018 12:41:53 +0200 Subject: [PATCH 6/6] Fix -Wold-style-cast compilation warnings by using static_cast and reinterpret_cast --- backward.hpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/backward.hpp b/backward.hpp index 237eb32..3bf648c 100644 --- a/backward.hpp +++ b/backward.hpp @@ -631,11 +631,11 @@ protected: void load_thread_info() { #ifdef BACKWARD_SYSTEM_LINUX #ifndef __ANDROID__ - _thread_id = (size_t)syscall(SYS_gettid); + _thread_id = static_cast(syscall(SYS_gettid)); #else - _thread_id = (size_t)gettid(); + _thread_id = static_cast(gettid()); #endif - if (_thread_id == (size_t) getpid()) { + if (_thread_id == static_cast(getpid())) { // If the thread is the main one, let's hide that. // I like to keep little secret sometimes. _thread_id = 0; @@ -701,7 +701,7 @@ private: static _Unwind_Reason_Code backtrace_trampoline( _Unwind_Context* ctx, void *self) { - return ((Unwinder*)self)->backtrace(ctx); + return (static_cast(self))->backtrace(ctx); } _Unwind_Reason_Code backtrace(_Unwind_Context* ctx) { @@ -721,7 +721,7 @@ private: } if (_index >= 0) { // ignore first frame. - (*_f)(_index, (void*)ip); + (*_f)(_index, reinterpret_cast(ip)); } _index += 1; return _URC_NO_REASON; @@ -911,7 +911,7 @@ class TraceResolverLinuxImpl: if(len < 0) { return ""; } - if ((size_t)len == path.size()) { + if (static_cast(len) == path.size()) { path.resize(path.size() * 2); } else { @@ -1163,7 +1163,7 @@ private: if (symtab_storage_size > 0) { symtab.reset( - (bfd_symbol**) malloc(symtab_storage_size) + static_cast(malloc(symtab_storage_size)) ); symcount = bfd_canonicalize_symtab( bfd_handle.get(), symtab.get() @@ -1172,7 +1172,7 @@ private: if (dyn_symtab_storage_size > 0) { dynamic_symtab.reset( - (bfd_symbol**) malloc(dyn_symtab_storage_size) + static_cast(malloc(dyn_symtab_storage_size)) ); dyn_symcount = bfd_canonicalize_dynamic_symtab( bfd_handle.get(), dynamic_symtab.get() @@ -1214,7 +1214,7 @@ private: context.base_addr = base_addr; context.result.found = false; bfd_map_over_sections(fobj.handle.get(), &find_in_section_trampoline, - (void*)&context); + static_cast(&context)); return context.result; } @@ -3665,7 +3665,7 @@ public: bool success = true; const size_t stack_size = 1024 * 1024 * 8; - _stack_content.reset((char*)malloc(stack_size)); + _stack_content.reset(static_cast(malloc(stack_size))); if (_stack_content) { stack_t ss; ss.ss_sp = _stack_content.get(); @@ -3697,7 +3697,7 @@ public: bool loaded() const { return _loaded; } static void handleSignal(int, siginfo_t* info, void* _ctx) { - ucontext_t *uctx = (ucontext_t*) _ctx; + ucontext_t *uctx = static_cast(_ctx); StackTrace st; void* error_addr = 0;