From d69f47b4969b877da3d1067c29c9978b889e1df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=90=E6=9D=A5?= Date: Thu, 7 Nov 2024 21:34:36 +0800 Subject: [PATCH 1/7] fix bplus tree log --- 474.patch | 161 ++++++++++++++++++ src/observer/storage/index/bplus_tree_log.cpp | 2 +- 2 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 474.patch diff --git a/474.patch b/474.patch new file mode 100644 index 000000000..21b43ea28 --- /dev/null +++ b/474.patch @@ -0,0 +1,161 @@ +From b1c7e6a57d896352dfaf2396a4644b61960b4a24 Mon Sep 17 00:00:00 2001 +From: AUA +Date: Mon, 4 Nov 2024 11:48:42 +0000 +Subject: [PATCH 1/3] commit-message: update the cmakelists.txt #432 + +--- + CMakeLists.txt | 27 ++++++++++++++------------- + build.sh | 31 ++++++++++++++----------------- + 2 files changed, 28 insertions(+), 30 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 31b5917f6..1b6540d9b 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -10,6 +10,14 @@ SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) + + SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) + ++# 设置默认构建类型为 Debug ++if(NOT CMAKE_BUILD_TYPE) ++ set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type:Default is debug" FORCE) ++endif() ++ ++# 输出当前构建类型 ++message(STATUS "Using build type: ${CMAKE_BUILD_TYPE}") ++ + OPTION(ENABLE_ASAN "Enable build with address sanitizer" ON) + OPTION(ENABLE_TSAN "Build with thread sanitizer" OFF) + OPTION(ENABLE_UBSAN "Build with undefined behavior sanitizer" OFF) +@@ -57,19 +65,6 @@ IF(USE_SIMD) + ADD_DEFINITIONS(-DUSE_SIMD) + ENDIF(USE_SIMD) + +-IF(DEBUG) +- MESSAGE(STATUS "DEBUG has been set as TRUE ${DEBUG}") +- SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O0 -g -DDEBUG ") +- ADD_DEFINITIONS(-DENABLE_DEBUG) +-ELSEIF(NOT DEFINED ENV{DEBUG}) +- MESSAGE(STATUS "Disable debug") +- SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O2 -g ") +-ELSE() +- MESSAGE(STATUS "Enable debug") +- SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O0 -g -DDEBUG") +- ADD_DEFINITIONS(-DENABLE_DEBUG) +-ENDIF(DEBUG) +- + IF (CONCURRENCY) + MESSAGE(STATUS "CONCURRENCY is ON") + SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DCONCURRENCY") +@@ -164,6 +159,12 @@ ENDIF(WITH_UNIT_TESTS) + + SET(CMAKE_CXX_FLAGS ${CMAKE_COMMON_FLAGS}) + SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS}) ++ ++set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0 -DDEBUG") ++set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2 -DNDEBUG") ++set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O2 -g -DDEBUG") ++set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -Os") ++ + MESSAGE(STATUS "CMAKE_CXX_FLAGS is " ${CMAKE_CXX_FLAGS}) + + # ADD_SUBDIRECTORY(src bin) bin 为目标目录, 可以省略 +diff --git a/build.sh b/build.sh +index 0a914d671..4b4f0f2f7 100755 +--- a/build.sh ++++ b/build.sh +@@ -137,8 +137,8 @@ function do_build + { + TYPE=$1; shift + prepare_build_dir $TYPE || return +- echo "${CMAKE_COMMAND} ${TOPDIR} $@" +- ${CMAKE_COMMAND} -S ${TOPDIR} $@ ++ echo "${CMAKE_COMMAND} $@ ${TOPDIR}" ++ ${CMAKE_COMMAND} $@ -S ${TOPDIR} + } + + function do_clean +@@ -147,23 +147,20 @@ function do_clean + find . -maxdepth 1 -type d -name 'build_*' | xargs rm -rf + } + +-function build +-{ +- set -- "${BUILD_ARGS[@]}" +- case "x$1" in +- xrelease) +- do_build "$@" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DDEBUG=OFF +- ;; +- xdebug) +- do_build "$@" -DCMAKE_BUILD_TYPE=Debug -DDEBUG=ON +- ;; +- *) +- BUILD_ARGS=(debug "${BUILD_ARGS[@]}") +- build +- ;; +- esac ++function build { ++ # 默认参数是 debug ++ if [ -z "${BUILD_ARGS[0]}" ]; then ++ set -- "debug" # 如果没有参数,则设置默认值 ++ else ++ set -- "${BUILD_ARGS[@]}" # 否则使用 BUILD_ARGS 的第一个参数 ++ fi ++ local build_type_lower=$(echo "$1" | tr '[:upper:]' '[:lower:]') # 转换为小写 ++ echo "Build type: $build_type_lower" # 输出构建类型 ++ ++ do_build "$build_type_lower" -DCMAKE_BUILD_TYPE="$build_type_lower" # 调用 do_build + } + ++ + function main + { + case "$1" in + +From 360240155dd67d397cd406c6e23c543e53bace48 Mon Sep 17 00:00:00 2001 +From: AUA +Date: Mon, 4 Nov 2024 12:27:03 +0000 +Subject: [PATCH 2/3] commit-message: update the cmakelists.txt #432 + +--- + CMakeLists.txt | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 1b6540d9b..a6618dc4e 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -161,7 +161,7 @@ SET(CMAKE_CXX_FLAGS ${CMAKE_COMMON_FLAGS}) + SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS}) + + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0 -DDEBUG") +-set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2 -DNDEBUG") ++set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") + set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O2 -g -DDEBUG") + set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -Os") + + +From 8c68a18b6035177e0f30e54569b45f50f0e7e193 Mon Sep 17 00:00:00 2001 +From: AUA +Date: Mon, 4 Nov 2024 12:48:05 +0000 +Subject: [PATCH 3/3] commit-message: update the cmakelists.txt #432 + +--- + build.sh | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/build.sh b/build.sh +index 4b4f0f2f7..e0ba80810 100755 +--- a/build.sh ++++ b/build.sh +@@ -137,8 +137,8 @@ function do_build + { + TYPE=$1; shift + prepare_build_dir $TYPE || return +- echo "${CMAKE_COMMAND} $@ ${TOPDIR}" +- ${CMAKE_COMMAND} $@ -S ${TOPDIR} ++ echo "${CMAKE_COMMAND} ${TOPDIR} $@" ++ ${CMAKE_COMMAND} -S ${TOPDIR} $@ + } + + function do_clean diff --git a/src/observer/storage/index/bplus_tree_log.cpp b/src/observer/storage/index/bplus_tree_log.cpp index e2ee32ff3..2225a38e1 100644 --- a/src/observer/storage/index/bplus_tree_log.cpp +++ b/src/observer/storage/index/bplus_tree_log.cpp @@ -33,7 +33,7 @@ BplusTreeLogger::BplusTreeLogger(LogHandler &log_handler, int32_t buffer_pool_id : log_handler_(log_handler), buffer_pool_id_(buffer_pool_id) {} -BplusTreeLogger::~BplusTreeLogger() { commit(); } +BplusTreeLogger::~BplusTreeLogger() {} RC BplusTreeLogger::init_header_page(Frame *frame, const IndexFileHeader &header) { From 2429115ac6d6c833cffbcaf08b0093314192a5eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=90=E6=9D=A5?= Date: Fri, 8 Nov 2024 09:57:45 +0800 Subject: [PATCH 2/7] remove unused file --- 474.patch | 161 ------------------------------------------------------ 1 file changed, 161 deletions(-) delete mode 100644 474.patch diff --git a/474.patch b/474.patch deleted file mode 100644 index 21b43ea28..000000000 --- a/474.patch +++ /dev/null @@ -1,161 +0,0 @@ -From b1c7e6a57d896352dfaf2396a4644b61960b4a24 Mon Sep 17 00:00:00 2001 -From: AUA -Date: Mon, 4 Nov 2024 11:48:42 +0000 -Subject: [PATCH 1/3] commit-message: update the cmakelists.txt #432 - ---- - CMakeLists.txt | 27 ++++++++++++++------------- - build.sh | 31 ++++++++++++++----------------- - 2 files changed, 28 insertions(+), 30 deletions(-) - -diff --git a/CMakeLists.txt b/CMakeLists.txt -index 31b5917f6..1b6540d9b 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -10,6 +10,14 @@ SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) - - SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) - -+# 设置默认构建类型为 Debug -+if(NOT CMAKE_BUILD_TYPE) -+ set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type:Default is debug" FORCE) -+endif() -+ -+# 输出当前构建类型 -+message(STATUS "Using build type: ${CMAKE_BUILD_TYPE}") -+ - OPTION(ENABLE_ASAN "Enable build with address sanitizer" ON) - OPTION(ENABLE_TSAN "Build with thread sanitizer" OFF) - OPTION(ENABLE_UBSAN "Build with undefined behavior sanitizer" OFF) -@@ -57,19 +65,6 @@ IF(USE_SIMD) - ADD_DEFINITIONS(-DUSE_SIMD) - ENDIF(USE_SIMD) - --IF(DEBUG) -- MESSAGE(STATUS "DEBUG has been set as TRUE ${DEBUG}") -- SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O0 -g -DDEBUG ") -- ADD_DEFINITIONS(-DENABLE_DEBUG) --ELSEIF(NOT DEFINED ENV{DEBUG}) -- MESSAGE(STATUS "Disable debug") -- SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O2 -g ") --ELSE() -- MESSAGE(STATUS "Enable debug") -- SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O0 -g -DDEBUG") -- ADD_DEFINITIONS(-DENABLE_DEBUG) --ENDIF(DEBUG) -- - IF (CONCURRENCY) - MESSAGE(STATUS "CONCURRENCY is ON") - SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DCONCURRENCY") -@@ -164,6 +159,12 @@ ENDIF(WITH_UNIT_TESTS) - - SET(CMAKE_CXX_FLAGS ${CMAKE_COMMON_FLAGS}) - SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS}) -+ -+set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0 -DDEBUG") -+set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2 -DNDEBUG") -+set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O2 -g -DDEBUG") -+set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -Os") -+ - MESSAGE(STATUS "CMAKE_CXX_FLAGS is " ${CMAKE_CXX_FLAGS}) - - # ADD_SUBDIRECTORY(src bin) bin 为目标目录, 可以省略 -diff --git a/build.sh b/build.sh -index 0a914d671..4b4f0f2f7 100755 ---- a/build.sh -+++ b/build.sh -@@ -137,8 +137,8 @@ function do_build - { - TYPE=$1; shift - prepare_build_dir $TYPE || return -- echo "${CMAKE_COMMAND} ${TOPDIR} $@" -- ${CMAKE_COMMAND} -S ${TOPDIR} $@ -+ echo "${CMAKE_COMMAND} $@ ${TOPDIR}" -+ ${CMAKE_COMMAND} $@ -S ${TOPDIR} - } - - function do_clean -@@ -147,23 +147,20 @@ function do_clean - find . -maxdepth 1 -type d -name 'build_*' | xargs rm -rf - } - --function build --{ -- set -- "${BUILD_ARGS[@]}" -- case "x$1" in -- xrelease) -- do_build "$@" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DDEBUG=OFF -- ;; -- xdebug) -- do_build "$@" -DCMAKE_BUILD_TYPE=Debug -DDEBUG=ON -- ;; -- *) -- BUILD_ARGS=(debug "${BUILD_ARGS[@]}") -- build -- ;; -- esac -+function build { -+ # 默认参数是 debug -+ if [ -z "${BUILD_ARGS[0]}" ]; then -+ set -- "debug" # 如果没有参数,则设置默认值 -+ else -+ set -- "${BUILD_ARGS[@]}" # 否则使用 BUILD_ARGS 的第一个参数 -+ fi -+ local build_type_lower=$(echo "$1" | tr '[:upper:]' '[:lower:]') # 转换为小写 -+ echo "Build type: $build_type_lower" # 输出构建类型 -+ -+ do_build "$build_type_lower" -DCMAKE_BUILD_TYPE="$build_type_lower" # 调用 do_build - } - -+ - function main - { - case "$1" in - -From 360240155dd67d397cd406c6e23c543e53bace48 Mon Sep 17 00:00:00 2001 -From: AUA -Date: Mon, 4 Nov 2024 12:27:03 +0000 -Subject: [PATCH 2/3] commit-message: update the cmakelists.txt #432 - ---- - CMakeLists.txt | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/CMakeLists.txt b/CMakeLists.txt -index 1b6540d9b..a6618dc4e 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -161,7 +161,7 @@ SET(CMAKE_CXX_FLAGS ${CMAKE_COMMON_FLAGS}) - SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS}) - - set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0 -DDEBUG") --set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2 -DNDEBUG") -+set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") - set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O2 -g -DDEBUG") - set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -Os") - - -From 8c68a18b6035177e0f30e54569b45f50f0e7e193 Mon Sep 17 00:00:00 2001 -From: AUA -Date: Mon, 4 Nov 2024 12:48:05 +0000 -Subject: [PATCH 3/3] commit-message: update the cmakelists.txt #432 - ---- - build.sh | 4 ++-- - 1 file changed, 2 insertions(+), 2 deletions(-) - -diff --git a/build.sh b/build.sh -index 4b4f0f2f7..e0ba80810 100755 ---- a/build.sh -+++ b/build.sh -@@ -137,8 +137,8 @@ function do_build - { - TYPE=$1; shift - prepare_build_dir $TYPE || return -- echo "${CMAKE_COMMAND} $@ ${TOPDIR}" -- ${CMAKE_COMMAND} $@ -S ${TOPDIR} -+ echo "${CMAKE_COMMAND} ${TOPDIR} $@" -+ ${CMAKE_COMMAND} -S ${TOPDIR} $@ - } - - function do_clean From 6d88bc008eec6bc36444982deb0f57f0d55376b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=90=E6=9D=A5?= Date: Wed, 13 Nov 2024 14:27:09 +0800 Subject: [PATCH 3/7] add some logs --- deps/common/log/log.h | 1 + src/observer/storage/buffer/disk_buffer_pool.cpp | 5 +++-- src/observer/storage/buffer/frame.cpp | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/deps/common/log/log.h b/deps/common/log/log.h index f51b798c8..c31983bf7 100644 --- a/deps/common/log/log.h +++ b/deps/common/log/log.h @@ -319,6 +319,7 @@ int Log::out(const LOG_LEVEL console_level, const LOG_LEVEL log_level, T &msg) do { \ if (!(expression)) { \ LOG_PANIC(description, ##__VA_ARGS__); \ + LOG_PANIC("%s", lbt()); \ assert(expression); \ } \ } while (0) diff --git a/src/observer/storage/buffer/disk_buffer_pool.cpp b/src/observer/storage/buffer/disk_buffer_pool.cpp index 92215cc7f..7ceba56ac 100644 --- a/src/observer/storage/buffer/disk_buffer_pool.cpp +++ b/src/observer/storage/buffer/disk_buffer_pool.cpp @@ -372,6 +372,7 @@ RC DiskBufferPool::allocate_page(Frame **frame) hdr_frame_->set_lsn(lsn); + LOG_INFO("allocate a new page without extend buffer pool. page num=%d, buffer pool=%d", i, id()); lock_.unlock(); return get_this_page(i, frame); } @@ -401,7 +402,7 @@ RC DiskBufferPool::allocate_page(Frame **frame) return rc; } - LOG_INFO("allocate new page. file=%s, pageNum=%d, pin=%d", + LOG_INFO("allocate new page by extending bufferpool. file=%s, pageNum=%d, pin=%d", file_name_.c_str(), page_num, allocated_frame->pin_count()); file_header_->allocated_pages++; @@ -705,7 +706,7 @@ RC DiskBufferPool::allocate_frame(PageNum page_num, Frame **buffer) Frame *frame = frame_manager_.alloc(id(), page_num); if (frame != nullptr) { *buffer = frame; - LOG_DEBUG("allocate frame %p, page num %d", frame, page_num); + LOG_DEBUG("allocate frame %p, page num %d, frame=%s", frame, page_num, frame->to_string().c_str()); return RC::SUCCESS; } diff --git a/src/observer/storage/buffer/frame.cpp b/src/observer/storage/buffer/frame.cpp index b25ec67cc..c61972e3a 100644 --- a/src/observer/storage/buffer/frame.cpp +++ b/src/observer/storage/buffer/frame.cpp @@ -262,6 +262,6 @@ string Frame::to_string() const { stringstream ss; ss << "frame id:" << frame_id().to_string() << ", dirty=" << dirty() << ", pin=" << pin_count() - << ", lsn=" << lsn(); + << ", lsn=" << lsn() << ", this=" << this; return ss.str(); } From 9662d84cbac959c7b2b1763294020afefc2d7a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=90=E6=9D=A5?= Date: Thu, 14 Nov 2024 12:36:58 +0000 Subject: [PATCH 4/7] support addr2line --- deps/common/log/backtrace.cpp | 144 ++++++++++++++++++++++++++++++++++ deps/common/log/backtrace.h | 23 ++++++ deps/common/log/log.cpp | 37 +-------- 3 files changed, 170 insertions(+), 34 deletions(-) create mode 100644 deps/common/log/backtrace.cpp create mode 100644 deps/common/log/backtrace.h diff --git a/deps/common/log/backtrace.cpp b/deps/common/log/backtrace.cpp new file mode 100644 index 000000000..3554f166e --- /dev/null +++ b/deps/common/log/backtrace.cpp @@ -0,0 +1,144 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by WangYunlai on 2024 +// + +#include +#include +#include +#include "common/lang/vector.h" + +namespace common { + +struct ProcMapSegment +{ + uint64_t start_address; + uint64_t end_address; +}; + +class ProcMap +{ +public: + ProcMap() = default; + ~ProcMap() = default; + + int parse(); + int parse_file(const char *file_name); + + const ProcMapSegment *get_segment(const uint64_t address) const; + + uint64_t get_offset(const uint64_t address) const; + +private: + vector segments_; +}; + +int ProcMap::parse() +{ + const char *file_name = "/proc/self/maps"; + return parse_file(file_name); +} + +int ProcMap::parse_file(const char *file_name) +{ + FILE *fp = fopen(file_name, "r"); + if (fp == nullptr) { + return -1; + } + + ProcMapSegment segment; + char line[1024] = {0}; + uint64_t start, end, inode, offset, major, minor; + char perms[8]; + char path[257]; + + while (fgets(line, sizeof(line), fp) != nullptr) { + + int ret = sscanf(line, "%lx-%lx %4s %lx %lx:%lx %ld %255s", + &start, &end, perms, &offset, &major, &minor, &inode, path); + if (ret < 8 || perms[2] != 'x') { + continue; + } + + segment.start_address = start; + segment.end_address = end; + + segments_.push_back(segment); + } + + fclose(fp); + return 0; +} + +const ProcMapSegment *ProcMap::get_segment(const uint64_t address) const +{ + for (const auto &segment : segments_) { + if (address >= segment.start_address && address < segment.end_address) { + return &segment; + } + } + + return nullptr; +} + +uint64_t ProcMap::get_offset(const uint64_t address) const +{ + const ProcMapSegment *segment = get_segment(address); + if (segment == nullptr) { + return address; + } + + return address - segment->start_address; +} + +////////////////////////////////////////////////////////////////////////// +static ProcMap g_proc_map; +int backtrace_init() +{ + return g_proc_map.parse(); +} + +const char *lbt() +{ + constexpr int buffer_size = 100; + void *buffer[buffer_size]; + + constexpr int bt_buffer_size = 8192; + thread_local char backtrace_buffer[bt_buffer_size]; + + int size = backtrace(buffer, buffer_size); + + char **symbol_array = nullptr; +#ifdef LBT_SYMBOLS + /* 有些环境下,使用addr2line 无法根据地址输出符号 */ + symbol_array = backtrace_symbols(buffer, size); +#endif // LBT_SYMBOLS + + int offset = 0; + for (int i = 0; i < size && offset < bt_buffer_size - 1; i++) { + uint64_t address = reinterpret_cast(buffer[i]); + address = g_proc_map.get_offset(address); + const char *format = (0 == i) ? "0x%lx" : " 0x%lx"; + offset += snprintf(backtrace_buffer + offset, sizeof(backtrace_buffer) - offset, format, address); + + if (symbol_array != nullptr) { + offset += snprintf(backtrace_buffer + offset, sizeof(backtrace_buffer) - offset, " %s", symbol_array[i]); + } + } + + if (symbol_array != nullptr) { + free(symbol_array); + } + return backtrace_buffer; +} + +} // namespace common \ No newline at end of file diff --git a/deps/common/log/backtrace.h b/deps/common/log/backtrace.h new file mode 100644 index 000000000..2d9a7cbaf --- /dev/null +++ b/deps/common/log/backtrace.h @@ -0,0 +1,23 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by WangYunlai on 2024 +// + +#pragma once + +namespace common { + +int backtrace_init(); + +const char *lbt(); + +} // namespace common diff --git a/deps/common/log/log.cpp b/deps/common/log/log.cpp index cc1d3c057..c59d19ff1 100644 --- a/deps/common/log/log.cpp +++ b/deps/common/log/log.cpp @@ -13,7 +13,6 @@ See the Mulan PSL v2 for more details. */ // #include -#include #include #include @@ -22,6 +21,8 @@ See the Mulan PSL v2 for more details. */ #include "common/lang/iostream.h" #include "common/lang/new.h" #include "common/log/log.h" +#include "common/log/backtrace.h" + namespace common { Log *g_log = nullptr; @@ -347,40 +348,8 @@ int LoggerFactory::init_default( return 0; } + backtrace_init(); return init(log_file, &g_log, log_level, console_level, rotate_type); } -const char *lbt() -{ - constexpr int buffer_size = 100; - void *buffer[buffer_size]; - - constexpr int bt_buffer_size = 8192; - thread_local char backtrace_buffer[bt_buffer_size]; - - int size = backtrace(buffer, buffer_size); - - char **symbol_array = nullptr; -#ifdef LBT_SYMBOLS - /* 有些环境下,使用addr2line 无法根据地址输出符号 */ - symbol_array = backtrace_symbols(buffer, size); -#endif // LBT_SYMBOLS - - int offset = 0; - for (int i = 0; i < size && offset < bt_buffer_size - 1; i++) { - const char *format = (0 == i) ? "0x%lx" : " 0x%lx"; - offset += snprintf( - backtrace_buffer + offset, sizeof(backtrace_buffer) - offset, format, reinterpret_cast(buffer[i])); - - if (symbol_array != nullptr) { - offset += snprintf(backtrace_buffer + offset, sizeof(backtrace_buffer) - offset, " %s", symbol_array[i]); - } - } - - if (symbol_array != nullptr) { - free(symbol_array); - } - return backtrace_buffer; -} - } // namespace common From b8c034041bf7a3b93d9543906316325bb9ca45ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=90=E6=9D=A5?= Date: Thu, 14 Nov 2024 12:45:44 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs/dev-env/miniob-how-to-debug.md | 39 ++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/docs/docs/dev-env/miniob-how-to-debug.md b/docs/docs/dev-env/miniob-how-to-debug.md index 2e5a3a774..d6e7af32c 100644 --- a/docs/docs/dev-env/miniob-how-to-debug.md +++ b/docs/docs/dev-env/miniob-how-to-debug.md @@ -52,9 +52,9 @@ Table::scan_record Table::create_index ``` -### 打印日志调试 +## 打印日志调试 -miniob提供的日志接口 +### miniob提供的日志接口 ```c++ deps/common/log/log.h: @@ -81,7 +81,40 @@ LOG_FILE_LEVEL=5 LOG_CONSOLE_LEVEL=1 ``` -### gdb调试 +### 在日志中输出调用栈 + +`lbt` 函数可以获取当前调用栈,可以在日志中输出调用栈,方便定位问题。 + +比如 +```c++ +LOG_DEBUG("debug lock %p, lbt=%s", &lock_, lbt()); +``` + +可能得到下面的日志 + +``` +unlock@mutex.cpp:273] >> debug unlock 0xffffa40fe8c0, lbt=0x4589c 0x5517f8 0x5329e0 0x166308 0x162c2c 0x210438 0x204ee0 0x2373b0 0x2373d0 0x203efc 0x203d88 0x223f6c 0x242fc8 0x274810 0x32bd58 0xca028 0x2dbcf8 0x2da614 0xbbf30 0x2cb908 0x2d4408 0x2d43dc 0x2d435c 0x2d431c 0x2d42d4 0x2d4270 0x2d4244 0x2d4224 0xd31fc 0x7d5c8 0xe5edc +``` + +可以用`addr2line`工具来解析地址,比如 + +```bash +addr2line -pCfe ./bin/observer 0x4589c 0x5517f8 0x5329e0 0x166308 0x162c2c 0x210438 0x204ee0 0x2373b0 0x2373d0 0x203efc 0x203d88 0x223f6c 0x242fc8 0x274810 0x32bd58 0xca028 0x2dbcf8 0x2da614 0xbbf30 0x2cb908 0x2d4408 0x2d43dc 0x2d435c 0x2d431c 0x2d42d4 0x2d4270 0x2d4244 0x2d4224 0xd31fc 0x7d5c8 0xe5edc +?? ??:0 +common::lbt() at /root/miniob/deps/common/log/backtrace.cpp:118 +common::DebugMutex::unlock() at /root/miniob/deps/common/lang/mutex.cpp:273 (discriminator 25) +Frame::write_unlatch(long) at /root/miniob/src/observer/storage/buffer/frame.cpp:113 +Frame::write_unlatch() at /root/miniob/src/observer/storage/buffer/frame.cpp:88 +RecordPageHandler::cleanup() at /root/miniob/src/observer/storage/record/record_manager.cpp:262 +RecordPageHandler::~RecordPageHandler() at /root/miniob/src/observer/storage/record/record_manager.cpp:96 +RowRecordPageHandler::~RowRecordPageHandler() at /root/miniob/src/observer/storage/record/record_manager.h:280 +RowRecordPageHandler::~RowRecordPageHandler() at /root/miniob/src/observer/storage/record/record_manager.h:280 +... +``` + +> 注意:./bin/observer 是你的可执行文件路径,这里是一个示例,实际路径可能不同。 + +## gdb调试 调试工具有很多种,但是它们的关键点都是类似的,比如关联到进程、运行时查看变量值、单步运行、跟踪变量等。GDB是在Linux环境中常用的调试工具。其它环境上也有类似的工具,比如LLDB,或者Windows可能使用Visual Studio直接启动调试。Java的调试工具是jdb。 From 86b5f12299c81062fbc9595d75834eacc6768cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=90=E6=9D=A5?= Date: Thu, 14 Nov 2024 13:16:37 +0000 Subject: [PATCH 6/7] fix format bug on mac --- deps/common/log/backtrace.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deps/common/log/backtrace.cpp b/deps/common/log/backtrace.cpp index 3554f166e..d6d614c5b 100644 --- a/deps/common/log/backtrace.cpp +++ b/deps/common/log/backtrace.cpp @@ -15,6 +15,7 @@ See the Mulan PSL v2 for more details. */ #include #include #include +#include #include "common/lang/vector.h" namespace common { @@ -63,7 +64,7 @@ int ProcMap::parse_file(const char *file_name) while (fgets(line, sizeof(line), fp) != nullptr) { - int ret = sscanf(line, "%lx-%lx %4s %lx %lx:%lx %ld %255s", + int ret = sscanf(line, "%" PRIx64 "-%" PRIx64 " %4s %" PRIx64 " %" PRIx64 ":%" PRIx64 "%" PRIu64 "%255s", &start, &end, perms, &offset, &major, &minor, &inode, path); if (ret < 8 || perms[2] != 'x') { continue; From cd3b7d1a9de457c0c82c0672a31a360bbdbaa821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=90=E6=9D=A5?= Date: Thu, 14 Nov 2024 13:18:25 +0000 Subject: [PATCH 7/7] format --- deps/common/log/backtrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/common/log/backtrace.h b/deps/common/log/backtrace.h index 2d9a7cbaf..ac7ab5295 100644 --- a/deps/common/log/backtrace.h +++ b/deps/common/log/backtrace.h @@ -20,4 +20,4 @@ int backtrace_init(); const char *lbt(); -} // namespace common +} // namespace common