Skip to content

Commit 438d855

Browse files
committed
add backtrace support
Signed-off-by: Alex Chi <[email protected]> fix clang-tidy Signed-off-by: Alex Chi <[email protected]>
1 parent 27d0e6d commit 438d855

File tree

11 files changed

+471
-128
lines changed

11 files changed

+471
-128
lines changed

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ set(BUSTUB_THIRD_PARTY_INCLUDE_DIR
138138
${PROJECT_SOURCE_DIR}/third_party/libpg_query/include
139139
${PROJECT_SOURCE_DIR}/third_party/argparse/include
140140
${PROJECT_SOURCE_DIR}/third_party/cpp_random_distributions
141+
${PROJECT_SOURCE_DIR}/third_party/backward-cpp/include
141142
)
142143

143144
include_directories(${BUSTUB_SRC_INCLUDE_DIR} ${BUSTUB_TEST_INCLUDE_DIR} ${BUSTUB_THIRD_PARTY_INCLUDE_DIR})
@@ -151,9 +152,13 @@ endfunction()
151152
# Other CMake modules
152153
# MUST BE ADDED AFTER CONFIGURING COMPILER PARAMETERS
153154
# #####################################################################################################################
155+
set(CMAKE_MODULE_PATH "${BUSTUB_BUILD_SUPPORT_DIR}/cmake;${CMAKE_MODULE_PATH}")
156+
find_package(LibElf)
157+
find_package(LibDwarf)
158+
159+
add_subdirectory(third_party)
154160
add_subdirectory(src)
155161
add_subdirectory(test)
156-
add_subdirectory(third_party)
157162
add_subdirectory(tools)
158163

