Skip to content

Commit 8c5e37e

Browse files
authored
[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.
1 parent f062f54 commit 8c5e37e

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "3rdparty/googletest"]
55
path = 3rdparty/googletest
66
url = https://github.com/google/googletest.git
7+
[submodule "3rdparty/cpptrace"]
8+
path = 3rdparty/cpptrace
9+
url = https://github.com/jeremy-rifkin/cpptrace.git

3rdparty/cpptrace

Submodule cpptrace added at 6689d14

CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ else()
1515
endif()
1616

1717
option(XGRAMMAR_BUILD_PYTHON_BINDINGS "Build Python bindings" ON)
18-
option(XGRAMMAR_BUILD_CXX_TESTS "Build C++ tests" ON)
18+
option(XGRAMMAR_BUILD_CXX_TESTS "Build C++ tests" OFF)
19+
option(XGRAMMAR_ENABLE_CPPTRACE
20+
"Enable C++ trace (Now only support Linux, and RelWithDebugInfo or Debug build)" OFF
21+
)
22+
1923
set(XGRAMMAR_CUDA_ARCHITECTURES
2024
native
2125
CACHE STRING "CUDA architectures"
@@ -61,6 +65,12 @@ list(FILTER XGRAMMAR_SOURCES_PATH EXCLUDE REGEX "${PROJECT_SOURCE_DIR}/cpp/pybin
6165
add_library(xgrammar STATIC ${XGRAMMAR_SOURCES_PATH})
6266
target_include_directories(xgrammar PUBLIC ${XGRAMMAR_INCLUDE_PATH})
6367

68+
# link to cpptrace
69+
if(XGRAMMAR_ENABLE_CPPTRACE)
70+
add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/cpptrace)
71+
target_link_libraries(xgrammar PUBLIC cpptrace::cpptrace)
72+
endif()
73+
6474
if(XGRAMMAR_BUILD_PYTHON_BINDINGS)
6575
add_subdirectory(${PROJECT_SOURCE_DIR}/cpp/pybind)
6676
endif()

cmake/config.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
set(CMAKE_BUILD_TYPE RelWithDebInfo)
22
set(XGRAMMAR_BUILD_PYTHON_BINDINGS ON)
33
set(XGRAMMAR_BUILD_CXX_TESTS OFF)
4+
set(XGRAMMAR_ENABLE_CPPTRACE OFF)

cpp/pybind/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ file(GLOB_RECURSE XGRAMMAR_BINDINGS_PATH ${PROJECT_SOURCE_DIR}/cpp/*.cc)
2121
pybind11_add_module(xgrammar_bindings ${XGRAMMAR_BINDINGS_PATH})
2222
target_include_directories(xgrammar_bindings PUBLIC ${XGRAMMAR_INCLUDE_PATH})
2323

24+
if(XGRAMMAR_ENABLE_CPPTRACE)
25+
target_link_libraries(xgrammar_bindings PUBLIC cpptrace::cpptrace)
26+
endif()
27+
2428
set(LIB_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/python/xgrammar")
2529
set_target_properties(xgrammar_bindings PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${LIB_OUTPUT_DIRECTORY})
2630
set_target_properties(

cpp/support/cpptrace.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*!
2+
* Copyright (c) 2024 by Contributors
3+
* \file xgrammar/support/cpptrace.h
4+
* \details This file is an encapsulation of the cpptrace library. It helps debugging. This file
5+
* can only be included when XGRAMMAR_ENABLE_CPPTRACE is set to ON, and only support Linux and
6+
* RelWithDebugInfo or Debug build.
7+
*/
8+
#ifndef XGRAMMAR_SUPPORT_CPPTRACE_H_
9+
#define XGRAMMAR_SUPPORT_CPPTRACE_H_
10+
11+
#include <cpptrace/cpptrace.hpp>
12+
13+
namespace xgrammar {
14+
15+
inline void PrintTrace() { cpptrace::generate_trace().print(); }
16+
17+
} // namespace xgrammar
18+
19+
#endif // XGRAMMAR_SUPPORT_CPPTRACE_H_

0 commit comments

Comments
 (0)