Skip to content

Commit

Permalink
[fix](stacktrace) Add conf enable_stacktrace (apache#37713)
Browse files Browse the repository at this point in the history
`enable_stacktrace` if false, turn off all stacktrace
  • Loading branch information
xinyiZzz authored and seawinde committed Jul 17, 2024
1 parent 2ebb0c8 commit d0fb104
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 33 deletions.
4 changes: 0 additions & 4 deletions be/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,6 @@ if (USE_UNWIND)
endif()
endif()

if (ENABLE_STACKTRACE)
add_definitions(-DENABLE_STACKTRACE)
endif()

if (USE_DWARF)
add_compile_options(-gdwarf-5)
endif()
Expand Down
2 changes: 2 additions & 0 deletions be/src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ DEFINE_mBool(enable_query_memory_overcommit, "true");

DEFINE_mBool(disable_memory_gc, "false");

DEFINE_mBool(enable_stacktrace, "true");

DEFINE_mBool(enable_stacktrace_in_allocator_check_failed, "false");

DEFINE_mInt64(large_memory_check_bytes, "2147483648");
Expand Down
3 changes: 3 additions & 0 deletions be/src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ DECLARE_mBool(enable_query_memory_overcommit);
// default gc strategy is conservative, if you want to exclude the interference of gc, let it be true
DECLARE_mBool(disable_memory_gc);

// if false, turn off all stacktrace
DECLARE_mBool(enable_stacktrace);

// Allocator check failed log stacktrace if not catch exception
DECLARE_mBool(enable_stacktrace_in_allocator_check_failed);

Expand Down
28 changes: 10 additions & 18 deletions be/src/common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
#include <utility>

#include "common/compiler_util.h" // IWYU pragma: keep
#ifdef ENABLE_STACKTRACE
#include "util/stack_util.h"
#endif

#include "common/config.h"
#include "common/expected.h"
#include "util/stack_util.h"

namespace doris {

Expand Down Expand Up @@ -363,9 +361,9 @@ class [[nodiscard]] Status {
Status(int code, std::string msg, std::string stack = "") : _code(code) {
_err_msg = std::make_unique<ErrMsg>();
_err_msg->_msg = std::move(msg);
#ifdef ENABLE_STACKTRACE
_err_msg->_stack = std::move(stack);
#endif
if (config::enable_stacktrace) {
_err_msg->_stack = std::move(stack);
}
}

// copy c'tor makes copy of error detail so Status can be returned by value
Expand Down Expand Up @@ -416,13 +414,12 @@ class [[nodiscard]] Status {
} else {
status._err_msg->_msg = fmt::format(msg, std::forward<Args>(args)...);
}
#ifdef ENABLE_STACKTRACE
if (stacktrace && ErrorCode::error_states[abs(code)].stacktrace) {
if (stacktrace && ErrorCode::error_states[abs(code)].stacktrace &&
config::enable_stacktrace) {
// Delete the first one frame pointers, which are inside the status.h
status._err_msg->_stack = get_stack_trace(1);
LOG(WARNING) << "meet error status: " << status; // may print too many stacks.
}
#endif
return status;
}

Expand All @@ -436,12 +433,11 @@ class [[nodiscard]] Status {
} else {
status._err_msg->_msg = fmt::format(msg, std::forward<Args>(args)...);
}
#ifdef ENABLE_STACKTRACE
if (stacktrace && ErrorCode::error_states[abs(code)].stacktrace) {
if (stacktrace && ErrorCode::error_states[abs(code)].stacktrace &&
config::enable_stacktrace) {
status._err_msg->_stack = get_stack_trace(1);
LOG(WARNING) << "meet error status: " << status; // may print too many stacks.
}
#endif
return status;
}

Expand Down Expand Up @@ -545,9 +541,7 @@ class [[nodiscard]] Status {
int _code;
struct ErrMsg {
std::string _msg;
#ifdef ENABLE_STACKTRACE
std::string _stack;
#endif
};
std::unique_ptr<ErrMsg> _err_msg;

Expand Down Expand Up @@ -604,11 +598,9 @@ class AtomicStatus {
inline std::ostream& operator<<(std::ostream& ostr, const Status& status) {
ostr << '[' << status.code_as_string() << ']';
ostr << status.msg();
#ifdef ENABLE_STACKTRACE
if (status._err_msg && !status._err_msg->_stack.empty()) {
if (status._err_msg && !status._err_msg->_stack.empty() && config::enable_stacktrace) {
ostr << '\n' << status._err_msg->_stack;
}
#endif
return ostr;
}

Expand Down
6 changes: 3 additions & 3 deletions be/src/util/stack_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ void DumpStackTraceToString(std::string* stacktrace);
namespace doris {

std::string get_stack_trace(int start_pointers_index, std::string dwarf_location_info_mode) {
#ifdef ENABLE_STACKTRACE
if (!config::enable_stacktrace) {
return "no enable stacktrace";
}
if (dwarf_location_info_mode.empty()) {
dwarf_location_info_mode = config::dwarf_location_info_mode;
}
Expand All @@ -55,8 +57,6 @@ std::string get_stack_trace(int start_pointers_index, std::string dwarf_location
} else {
return "no stack";
}
#endif
return "no enable stack";
}

std::string get_stack_trace_by_glog() {
Expand Down
6 changes: 3 additions & 3 deletions be/src/vec/common/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ void Allocator<clear_memory_, mmap_populate, use_mmap>::sys_memory_check(size_t
[[maybe_unused]] auto stack_trace_st =
doris::Status::Error<doris::ErrorCode::MEM_ALLOC_FAILED, true>(
injection_err_msg);
#ifndef ENABLE_STACKTRACE
LOG(INFO) << stack_trace_st.to_string();
#endif
if (!doris::config::enable_stacktrace) {
LOG(INFO) << stack_trace_st.to_string();
}
if (!doris::enable_thread_catch_bad_alloc) {
doris::thread_context()->thread_mem_tracker_mgr->cancel_query(injection_err_msg);
} else {
Expand Down
5 changes: 5 additions & 0 deletions be/test/testutil/run_all_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "common/config.h"
#include "common/logging.h"
#include "common/status.h"
#include "gtest/gtest.h"
#include "gtest/gtest_pred_impl.h"
#include "http/ev_http_server.h"
Expand Down Expand Up @@ -60,6 +61,10 @@ int main(int argc, char** argv) {
doris::TabletSchemaCache::create_global_schema_cache(
doris::config::tablet_schema_cache_capacity));
LOG(INFO) << "init config " << st;
doris::Status s = doris::config::set_config("enable_stacktrace", "false");
if (!s.ok()) {
LOG(WARNING) << "set enable_stacktrace=false failed";
}

doris::init_glog("be-test");
::testing::InitGoogleTest(&argc, argv);
Expand Down
5 changes: 0 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,6 @@ fi
if [[ -z "${USE_BTHREAD_SCANNER}" ]]; then
USE_BTHREAD_SCANNER='OFF'
fi
if [[ -z "${ENABLE_STACKTRACE}" ]]; then
ENABLE_STACKTRACE='ON'
fi

if [[ -z "${USE_DWARF}" ]]; then
USE_DWARF='OFF'
Expand Down Expand Up @@ -484,7 +481,6 @@ echo "Get params:
USE_MEM_TRACKER -- ${USE_MEM_TRACKER}
USE_JEMALLOC -- ${USE_JEMALLOC}
USE_BTHREAD_SCANNER -- ${USE_BTHREAD_SCANNER}
ENABLE_STACKTRACE -- ${ENABLE_STACKTRACE}
ENABLE_INJECTION_POINT -- ${ENABLE_INJECTION_POINT}
DENABLE_CLANG_COVERAGE -- ${DENABLE_CLANG_COVERAGE}
DISPLAY_BUILD_TIME -- ${DISPLAY_BUILD_TIME}
Expand Down Expand Up @@ -586,7 +582,6 @@ if [[ "${BUILD_BE}" -eq 1 ]]; then
-DENABLE_PCH="${ENABLE_PCH}" \
-DUSE_MEM_TRACKER="${USE_MEM_TRACKER}" \
-DUSE_JEMALLOC="${USE_JEMALLOC}" \
-DENABLE_STACKTRACE="${ENABLE_STACKTRACE}" \
-DUSE_AVX2="${USE_AVX2}" \
-DGLIBC_COMPATIBILITY="${GLIBC_COMPATIBILITY}" \
-DEXTRA_CXX_FLAGS="${EXTRA_CXX_FLAGS}" \
Expand Down

0 comments on commit d0fb104

Please sign in to comment.