Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mlpack::backtrace utility. #535

Merged
merged 29 commits into from Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e671aa8
Add mlpack::backtrace for Ubuntu (C++, Clang).
Kirizaki Feb 24, 2016
c827d59
Commit mlpack::backtrace.
Kirizaki Feb 24, 2016
e9ee6bd
Update for mlpack::backtrace
Kirizaki Feb 25, 2016
bce8a34
Delete old backtrace depend.
Kirizaki Feb 25, 2016
aa35528
Add mlpack::backtrace info.
Kirizaki Feb 25, 2016
5f430cd
Add mlpack::backtrace header.
Kirizaki Feb 25, 2016
8cbd0d5
Add mlpack::backtrace
Kirizaki Feb 25, 2016
1404c1d
Add mlpack::backtrace
Kirizaki Feb 25, 2016
5e2f445
Add mlpack::backtrace
Kirizaki Feb 25, 2016
6257fad
Add mlpack::backtrace
Kirizaki Feb 25, 2016
322d384
Merge pull request #1 from mlpack/master
Kirizaki Feb 25, 2016
876de9c
Update log.cpp
Kirizaki Feb 25, 2016
95b9320
Dummy constructor
Kirizaki Feb 25, 2016
0a0d68b
Dummy constructor
Kirizaki Feb 25, 2016
dc0e553
Include backtrace.hpp only when libs are found
Kirizaki Feb 25, 2016
171c35b
Work with mlpack::backtrace only when HAS_BFD_DL..
Kirizaki Feb 25, 2016
170c3a2
Merge pull request #2 from mlpack/master
Kirizaki Mar 1, 2016
76e48a5
Update prefixedoutstream_impl.hpp
Kirizaki Mar 1, 2016
f987048
Merge pull request #3 from mlpack/master
Kirizaki Mar 2, 2016
6c4eb24
Add -rdynamic if BFD or DL found.
Kirizaki Mar 3, 2016
0bc16ea
Change Ubuntu on Linux
Kirizaki Mar 3, 2016
e664f3d
Change order in printing backtrace
Kirizaki Mar 3, 2016
f8228a5
Change order in printing backtrace
Kirizaki Mar 3, 2016
78c60a7
Merge pull request #4 from mlpack/master
Kirizaki Mar 3, 2016
cdb5ac3
Linux specific dependencies
Kirizaki Mar 5, 2016
a0472e4
Platform specific dependencies
Kirizaki Mar 5, 2016
0aad1ee
Linux specific dependencies
Kirizaki Mar 5, 2016
38c6d82
Update CMakeLists.txt
Kirizaki Mar 8, 2016
7ae1355
Update CMakeLists.txt
Kirizaki Mar 8, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
106 changes: 0 additions & 106 deletions CMake/FindBacktrace.cmake

This file was deleted.

89 changes: 89 additions & 0 deletions CMake/FindBfd.cmake
@@ -0,0 +1,89 @@
# - Try to find libbfd
# Once done this will define
#
# LIBBFD_FOUND - system has libbfd
# LIBBFD_INCLUDE_DIRS - the libbfd include directory
# LIBBFD_LIBRARIES - Link these to use libbfd
# LIBBFD_DEFINITIONS - Compiler switches required for using libbfd
#
# Based on:
#
# Copyright (c) 2008 Bernhard Walle <bernhard.walle@gmx.de>
#
# Redistribution and use is allowed according to the terms of the New
# BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#


if (LIBBFD_LIBRARIES AND LIBBFD_INCLUDE_DIRS)
set (LIBBFD_FIND_QUIETLY TRUE)
endif (LIBBFD_LIBRARIES AND LIBBFD_INCLUDE_DIRS)

find_path (LIBBFD_INCLUDE_DIRS
NAMES
bfd.h
dis-asm.h
PATHS
/usr/include
/usr/local/include
/opt/local/include
/opt/include
ENV CPATH)

# Ugly, yes ugly...
find_library (LIBBFD_BFD_LIBRARY
NAMES
bfd
PATHS
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/usr/include
/opt/local/lib
/opt/usr/lib64
ENV LIBRARY_PATH
ENV LD_LIBRARY_PATH)

