Skip to content

Commit

Permalink
[Debug] Support cpptrace (#176)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ubospica authored Jan 30, 2025
1 parent f062f54 commit 8c5e37e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions 3rdparty/cpptrace
Submodule cpptrace added at 6689d1
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions cpp/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions cpp/support/cpptrace.h
Original file line number Diff line number Diff line change
@@ -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 <cpptrace/cpptrace.hpp>

namespace xgrammar {

inline void PrintTrace() { cpptrace::generate_trace().print(); }

} // namespace xgrammar

#endif // XGRAMMAR_SUPPORT_CPPTRACE_H_

0 comments on commit 8c5e37e

Please sign in to comment.