Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions host-configs/tpls.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ endif()
if(EXISTS ${GEOS_TPL_DIR}/mathpresso)
set(MATHPRESSO_DIR ${GEOS_TPL_DIR}/mathpresso CACHE PATH "" FORCE)
endif()

if(EXISTS ${GEOS_TPL_DIR}/cpptrace)
set(CPPTRACE_DIR ${GEOS_TPL_DIR}/cpptrace CACHE PATH "" FORCE)
endif()
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ if( ENABLE_HIP )
list( APPEND extraComponentsLinkList blt::hip )
endif()

if( ENABLE_CPPTRACE )
list( APPEND extraComponentsLinkList cpptrace::cpptrace )
endif()

blt_add_executable( NAME geosx
SOURCES main/main.cpp
DEPENDS_ON geosx_core
Expand Down
1 change: 1 addition & 0 deletions src/cmake/GeosxConfig.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set( PREPROCESSOR_DEFINES BOUNDS_CHECK
CALIPER
CPPTRACE
CHAI
CUDA
CUDA_NVTOOLSEXT
Expand Down
36 changes: 36 additions & 0 deletions src/cmake/thirdparty/SetupGeosxThirdParty.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ else()
set(ENABLE_PETSC OFF CACHE BOOL "" FORCE)
message(STATUS "Not using PETSc")
endif()

################################
# VTK
################################
Expand Down Expand Up @@ -935,6 +936,41 @@ else()
message(STATUS "Not using VTK")
endif()

################################
# CPPTRACE
################################
if( DEFINED CPPTRACE_DIR )
message( STATUS "CPPTRACE_DIR = ${CPPTRACE_DIR}" )

if( DEFINED ZSTD_DIR )
message( STATUS "ZSTD_DIR = ${ZSTD_DIR}" )
list( PREPEND CMAKE_PREFIX_PATH "${ZSTD_DIR}" )
endif()
if( DEFINED LIBDWARF_DIR )
message( STATUS "LIBDWARF_DIR = ${LIBDWARF_DIR}" )
list( PREPEND CMAKE_PREFIX_PATH "${LIBDWARF_DIR}" )
endif()

find_package( cpptrace REQUIRED
PATHS ${CPPTRACE_DIR}
NO_DEFAULT_PATH )

if( DEFINED cpptrace_VERSION )
message( " ----> cpptrace_VERSION=${cpptrace_VERSION}")
endif()

blt_convert_to_system_includes( TARGET cpptrace::cpptrace )

set( ENABLE_CPPTRACE ON CACHE BOOL "" )
set( thirdPartyLibs ${thirdPartyLibs} cpptrace::cpptrace )
else()
if( ENABLE_CPPTRACE )
message( WARNING "ENABLE_CPPTRACE is ON but CPPTRACE_DIR isn't defined." )
endif()

set( ENABLE_CPPTRACE OFF CACHE BOOL "" )
message( STATUS "Not using cpptrace" )
endif()

################################
# uncrustify
Expand Down
6 changes: 6 additions & 0 deletions src/coreComponents/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set( common_headers
logger/Logger.hpp
logger/ErrorHandling.hpp
logger/ExternalErrorHandler.hpp
logger/StackTrace.hpp
MpiWrapper.hpp
Path.hpp
Span.hpp
Expand Down Expand Up @@ -82,6 +83,7 @@ set( common_sources
logger/Logger.cpp
logger/ErrorHandling.cpp
logger/ExternalErrorHandler.cpp
logger/StackTrace.cpp
BufferAllocator.cpp
MemoryInfos.cpp
MpiWrapper.cpp
Expand Down Expand Up @@ -136,6 +138,10 @@ if( ENABLE_CALIPER )
endif()
endif()

if( ENABLE_CPPTRACE )
list( APPEND dependencyList cpptrace::cpptrace )
endif()

blt_add_library( NAME common
SOURCES ${common_sources}
HEADERS ${common_headers}
Expand Down
3 changes: 3 additions & 0 deletions src/coreComponents/common/GeosxConfig.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
/// Enables use of CHAI (CMake option ENABLE_CHAI)
#cmakedefine GEOS_USE_CHAI

/// Enables use of Cpptrace (CMake option ENABLE_CPPTRACE)
#cmakedefine GEOS_USE_CPPTRACE

/// Enables use of Mathpresso library (CMake option ENABLE_MATHPRESSO)
#cmakedefine GEOS_USE_MATHPRESSO

Expand Down
5 changes: 3 additions & 2 deletions src/coreComponents/common/initializeEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "logger/ErrorHandling.hpp"
#include "logger/ExternalErrorHandler.hpp"
#include <umpire/TypedAllocator.hpp>
#include "logger/StackTrace.hpp"
// TPL includes
#include <umpire/ResourceManager.hpp>
#include <umpire/Allocator.hpp>
Expand Down Expand Up @@ -86,7 +87,7 @@ void setupLogger()
}
else
{
std::string const stackHistory = LvArray::system::stackTrace( true );
std::string const stackHistory = StackTrace::stackTrace();
DiagnosticMsg diagnosticMsg;
ErrorLogger::global().flushErrorMsg( DiagnosticMsgBuilder::init( diagnosticMsg,
MsgType::Error, errorMsg,
Expand All @@ -109,7 +110,7 @@ void setupLogger()
ExternalErrorHandler::instance().flush( "before signal error output" );

// error message output
std::string const stackHistory = LvArray::system::stackTrace( true );
std::string const stackHistory = StackTrace::signalSafeStackTrace();
DiagnosticMsg diagnosticMsg;
ErrorLogger::global().flushErrorMsg( DiagnosticMsgBuilder::init( diagnosticMsg,
MsgType::ExternalError, "",
Expand Down
19 changes: 13 additions & 6 deletions src/coreComponents/common/logger/ErrorHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
*/

#include "ErrorHandling.hpp"
#include "Logger.hpp"
#include "StackTrace.hpp"
#include "common/DataTypes.hpp"
#include "common/logger/Logger.hpp"
#include "common/logger/StackTrace.hpp"
#include "common/format/StringUtilities.hpp"

#include <fstream>
Expand Down Expand Up @@ -66,7 +69,7 @@ DiagnosticMsgBuilder ErrorLogger::initCurrentExceptionMessage( MsgType msgType,
m_getCurrentExceptionMsg = DiagnosticMsgBuilder::init( diagnosticMsg,
msgType, msgContent,
rank )
.addCallStackInfo( LvArray::system::stackTrace( true ) )
.addCallStackInfo( StackTrace::stackTrace() )
.getDiagnosticMsg();
return DiagnosticMsgBuilder::modify( m_getCurrentExceptionMsg );
}
Expand Down Expand Up @@ -203,20 +206,24 @@ DiagnosticMsgBuilder & DiagnosticMsgBuilder::addRank( integer const rank )

DiagnosticMsgBuilder & DiagnosticMsgBuilder::addCallStackInfo( std::string_view ossStackTrace )
{
m_errorMsg.m_sourceCallStack.clear();
m_errorMsg.m_isValidStackTrace = false;

std::string str = std::string( ossStackTrace );
std::istringstream iss( str );
std::string stackLine;
std::size_t index;

std::regex pattern( R"(Frame \d+: \S+)" );
std::regex lvArrayPattern( R"(Frame \d+:\s*)" );
std::regex cpptracePattern( R"(^\s*#\d+\s+)" );

while( std::getline( iss, stackLine ) )
{
if( std::regex_search( stackLine, pattern ))
std::smatch m;
if( std::regex_search( stackLine, m, lvArrayPattern ) ||
std::regex_search( stackLine, m, cpptracePattern ) )
{
m_errorMsg.m_isValidStackTrace = true;
index = stackLine.find( ':' );
m_errorMsg.m_sourceCallStack.push_back( stackLine.substr( index + 1 ) );
m_errorMsg.m_sourceCallStack.push_back( stackLine.substr( m.position() + m.length() ) );
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/coreComponents/common/logger/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// Source includes
#include "LvArray/src/Macros.hpp"
#include "common/logger/GeosExceptions.hpp"
#include "common/logger/StackTrace.hpp"

// System includes
#include <stdexcept>
Expand Down Expand Up @@ -161,7 +162,7 @@
::geos::logger::internal::g_rank ) \
.setCodeLocation( __FILE__, __LINE__ ) \
.setCause( __causemsgsoss.str() ) \
.addCallStackInfo( LvArray::system::stackTrace( true ) ) \
.addCallStackInfo( StackTrace::stackTrace() ) \
.addContextInfo( GEOS_DETAIL_REST_ARGS( __VA_ARGS__ )); \
GEOS_GLOBAL_LOGGER.flushCurrentExceptionMessage(); \
LvArray::system::callErrorHandler(); \
Expand Down Expand Up @@ -248,7 +249,7 @@
::geos::logger::internal::g_rank ) \
.setCodeLocation( __FILE__, __LINE__ ) \
.setCause( __causemsgsoss.str() ) \
.addCallStackInfo( LvArray::system::stackTrace( true ) ) \
.addCallStackInfo( StackTrace::stackTrace() ) \
.addContextInfo( GEOS_DETAIL_REST_ARGS( __VA_ARGS__ )) \
.getDiagnosticMsg(); \
auto ex = GEOS_DETAIL_FIRST_ARG( __VA_ARGS__ )(); \
Expand Down Expand Up @@ -341,7 +342,7 @@
::geos::logger::internal::g_rank ) \
.setCodeLocation( __FILE__, __LINE__ ) \
.setCause( __causemsgsoss.str() ) \
.addCallStackInfo( LvArray::system::stackTrace( true ) ) \
.addCallStackInfo( StackTrace::stackTrace() ) \
.addContextInfo( GEOS_DETAIL_REST_ARGS( __VA_ARGS__ )) \
.getDiagnosticMsg() ); \
} \
Expand Down
91 changes: 91 additions & 0 deletions src/coreComponents/common/logger/StackTrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 TotalEnergies
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/

/**
* @file StackTrace.cpp
*/

#include "StackTrace.hpp"
#include "LvArray/src/system.hpp"

#include "common/GeosxConfig.hpp"
#ifdef GEOS_USE_CPPTRACE
#include <cpptrace/cpptrace.hpp>
#include <cpptrace/from_current.hpp>
#include <cpptrace/formatting.hpp>
#endif

namespace geos
{


std::string StackTrace::stackTrace()
{
#ifdef GEOS_USE_CPPTRACE
return formatter().format( cpptrace::generate_trace( /* skip = */ 1 ) );
#else
return LvArray::system::stackTrace( true );
#endif
}

std::string StackTrace::signalSafeStackTrace()
{
return LvArray::system::stackTrace( true );
}

#ifdef GEOS_USE_CPPTRACE
cpptrace::formatter const & StackTrace::formatter()
{
static cpptrace::formatter const fmt = cpptrace::formatter{}
.header( "" )
.addresses( cpptrace::formatter::address_mode::none )
.paths( cpptrace::formatter::path_mode::basename )
.symbols( cpptrace::formatter::symbol_mode::pretty )
.snippets( false )
.columns( true )
.filtered_frame_placeholders( false )
.filter( []( cpptrace::stacktrace_frame const & frame )
{
static char const * const blacklist[] = {
"__cxa_throw",
"__cxa_rethrow",
"_Unwind_RaiseException",
"_Unwind_Resume",
"cpptrace::detail::",
"cpptrace::v1::detail::",
"cpptrace::try_catch::",
"cpptrace::v1::try_catch::",
};
for( char const * symbolToHide : blacklist )
{
if( frame.symbol.find( symbolToHide ) != std::string::npos )
{
return false;
}
}
return true;
} );

return fmt;
}

std::string StackTrace::formatStackTrace( cpptrace::stacktrace const & stacktrace )
{
return formatter().format( stacktrace );
}
#endif


} /* namespace geos */
74 changes: 74 additions & 0 deletions src/coreComponents/common/logger/StackTrace.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 TotalEnergies
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/

/**
* @file StackTrace.hpp
*/

#ifndef GEOS_COMMON_LOGGER_STACKTRACE_HPP
#define GEOS_COMMON_LOGGER_STACKTRACE_HPP

#include "common/GeosxConfig.hpp" // For the following guards
#ifdef GEOS_USE_CPPTRACE
#include <cpptrace/cpptrace.hpp>
#include <cpptrace/formatting.hpp>
#endif

#include <string>

namespace geos
{

/**
* @brief Utility class to interact with stack traces.
*/
class StackTrace
{
public:

/**
* @brief Get a stack trace for the current thread.
* @return The stack trace as a string.
* @note Not signal-safe. Use signalSafeStackTrace() from inside a signal handler.
*/
static std::string stackTrace();

/**
* @brief Get a stack trace from a context where signal-safety is required.
* @return The stack trace as a string.
*/
static std::string signalSafeStackTrace();

#ifdef GEOS_USE_CPPTRACE
/**
* @brief Access the configured cpptrace formatter.
* @return The formatter instance.
*/
static cpptrace::formatter const & formatter();

/**
* @brief Format a cpptrace stacktrace using the configured formatter.
* @param stacktrace The cpptrace stack trace to format.
* @return Formatted stack trace string.
*/
static std::string formatStackTrace( cpptrace::stacktrace const & stacktrace );
#endif

};


} /* namespace geos */

#endif /* GEOS_COMMON_LOGGER_STACKTRACE_HPP */
Loading
Loading