Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
stacktrace_log.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
17#include "shambase/string.hpp"
20#include <sstream>
21#include <vector>
22
23#ifdef SHAMROCK_USE_CPPTRACE
24 #include <cpptrace/cpptrace.hpp>
25 #include <cpptrace/formatting.hpp>
26#endif
27
28namespace {
29
30 // replace rules
31 static const std::vector<std::pair<std::string, std::string>> replace_rules = {
32#ifdef SYCL_COMP_ACPP
33 {"hipsycl::sycl::vec<long, 3, hipsycl::sycl::detail::vec_storage<long, 3> >", "i64_3"},
34 {"hipsycl::sycl::vec<double, 3, hipsycl::sycl::detail::vec_storage<double, 3> >", "f64_3"},
35 {"hipsycl::sycl::", "sycl::"},
36#endif
37#ifdef SYCL_COMP_DPCPP
38 {"sycl::_V1::vec<long, 3>", "i64_3"},
39 {"sycl::_V1::vec<double, 3>", "f64_3"},
40 {"sycl::_V1::", "sycl::"},
41#endif
42 };
43
44#ifdef SHAMROCK_USE_CPPTRACE
45 static cpptrace::formatter formatter = {};
46#endif
47
48} // namespace
49
50namespace shamsys {
51
52 void init_backtrace_utilities(bool enable_colors) {
53
54#ifdef SHAMROCK_USE_CPPTRACE
55 auto color_mode = enable_colors ? cpptrace::formatter::color_mode::always
56 : cpptrace::formatter::color_mode::none;
57
58 formatter = cpptrace::formatter{}
59 .transform([](cpptrace::stacktrace_frame frame) {
60 for (const auto &[pattern, replacement] : replace_rules) {
61 shambase::replace_all(frame.symbol, pattern, replacement);
62 }
63 return frame;
64 })
65 .symbols(cpptrace::formatter::symbol_mode::pretty)
66 .colors(color_mode)
67 .break_before_filename()
68 .snippets(false);
69#endif
70 }
71
72 std::string crash_report_backtrace() {
73 std::stringstream ss;
74 ss << "------ Profiler stacktrace ------\n";
75 ss << " - Provide a basic stacktrace based on the locations of StackEntry objects\n";
76#ifndef SHAMROCK_USE_CPPTRACE
77 ss << " - To get the precise a more precise stacktrace, please reconfigure with :\n";
78 ss << " \"cmake . -DSHAMROCK_USE_CPPTRACE=on -DCMAKE_BUILD_TYPE=RelWithDebInfo\"\n";
79#endif
80 ss << "\n";
82 ss << "------ End of profiler stacktrace ------\n";
83 ss << "\n";
84#ifdef SHAMROCK_USE_CPPTRACE
85 ss << "------ True stacktrace ------\n";
86 ss << " - Provide a true stacktrace based on the actual call stack (using cpptrace)\n";
87 ss << " - Please compile with -g to get line informations (cmake . "
88 "-DCMAKE_BUILD_TYPE=RelWithDebInfo)\n";
89 ss << "\n";
90 ss << formatter.format(cpptrace::generate_trace());
91 ss << "\n";
92 ss << "------ End of true stacktrace ------\n";
93#endif
94 return ss.str();
95 }
96
97} // namespace shamsys
std::string fmt_callstack()
Get the formatted callstack.
void replace_all(std::string &inout, std::string_view what, std::string_view with)
replace all occurence of a search string with another
Definition string.hpp:183
fmt::formatter< T > formatter
Formatter alias for fmt::formatter
Definition format.hpp:35
namespace for the system handling
void init_backtrace_utilities(bool enable_colors)
Initialize the backtrace utilities.
std::string crash_report_backtrace()
Generate a backtrace for the crash report.
This file contains the definition for the stacktrace related functionality.
Utilities to generate a backtrace for the crash report.