From 8c5e37ee4c2b27b7b571e418934333230ab1ff24 Mon Sep 17 00:00:00 2001 From: Yixin Dong Date: Thu, 30 Jan 2025 18:55:56 -0500 Subject: [PATCH] [Debug] Support cpptrace (#176) This PR supports cpptrace as an debug utility. Set `XGRAMMAR_ENABLE_CPPTRACE` to `ON` in config.cmake to enable it. Now it is only supported in Linux. --- .gitmodules | 3 +++ 3rdparty/cpptrace | 1 + CMakeLists.txt | 12 +++++++++++- cmake/config.cmake | 1 + cpp/pybind/CMakeLists.txt | 4 ++++ cpp/support/cpptrace.h | 19 +++++++++++++++++++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 160000 3rdparty/cpptrace create mode 100644 cpp/support/cpptrace.h diff --git a/.gitmodules b/.gitmodules index 6b938c9..5bd60fb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "3rdparty/googletest"] path = 3rdparty/googletest url = https://github.com/google/googletest.git +[submodule "3rdparty/cpptrace"] + path = 3rdparty/cpptrace + url = https://github.com/jeremy-rifkin/cpptrace.git diff --git a/3rdparty/cpptrace b/3rdparty/cpptrace new file mode 160000 index 0000000..6689d14 --- /dev/null +++ b/3rdparty/cpptrace @@ -0,0 +1 @@ +Subproject commit 6689d14c203eed390ae7bb64f56a983cfd7dff9c diff --git a/CMakeLists.txt b/CMakeLists.txt index bc9aa06..1088070 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,11 @@ else() endif() option(XGRAMMAR_BUILD_PYTHON_BINDINGS "Build Python bindings" ON) -option(XGRAMMAR_BUILD_CXX_TESTS "Build C++ tests" ON) +option(XGRAMMAR_BUILD_CXX_TESTS "Build C++ tests" OFF) +option(XGRAMMAR_ENABLE_CPPTRACE + "Enable C++ trace (Now only support Linux, and RelWithDebugInfo or Debug build)" OFF +) + set(XGRAMMAR_CUDA_ARCHITECTURES native CACHE STRING "CUDA architectures" @@ -61,6 +65,12 @@ list(FILTER XGRAMMAR_SOURCES_PATH EXCLUDE REGEX "${PROJECT_SOURCE_DIR}/cpp/pybin add_library(xgrammar STATIC ${XGRAMMAR_SOURCES_PATH}) target_include_directories(xgrammar PUBLIC ${XGRAMMAR_INCLUDE_PATH}) +# link to cpptrace +if(XGRAMMAR_ENABLE_CPPTRACE) + add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/cpptrace) + target_link_libraries(xgrammar PUBLIC cpptrace::cpptrace) +endif() + if(XGRAMMAR_BUILD_PYTHON_BINDINGS) add_subdirectory(${PROJECT_SOURCE_DIR}/cpp/pybind) endif() diff --git a/cmake/config.cmake b/cmake/config.cmake index b47e5f9..20bd9f8 100644 --- a/cmake/config.cmake +++ b/cmake/config.cmake @@ -1,3 +1,4 @@ set(CMAKE_BUILD_TYPE RelWithDebInfo) set(XGRAMMAR_BUILD_PYTHON_BINDINGS ON) set(XGRAMMAR_BUILD_CXX_TESTS OFF) +set(XGRAMMAR_ENABLE_CPPTRACE OFF) diff --git a/cpp/pybind/CMakeLists.txt b/cpp/pybind/CMakeLists.txt index 721beb1..5d3b843 100644 --- a/cpp/pybind/CMakeLists.txt +++ b/cpp/pybind/CMakeLists.txt @@ -21,6 +21,10 @@ file(GLOB_RECURSE XGRAMMAR_BINDINGS_PATH ${PROJECT_SOURCE_DIR}/cpp/*.cc) pybind11_add_module(xgrammar_bindings ${XGRAMMAR_BINDINGS_PATH}) target_include_directories(xgrammar_bindings PUBLIC ${XGRAMMAR_INCLUDE_PATH}) +if(XGRAMMAR_ENABLE_CPPTRACE) + target_link_libraries(xgrammar_bindings PUBLIC cpptrace::cpptrace) +endif() + set(LIB_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/python/xgrammar") set_target_properties(xgrammar_bindings PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${LIB_OUTPUT_DIRECTORY}) set_target_properties( diff --git a/cpp/support/cpptrace.h b/cpp/support/cpptrace.h new file mode 100644 index 0000000..fb4b4e6 --- /dev/null +++ b/cpp/support/cpptrace.h @@ -0,0 +1,19 @@ +/*! + * Copyright (c) 2024 by Contributors + * \file xgrammar/support/cpptrace.h + * \details This file is an encapsulation of the cpptrace library. It helps debugging. This file + * can only be included when XGRAMMAR_ENABLE_CPPTRACE is set to ON, and only support Linux and + * RelWithDebugInfo or Debug build. + */ +#ifndef XGRAMMAR_SUPPORT_CPPTRACE_H_ +#define XGRAMMAR_SUPPORT_CPPTRACE_H_ + +#include + +namespace xgrammar { + +inline void PrintTrace() { cpptrace::generate_trace().print(); } + +} // namespace xgrammar + +#endif // XGRAMMAR_SUPPORT_CPPTRACE_H_