#find_library (LIBBFD_IBERTY_LIBRARY
# NAMES
# iberty
# PATHS
# /usr/lib
# /usr/lib64
# /usr/local/lib
# /usr/local/lib64
# /usr/include
# /opt/local/lib
# /opt/usr/lib64
# ENV LIBRARY_PATH
# ENV LD_LIBRARY_PATH)

#find_library (LIBBFD_OPCODES_LIBRARY
# NAMES
# opcodes
# PATHS
# /usr/lib
# /usr/lib64
# /usr/local/lib
# /usr/local/lib64
# /usr/include
# /opt/local/lib
# /opt/usr/lib64
# ENV LIBRARY_PATH
# ENV LD_LIBRARY_PATH)


include (FindPackageHandleStandardArgs)


# handle the QUIETLY and REQUIRED arguments and set LIBBFD_FOUND to TRUE if all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBBFD DEFAULT_MSG
LIBBFD_BFD_LIBRARY
# LIBBFD_IBERTY_LIBRARY
# LIBBFD_OPCODES_LIBRARY
LIBBFD_INCLUDE_DIRS)

set(LIBBFD_LIBRARIES "${LIBBFD_BFD_LIBRARY}")
mark_as_advanced(LIBBFD_INCLUDE_DIRS LIBBFD_LIBRARIES)
32 changes: 32 additions & 0 deletions CMake/FindLibDL.cmake
@@ -0,0 +1,32 @@
# - Try to find libdl
# Once done this will define
#
# LIBDL_FOUND - system has libdl
# LIBDL_INCLUDE_DIRS - the libdl include directory
# LIBDL_LIBRARIES - Link these to use libdl
# LIBDL_NEEDS_UNDERSCORE - If extern "C" symbols are prefixed (BSD/Apple)
#

find_path (LIBDL_INCLUDE_DIRS NAMES dlfcn.h)
find_library (LIBDL_LIBRARIES NAMES dl)
include (FindPackageHandleStandardArgs)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibDL DEFAULT_MSG
LIBDL_LIBRARIES
LIBDL_INCLUDE_DIRS)

