Skip to content

Commit

Permalink
add backtrace support
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi <[email protected]>

fix clang-tidy

Signed-off-by: Alex Chi <[email protected]>
  • Loading branch information
skyzh committed Aug 26, 2023
1 parent 27d0e6d commit 438d855
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 128 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ set(BUSTUB_THIRD_PARTY_INCLUDE_DIR
${PROJECT_SOURCE_DIR}/third_party/libpg_query/include
${PROJECT_SOURCE_DIR}/third_party/argparse/include
${PROJECT_SOURCE_DIR}/third_party/cpp_random_distributions
${PROJECT_SOURCE_DIR}/third_party/backward-cpp/include
)

include_directories(${BUSTUB_SRC_INCLUDE_DIR} ${BUSTUB_TEST_INCLUDE_DIR} ${BUSTUB_THIRD_PARTY_INCLUDE_DIR})
Expand All @@ -151,9 +152,13 @@ endfunction()
# Other CMake modules
# MUST BE ADDED AFTER CONFIGURING COMPILER PARAMETERS
# #####################################################################################################################
set(CMAKE_MODULE_PATH "${BUSTUB_BUILD_SUPPORT_DIR}/cmake;${CMAKE_MODULE_PATH}")
find_package(LibElf)
find_package(LibDwarf)

add_subdirectory(third_party)
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(third_party)
add_subdirectory(tools)

# #####################################################################################################################
Expand Down
131 changes: 131 additions & 0 deletions build_support/cmake/FindLibDwarf.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Copyright (c) 2004-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the hphp/hsl/ subdirectory of this source tree.

# - Try to find libdwarf
# Once done this will define
#
# LIBDWARF_FOUND - system has libdwarf
# LIBDWARF_INCLUDE_DIRS - the libdwarf include directory
# LIBDWARF_LIBRARIES - Link these to use libdwarf
# LIBDWARF_DEFINITIONS - Compiler switches required for using libdwarf
#

# Locate libelf library at first
if(NOT LIBELF_FOUND)
find_package(LibElf)
endif(NOT LIBELF_FOUND)

if(LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)
set(LibDwarf_FIND_QUIETLY TRUE)
endif(LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)

find_package(PkgConfig)
pkg_check_modules(PkgConfig_LibDwarf QUIET libdwarf)

find_path(DWARF_INCLUDE_DIR
NAMES
libdwarf.h dwarf.h
PATHS
${PkgConfig_LibDwarf_INCLUDE_DIRS}
/usr/include
/usr/include/libdwarf
/usr/local/include
/usr/local/include/libdwarf
/opt/local/include
/sw/include
ENV CPATH) # PATH and INCLUDE will also work

if(DWARF_INCLUDE_DIR)
set(LIBDWARF_INCLUDE_DIRS ${DWARF_INCLUDE_DIR})
endif()

find_library(LIBDWARF_LIBRARIES
NAMES
dwarf libdwarf
PATHS
/usr/lib
/usr/local/lib
/opt/local/lib
/sw/lib
${PkgConfig_LibDwarf_LIBRARY_DIRS}
ENV LIBRARY_PATH # PATH and LIB will also work
ENV LD_LIBRARY_PATH)
include(FindPackageHandleStandardArgs)

# handle the QUIETLY and REQUIRED arguments and set LIBDWARF_FOUND to TRUE
# if all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibDwarf DEFAULT_MSG
LIBELF_FOUND
LIBDWARF_LIBRARIES
LIBDWARF_INCLUDE_DIRS)

