Skip to content

Commit

Permalink
lib: reformatted code and removed printf specifier
Browse files Browse the repository at this point in the history
- renamed original stacktrace.cxx into backtrace.cc
- do not print pointers or offets
- just output function names
- write backtrace information to stdout
- added a header file for backtrace.cc
  • Loading branch information
franku committed Jul 4, 2019
1 parent 16ff7ad commit 797125f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 71 deletions.
4 changes: 4 additions & 0 deletions core/CMakeLists.txt
Expand Up @@ -100,6 +100,10 @@ IF (NOT ${OPENSSL_FOUND})
MESSAGE(FATAL_ERROR "FATAL ERROR: OpenSSL is required but was not found.")
ENDIF()

IF (${HAVE_EXECINFO_H})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ldl")
ENDIF()

IF (coverage)
set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage" CACHE INTERNAL "")
MESSAGE(STATUS "coverage requested, adding COVERAGE_COMPILER_FLAGS : ${COVERAGE_COMPILER_FLAGS}")
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/CMakeLists.txt
Expand Up @@ -46,7 +46,7 @@ include_directories(
${CAP_INCLUDE_DIRS}
${WRAP_INCLUDE_DIRS})

set (BAREOS_SRCS address_conf.cc alist.cc attr.cc attribs.cc base64.cc
set (BAREOS_SRCS address_conf.cc alist.cc attr.cc attribs.cc backtrace.cc base64.cc
berrno.cc bget_msg.cc binflate.cc bnet_server_tcp.cc bnet.cc bpipe.cc
breg.cc bregex.cc bsnprintf.cc bsock.cc bsock_network_dump.cc bsock_tcp.cc bstringlist.cc
bsys.cc btime.cc btimers.cc cbuf.cc
Expand Down
59 changes: 59 additions & 0 deletions core/src/lib/backtrace.cc
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2009-2017, Farooq Mela
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <execinfo.h> // for backtrace
#include <dlfcn.h> // for dladdr
#include <cxxabi.h> // for __cxa_demangle

#include <string>
#include <sstream>

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;
if (dladdr(callstack[i], &info)) {
char* demangled = NULL;
int status;
demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
snprintf(buf, sizeof(buf), "%-3d %s\n", i,
status == 0 ? demangled : info.dli_sname);
free(demangled);
} else {
snprintf(buf, sizeof(buf), "%-3d\n", i);
}
trace_buf << buf;
}
free(symbols);
if (nFrames == nMaxFrames) trace_buf << "[truncated]\n";
return trace_buf.str();
}
27 changes: 27 additions & 0 deletions core/src/lib/backtrace.h
@@ -0,0 +1,27 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2019-2019 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/

#ifndef BAREOS_LIB_BACKTRACE_H_
#define BAREOS_LIB_BACKTRACE_H_

std::string Backtrace(int skip = 1);

#endif // BAREOS_LIB_BACKTRACE_H_
70 changes: 0 additions & 70 deletions core/src/lib/stacktrace.cxx

This file was deleted.

0 comments on commit 797125f

Please sign in to comment.