SET(CMAKE_REQUIRED_LIBRARIES dl)
INCLUDE(CheckCSourceRuns)
CHECK_C_SOURCE_RUNS("#include <dlfcn.h>
#include <stdlib.h>
void testfunc() {}
int main() {
testfunc();
if (dlsym(0, \"_testfunc\") != (void*)0) {
return EXIT_SUCCESS;
} else {
return EXIT_FAILURE;
}
}" LIBDL_NEEDS_UNDERSCORE)

mark_as_advanced(LIBDL_INCLUDE_DIRS LIBDL_LIBRARIES LIBDL_NEEDS_UNDERSCORE)
24 changes: 14 additions & 10 deletions CMakeLists.txt
Expand Up @@ -66,6 +66,20 @@ if(DEBUG)
add_definitions(-DDEBUG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -ftemplate-backtrace-limit=0")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -g -O0")
# mlpack uses it's own mlpack::backtrace class based on Binary File Descriptor
# <bfd.h> and linux Dynamic Loader <libdl.h> and more portable version in future
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(Bfd)
find_package(LibDL)
if(LIBBFD_FOUND AND LIBDL_FOUND)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic")
include_directories(${LIBBFD_INCLUDE_DIRS})
include_directories(${LIBDL_INCLUDE_DIRS})
add_definitions(-DHAS_BFD_DL)
else(LIBBFD_FOUND AND LIBDL_FOUND)
message(WARNING "No libBFD and/or libDL has been found!")
endif(LIBBFD_FOUND AND LIBDL_FOUND)
endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
else()
add_definitions(-DARMA_NO_DEBUG)
add_definitions(-DNDEBUG)
Expand All @@ -80,16 +94,6 @@ if(CMAKE_COMPILER_IS_GNUCC AND PROFILE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
endif(CMAKE_COMPILER_IS_GNUCC AND PROFILE)

# mlpack uses backtrace() facilities. On some systems (e.g., GNU/Linux), it
# resides in libc itself and requires <execinfo.h>; on other (e.g., OpenBSD)
# it's provided by a separate library.
find_package(Backtrace)
if(Backtrace_FOUND)
include_directories(${Backtrace_INCLUDE_DIRS})
add_definitions(-DBACKTRACE_FOUND)
add_definitions(-DBACKTRACE_HEADER="${Backtrace_HEADER}")
endif(Backtrace_FOUND)

# If the user asked for running test cases with verbose output, turn that on.
if(TEST_VERBOSE)
add_definitions(-DTEST_VERBOSE)
Expand Down
22 changes: 19 additions & 3 deletions doc/guide/iodoc.hpp
Expand Up @@ -23,7 +23,8 @@ Output to Log::Debug does not show (and has no performance penalty) when mlpack
is compiled without debugging symbols. Output to Log::Info is only shown when
the program is run with the --verbose (or -v) flag. Log::Warn is always shown,
and Log::Fatal will throw a std::runtime_error exception, when a newline is sent
to it.
to it only. If mlpack was compiled with debugging symbols, Log::Fatal will
always throw a std::runtime_error exception and print backtrace.

Here is a simple example, and its output:

Expand All @@ -48,13 +49,28 @@ int main(int argc, char** argv)
}
@endcode

With debugging output and --verbose, the following is shown:
With debugging output--verbose, the following is shown:

@code
$ ./main --verbose
[DEBUG] Compiled with debugging symbols.
[INFO ] Some test informational output.
[WARN ] A warning!
[FATAL] [bt]: (1) /absolute/path/to/file/example.cpp:6: function()
[FATAL] Program has crashed.
terminate called after throwing an instance of 'std::runtime_error'
what(): fatal error; see Log::Fatal output
Aborted
@endcode

With debugging output, compilation flags -g -rdynamic and --verbose,
the following is shown:

@code
[DEBUG] Compiled with debugging symbols.
[INFO ] Some test informational output.
[WARN ] A warning!
[FATAL] Cannot give backtrace because program was compiled without: -g -rdynamic
[FATAL] For a backtrace, recompile with: -g -rdynamic.
[FATAL] Program has crashed.
terminate called after throwing an instance of 'std::runtime_error'
what(): fatal error; see Log::Fatal output
Expand Down
12 changes: 7 additions & 5 deletions src/mlpack/CMakeLists.txt
Expand Up @@ -41,11 +41,13 @@ if(UNIX AND NOT APPLE)
target_link_libraries(mlpack rt)
endif(UNIX AND NOT APPLE)

# Log::Assert may require linking against whatever provides backtrace
# functionality.
if(Backtrace_FOUND)
target_link_libraries(mlpack ${Backtrace_LIBRARIES})
endif(Backtrace_FOUND)
# Backtrace for Linux need those libs.
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(LIBBFD_FOUND AND LIBDL_FOUND AND DEBUG)
target_link_libraries(mlpack ${LIBBFD_LIBRARIES})
target_link_libraries(mlpack ${LIBDL_LIBRARIES})
endif(LIBBFD_FOUND AND LIBDL_FOUND AND DEBUG)
endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")

# Collect all header files in the library.
file(GLOB_RECURSE INCLUDE_H_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h)
Expand Down
4 changes: 4 additions & 0 deletions src/mlpack/core.hpp
Expand Up @@ -205,6 +205,10 @@
#include <mlpack/core/dists/discrete_distribution.hpp>
#include <mlpack/core/dists/gaussian_distribution.hpp>
#include <mlpack/core/dists/laplace_distribution.hpp>
//mlpack::backtrace only for linux
#ifdef HAS_BFD_DL
#include <mlpack/core/util/backtrace.hpp>
#endif

// Include kernel traits.
#include <mlpack/core/kernels/kernel_traits.hpp>
Expand Down
2 changes: 2 additions & 0 deletions src/mlpack/core/util/CMakeLists.txt
Expand Up @@ -3,6 +3,8 @@
set(SOURCES
arma_config.hpp
arma_config_check.hpp
backtrace.hpp
backtrace.cpp
cli.hpp
cli.cpp
cli_deleter.hpp
Expand Down