if(LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)
set(CMAKE_REQUIRED_INCLUDES ${LIBDWARF_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES ${LIBDWARF_LIBRARIES} ${LIBELF_LIBRARIES})

# libdwarf makes breaking changes occasionally and doesn't provide an easy
# way to test for them. The following checks should detect the changes and
# pass that information on accordingly.
INCLUDE(CheckCXXSourceCompiles)
INCLUDE(CheckFunctionExists)

MACRO(CHECK_LIBDWARF_INIT init params var)
# Check for the existence of this particular init function.
unset(INIT_EXISTS CACHE)
CHECK_FUNCTION_EXISTS(${init} INIT_EXISTS)

if(INIT_EXISTS)
set(LIBDWARF_USE_INIT_C ${var})

# Check to see if we can use a const name.
unset(DW_CONST CACHE)

if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# -std=c++11 is already set in HPHPCompiler.cmake, don't
# add -std=c++0x on top of that or clang will give errors
set(CMAKE_REQUIRED_FLAGS "-std=c++0x")
endif()

CHECK_CXX_SOURCE_COMPILES("
#include <libdwarf.h>
#include <cstddef>
int dwarfCallback(const char * a, int b, Dwarf_Unsigned c,
Dwarf_Unsigned d, Dwarf_Unsigned e, Dwarf_Unsigned f,
Dwarf_Unsigned * g, Dwarf_Ptr h, int * i) {}
int main() { ${init}(${params}); return 0; }" DW_CONST)

if(DW_CONST)
set(LIBDWARF_CONST_NAME 1)
else()
set(LIBDWARF_CONST_NAME 0)
endif()
endif()
ENDMACRO(CHECK_LIBDWARF_INIT)

# Order is important, last one is used.
CHECK_LIBDWARF_INIT("dwarf_producer_init"
"0, dwarfCallback, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr" 0)
CHECK_LIBDWARF_INIT("dwarf_producer_init_c" "0, dwarfCallback, nullptr, nullptr, nullptr, nullptr" 1)

set(CMAKE_REQUIRED_INCLUDES)
set(CMAKE_REQUIRED_LIBRARIES)
endif()

if(LIBDWARF_CONST_NAME)
message(STATUS "libdwarf uses const char* type")
else()
message(STATUS "libdwarf uses char* type")
endif()

if(LIBDWARF_USE_INIT_C)
message(STATUS "libdwarf has dwarf_producer_init_c")
else()
message(STATUS "libdwarf does not have dwarf_producer_init_c, using dwarf_producer_init")
endif()

mark_as_advanced(LIBDW_INCLUDE_DIR DWARF_INCLUDE_DIR)
mark_as_advanced(LIBDWARF_INCLUDE_DIRS LIBDWARF_LIBRARIES)
mark_as_advanced(LIBDWARF_CONST_NAME LIBDWARF_USE_INIT_C)
68 changes: 68 additions & 0 deletions build_support/cmake/FindLibElf.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# - Try to find libelf
# Once done this will define
#
# LIBELF_FOUND - system has libelf
# LIBELF_INCLUDE_DIRS - the libelf include directory
# LIBELF_LIBRARIES - Link these to use libelf
# LIBELF_DEFINITIONS - Compiler switches required for using libelf
#
# Copyright (c) 2008 Bernhard Walle <[email protected]>
#
# Redistribution and use is allowed according to the terms of the New
# BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#

if(LIBELF_LIBRARIES AND LIBELF_INCLUDE_DIRS)
set(LibElf_FIND_QUIETLY TRUE)
endif(LIBELF_LIBRARIES AND LIBELF_INCLUDE_DIRS)

find_package(PkgConfig)
pkg_check_modules(PkgConfig_LibElf QUIET libelf)

find_path(LIBELF_INCLUDE_DIRS
NAMES
libelf.h
PATHS
${PkgConfig_LibElf_INCLUDE_DIRS}
/usr/include
/usr/include/libelf
/usr/local/include
/usr/local/include/libelf
/opt/local/include
/opt/local/include/libelf
/sw/include
/sw/include/libelf
ENV CPATH)

find_library(LIBELF_LIBRARIES
NAMES
elf
PATHS
/usr/lib
/usr/local/lib
/opt/local/lib
/sw/lib
${PkgConfig_LibElf_LIBRARY_DIRS}
ENV LIBRARY_PATH
ENV LD_LIBRARY_PATH)

include(FindPackageHandleStandardArgs)

# handle the QUIETLY and REQUIRED arguments and set LIBELF_FOUND to TRUE if all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibElf DEFAULT_MSG
LIBELF_LIBRARIES
LIBELF_INCLUDE_DIRS)

SET(CMAKE_REQUIRED_LIBRARIES elf)
INCLUDE(CheckCXXSourceCompiles)
CHECK_CXX_SOURCE_COMPILES("#include <libelf.h>
int main() {
Elf *e = (Elf*)0;
size_t sz;
elf_getshdrstrndx(e, &sz);
return 0;
}" ELF_GETSHDRSTRNDX)
SET(CMAKE_REQUIRED_LIBRARIES)

mark_as_advanced(LIBELF_INCLUDE_DIRS LIBELF_LIBRARIES ELF_GETSHDRSTRNDX)
5 changes: 4 additions & 1 deletion build_support/packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ install_mac() {
brew ls --versions doxygen || brew install doxygen
brew ls --versions git || brew install git
(brew ls --versions llvm | grep 14) || brew install llvm@14
brew ls --versions libelf || brew install libelf
}

install_linux() {
Expand All @@ -94,7 +95,9 @@ install_linux() {
doxygen \
git \
pkg-config \
zlib1g-dev
zlib1g-dev \
libelf-dev \
libdwarf-dev
}

main "$@"
Loading

0 comments on commit 438d855

Please sign in to comment.