159164
# #####################################################################################################################
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Copyright (c) 2004-present, Facebook, Inc.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the MIT license found in the
5+
# LICENSE file in the hphp/hsl/ subdirectory of this source tree.
6+
7+
# - Try to find libdwarf
8+
# Once done this will define
9+
#
10+
# LIBDWARF_FOUND - system has libdwarf
11+
# LIBDWARF_INCLUDE_DIRS - the libdwarf include directory
12+
# LIBDWARF_LIBRARIES - Link these to use libdwarf
13+
# LIBDWARF_DEFINITIONS - Compiler switches required for using libdwarf
14+
#
15+
16+
# Locate libelf library at first
17+
if(NOT LIBELF_FOUND)
18+
find_package(LibElf)
19+
endif(NOT LIBELF_FOUND)
20+
21+
if(LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)
22+
set(LibDwarf_FIND_QUIETLY TRUE)
23+
endif(LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)
24+
25+
find_package(PkgConfig)
26+
pkg_check_modules(PkgConfig_LibDwarf QUIET libdwarf)
27+
28+
find_path(DWARF_INCLUDE_DIR
29+
NAMES
30+
libdwarf.h dwarf.h
31+
PATHS
32+
${PkgConfig_LibDwarf_INCLUDE_DIRS}
33+
/usr/include
34+
/usr/include/libdwarf
35+
/usr/local/include
36+
/usr/local/include/libdwarf
37+
/opt/local/include
38+
/sw/include
39+
ENV CPATH) # PATH and INCLUDE will also work
40+
41+
if(DWARF_INCLUDE_DIR)
42+
set(LIBDWARF_INCLUDE_DIRS ${DWARF_INCLUDE_DIR})
43+
endif()
44+
45+
find_library(LIBDWARF_LIBRARIES
46+
NAMES
47+
dwarf libdwarf
48+
PATHS
49+
/usr/lib
50+
/usr/local/lib
51+
/opt/local/lib
52+
/sw/lib
53+
${PkgConfig_LibDwarf_LIBRARY_DIRS}
54+
ENV LIBRARY_PATH # PATH and LIB will also work
55+
ENV LD_LIBRARY_PATH)
56+
include(FindPackageHandleStandardArgs)
57+
58+
# handle the QUIETLY and REQUIRED arguments and set LIBDWARF_FOUND to TRUE
59+
# if all listed variables are TRUE
60+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibDwarf DEFAULT_MSG
61+
LIBELF_FOUND
62+
LIBDWARF_LIBRARIES
63+
LIBDWARF_INCLUDE_DIRS)
64+
65+
if(LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)
66+
set(CMAKE_REQUIRED_INCLUDES ${LIBDWARF_INCLUDE_DIRS})
67+
set(CMAKE_REQUIRED_LIBRARIES ${LIBDWARF_LIBRARIES} ${LIBELF_LIBRARIES})
68+
69+
# libdwarf makes breaking changes occasionally and doesn't provide an easy
70+
# way to test for them. The following checks should detect the changes and
71+
# pass that information on accordingly.
72+
INCLUDE(CheckCXXSourceCompiles)
73+
INCLUDE(CheckFunctionExists)
74+
75+
MACRO(CHECK_LIBDWARF_INIT init params var)
76+
# Check for the existence of this particular init function.
77+
unset(INIT_EXISTS CACHE)
78+
CHECK_FUNCTION_EXISTS(${init} INIT_EXISTS)
79+
80+
if(INIT_EXISTS)
81+
set(LIBDWARF_USE_INIT_C ${var})
82+
83+
# Check to see if we can use a const name.
84+
unset(DW_CONST CACHE)
85+
86+
if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
87+
# -std=c++11 is already set in HPHPCompiler.cmake, don't
88+
# add -std=c++0x on top of that or clang will give errors
89+
set(CMAKE_REQUIRED_FLAGS "-std=c++0x")
90+
endif()
91+
92+
CHECK_CXX_SOURCE_COMPILES("
93+
#include <libdwarf.h>
94+
#include <cstddef>
95+
int dwarfCallback(const char * a, int b, Dwarf_Unsigned c,
96+
Dwarf_Unsigned d, Dwarf_Unsigned e, Dwarf_Unsigned f,
97+
Dwarf_Unsigned * g, Dwarf_Ptr h, int * i) {}
98+
int main() { ${init}(${params}); return 0; }" DW_CONST)
99+
100+
if(DW_CONST)
101+
set(LIBDWARF_CONST_NAME 1)
102+
else()
103+
set(LIBDWARF_CONST_NAME 0)
104+
endif()
105+
endif()
106+
ENDMACRO(CHECK_LIBDWARF_INIT)
107+
108+
# Order is important, last one is used.
109+
CHECK_LIBDWARF_INIT("dwarf_producer_init"
110+
"0, dwarfCallback, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr" 0)
111+
CHECK_LIBDWARF_INIT("dwarf_producer_init_c" "0, dwarfCallback, nullptr, nullptr, nullptr, nullptr" 1)
112+
113+
set(CMAKE_REQUIRED_INCLUDES)
114+
set(CMAKE_REQUIRED_LIBRARIES)
115+
endif()
116+
117+
if(LIBDWARF_CONST_NAME)
118+
message(STATUS "libdwarf uses const char* type")
119+
else()
120+
message(STATUS "libdwarf uses char* type")
121+
endif()
122+
123+
if(LIBDWARF_USE_INIT_C)
124+
message(STATUS "libdwarf has dwarf_producer_init_c")
125+
else()
126+
message(STATUS "libdwarf does not have dwarf_producer_init_c, using dwarf_producer_init")
127+
endif()
128+
129+
mark_as_advanced(LIBDW_INCLUDE_DIR DWARF_INCLUDE_DIR)
130+
mark_as_advanced(LIBDWARF_INCLUDE_DIRS LIBDWARF_LIBRARIES)
131+
mark_as_advanced(LIBDWARF_CONST_NAME LIBDWARF_USE_INIT_C)

build_support/cmake/FindLibElf.cmake

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# - Try to find libelf
2+
# Once done this will define
3+
#
4+
# LIBELF_FOUND - system has libelf
5+
# LIBELF_INCLUDE_DIRS - the libelf include directory
6+
# LIBELF_LIBRARIES - Link these to use libelf
7+
# LIBELF_DEFINITIONS - Compiler switches required for using libelf
8+
#
9+
# Copyright (c) 2008 Bernhard Walle <[email protected]>
10+
#
11+
# Redistribution and use is allowed according to the terms of the New
12+
# BSD license.
13+
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
14+
#
15+
16+
if(LIBELF_LIBRARIES AND LIBELF_INCLUDE_DIRS)
17+
set(LibElf_FIND_QUIETLY TRUE)
18+
endif(LIBELF_LIBRARIES AND LIBELF_INCLUDE_DIRS)
19+
20+
find_package(PkgConfig)
21+
pkg_check_modules(PkgConfig_LibElf QUIET libelf)
22+
23+
find_path(LIBELF_INCLUDE_DIRS
24+
NAMES
25+
libelf.h
26+
PATHS
27+
${PkgConfig_LibElf_INCLUDE_DIRS}
28+
/usr/include
29+
/usr/include/libelf
30+
/usr/local/include
31+
/usr/local/include/libelf
32+
/opt/local/include
33+
/opt/local/include/libelf
34+
/sw/include
35+
/sw/include/libelf
36+
ENV CPATH)
37+
38+
find_library(LIBELF_LIBRARIES
39+
NAMES
40+
elf
41+
PATHS
42+
/usr/lib
43+
/usr/local/lib
44+
/opt/local/lib
45+
/sw/lib
46+
${PkgConfig_LibElf_LIBRARY_DIRS}
47+
ENV LIBRARY_PATH
48+
ENV LD_LIBRARY_PATH)
49+
50+
include(FindPackageHandleStandardArgs)
51+
52+
# handle the QUIETLY and REQUIRED arguments and set LIBELF_FOUND to TRUE if all listed variables are TRUE
53+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibElf DEFAULT_MSG
54+
LIBELF_LIBRARIES
55+
LIBELF_INCLUDE_DIRS)
56+
57+
SET(CMAKE_REQUIRED_LIBRARIES elf)
58+
INCLUDE(CheckCXXSourceCompiles)
59+
CHECK_CXX_SOURCE_COMPILES("#include <libelf.h>
60+
int main() {
61+
Elf *e = (Elf*)0;
62+
size_t sz;
63+
elf_getshdrstrndx(e, &sz);
64+
return 0;
65+
}" ELF_GETSHDRSTRNDX)
66+
SET(CMAKE_REQUIRED_LIBRARIES)
67+
68+
mark_as_advanced(LIBELF_INCLUDE_DIRS LIBELF_LIBRARIES ELF_GETSHDRSTRNDX)

build_support/packages.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ install_mac() {
7979
brew ls --versions doxygen || brew install doxygen
8080
brew ls --versions git || brew install git
8181
(brew ls --versions llvm | grep 14) || brew install llvm@14
82+
brew ls --versions libelf || brew install libelf
8283
}
8384

8485
install_linux() {
@@ -94,7 +95,9 @@ install_linux() {
9495
doxygen \
9596
git \
9697
pkg-config \
97-
zlib1g-dev
98+
zlib1g-dev \
99+
libelf-dev \
100+
libdwarf-dev
98101
}
99102

100103
main "$@"

0 commit comments

Comments
 (0)