beanz created this revision. Herald added a subscriber: mgorny. This patch refactors the CMake build system's support for building debugserver to allow us to build the majority of debugserver's sources into the debugserverCommon library which can then be reused by unit tests.
The first unit test I've written tests debug server's ability to accept incoming connections from LLDB. The test forks the process, and one side creates a listening socket using debugserver's socket API, the other side creates a transmitting socket using LLDB's TCPSocket class. I have no clue where to even start getting this connected into the LLDB Xcode project, so for now these tests are CMake-only. https://reviews.llvm.org/D31357 Files: tools/debugserver/source/CMakeLists.txt tools/debugserver/source/MacOSX/CMakeLists.txt unittests/CMakeLists.txt unittests/debugserver/CMakeLists.txt unittests/debugserver/RNBSocketTest.cpp unittests/debugserver/debugserver_LogCallback.cpp
Index: unittests/debugserver/debugserver_LogCallback.cpp =================================================================== --- /dev/null +++ unittests/debugserver/debugserver_LogCallback.cpp @@ -0,0 +1,20 @@ +//===-- debugserver_LogCallback.cpp -----------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +//------------------------------------------------------------------------------ +// this function is defined in debugserver.cpp, but is needed to link the +// debugserver Common library. It is for logging only, so it is left +// unimplemented here. +//------------------------------------------------------------------------------ + +#include <stdint.h> +#include <stdarg.h> + +void FileLogCallback(void *baton, uint32_t flags, const char *format, + va_list args) {} \ No newline at end of file Index: unittests/debugserver/RNBSocketTest.cpp =================================================================== --- /dev/null +++ unittests/debugserver/RNBSocketTest.cpp @@ -0,0 +1,35 @@ +//===-- RNBSocketTest.cpp ---------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" + +#include <sys/sysctl.h> +#include <unistd.h> + +#include "RNBSocket.h" +#include "lldb/Host/Socket.h" + +using namespace lldb_private; + +static void ServerCallback(const void *baton, in_port_t port) {} + +TEST(RNBSocket, LoopBackListenIPv4) { + auto ChildPID = fork(); + if (ChildPID != 0) { + RNBSocket ServerSocket; + auto result = ServerSocket.Listen("localhost", 4242, ServerCallback, NULL); + ASSERT_FALSE(result); + } else { + Socket *ClientSocket; + Error Err = Socket::TcpConnect("localhost:4242", false, ClientSocket); + if (Err.Fail()) + abort(); + exit(0); + } +} Index: unittests/debugserver/CMakeLists.txt =================================================================== --- /dev/null +++ unittests/debugserver/CMakeLists.txt @@ -0,0 +1,13 @@ +include_directories(${LLDB_SOURCE_DIR}/tools/debugserver/source + ${LLDB_SOURCE_DIR}/tools/debugserver/source/MacOSX) + +add_lldb_unittest(debugserverTests + RNBSocketTest.cpp + debugserver_LogCallback.cpp + + LINK_LIBS + lldbDebugserverCommon + lldbHost + LINK_COMPONENTS + Support + ) Index: unittests/CMakeLists.txt =================================================================== --- unittests/CMakeLists.txt +++ unittests/CMakeLists.txt @@ -70,3 +70,7 @@ add_subdirectory(Target) add_subdirectory(UnwindAssembly) add_subdirectory(Utility) + +if(LLDB_CAN_USE_DEBUGSERVER) + add_subdirectory(debugserver) +endif() \ No newline at end of file Index: tools/debugserver/source/MacOSX/CMakeLists.txt =================================================================== --- tools/debugserver/source/MacOSX/CMakeLists.txt +++ tools/debugserver/source/MacOSX/CMakeLists.txt @@ -6,70 +6,3 @@ add_subdirectory(DarwinLog) include_directories(..) - -set(generated_mach_interfaces - ${CMAKE_CURRENT_BINARY_DIR}/mach_exc.h - ${CMAKE_CURRENT_BINARY_DIR}/mach_excServer.c - ${CMAKE_CURRENT_BINARY_DIR}/mach_excUser.c - ) -add_custom_command(OUTPUT ${generated_mach_interfaces} - COMMAND mig ${CMAKE_CURRENT_SOURCE_DIR}/dbgnub-mig.defs - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/dbgnub-mig.defs - ) - -set(DEBUGSERVER_VERS_GENERATED_FILE ${CMAKE_CURRENT_BINARY_DIR}/debugserver_vers.c) -set_source_files_properties(${DEBUGSERVER_VERS_GENERATED_FILE} PROPERTIES GENERATED 1) - -add_custom_command(OUTPUT ${DEBUGSERVER_VERS_GENERATED_FILE} - COMMAND ${LLDB_SOURCE_DIR}/scripts/generate-vers.pl - ${LLDB_SOURCE_DIR}/lldb.xcodeproj/project.pbxproj debugserver - > ${DEBUGSERVER_VERS_GENERATED_FILE} - DEPENDS ${LLDB_SOURCE_DIR}/scripts/generate-vers.pl - ${LLDB_SOURCE_DIR}/lldb.xcodeproj/project.pbxproj - ) - -add_lldb_tool(debugserver INCLUDE_IN_FRAMEWORK - CFBundle.cpp - CFString.cpp - Genealogy.cpp - MachException.cpp - MachProcess.mm - MachTask.mm - MachThread.cpp - MachThreadList.cpp - MachVMMemory.cpp - MachVMRegion.cpp - OsLogger.cpp - ${generated_mach_interfaces} - ${DEBUGSERVER_VERS_GENERATED_FILE} - - LINK_LIBS - lldbDebugserverCommon - lldbDebugserverMacOSX_I386 - lldbDebugserverMacOSX_X86_64 - lldbDebugserverMacOSX_DarwinLog - ) - -set_source_files_properties( - HasAVX.s - # Necessary since compilation will fail with stand-alone assembler - PROPERTIES LANGUAGE C COMPILE_FLAGS "-x assembler-with-cpp" - ) - -set(LLDB_CODESIGN_IDENTITY "lldb_codesign" - CACHE STRING "Identity used for code signing. Set to empty string to skip the signing step.") -if (NOT ("${LLDB_CODESIGN_IDENTITY}" STREQUAL "")) - execute_process( - COMMAND xcrun -f codesign_allocate - OUTPUT_STRIP_TRAILING_WHITESPACE - OUTPUT_VARIABLE CODESIGN_ALLOCATE - ) - add_custom_command(TARGET debugserver - POST_BUILD - # Note: --entitlements option removed (see comment above). - COMMAND ${CMAKE_COMMAND} -E env CODESIGN_ALLOCATE=${CODESIGN_ALLOCATE} - codesign --force --sign ${LLDB_CODESIGN_IDENTITY} - $<TARGET_FILE:debugserver> - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin - ) -endif() Index: tools/debugserver/source/CMakeLists.txt =================================================================== --- tools/debugserver/source/CMakeLists.txt +++ tools/debugserver/source/CMakeLists.txt @@ -2,12 +2,10 @@ include_directories(${LLDB_SOURCE_DIR}/source) include_directories(MacOSX/DarwinLog) -if (CMAKE_SYSTEM_NAME MATCHES "Darwin") - include_directories(MacOSX) - #include_directories(${CMAKE_CURRENT_BINARY_DIR}/MacOSX) +include_directories(MacOSX) +#include_directories(${CMAKE_CURRENT_BINARY_DIR}/MacOSX) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -Wl,-sectcreate,__TEXT,__info_plist,${CMAKE_CURRENT_SOURCE_DIR}/../resources/lldb-debugserver-Info.plist") -endif() +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -Wl,-sectcreate,__TEXT,__info_plist,${CMAKE_CURRENT_SOURCE_DIR}/../resources/lldb-debugserver-Info.plist") check_cxx_compiler_flag("-Wno-gnu-zero-variadic-macro-arguments" CXX_SUPPORTS_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS) @@ -27,19 +25,31 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-extended-offsetof") endif () -if (NOT CMAKE_SYSTEM_NAME MATCHES "Darwin") - add_definitions( - -DDEBUGSERVER_VERSION_STR="${LLDB_VERSION}" - ) -endif () +find_library(COCOA_LIBRARY Cocoa) +add_subdirectory(MacOSX) -if (CMAKE_SYSTEM_NAME MATCHES "Darwin") - find_library(COCOA_LIBRARY Cocoa) - add_subdirectory(MacOSX) -endif() +set(generated_mach_interfaces + ${CMAKE_CURRENT_BINARY_DIR}/mach_exc.h + ${CMAKE_CURRENT_BINARY_DIR}/mach_excServer.c + ${CMAKE_CURRENT_BINARY_DIR}/mach_excUser.c + ) +add_custom_command(OUTPUT ${generated_mach_interfaces} + COMMAND mig ${CMAKE_CURRENT_SOURCE_DIR}/MacOSX/dbgnub-mig.defs + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/MacOSX/dbgnub-mig.defs + ) -add_lldb_library(lldbDebugserverCommon - debugserver.cpp +set(DEBUGSERVER_VERS_GENERATED_FILE ${CMAKE_CURRENT_BINARY_DIR}/debugserver_vers.c) +set_source_files_properties(${DEBUGSERVER_VERS_GENERATED_FILE} PROPERTIES GENERATED 1) + +add_custom_command(OUTPUT ${DEBUGSERVER_VERS_GENERATED_FILE} + COMMAND ${LLDB_SOURCE_DIR}/scripts/generate-vers.pl + ${LLDB_SOURCE_DIR}/lldb.xcodeproj/project.pbxproj debugserver + > ${DEBUGSERVER_VERS_GENERATED_FILE} + DEPENDS ${LLDB_SOURCE_DIR}/scripts/generate-vers.pl + ${LLDB_SOURCE_DIR}/lldb.xcodeproj/project.pbxproj + ) + +set(lldbDebugserverCommonSources DNBArch.cpp DNBBreakpoint.cpp DNB.cpp @@ -63,12 +73,54 @@ RNBSocket.cpp SysSignal.cpp TTYState.cpp + + MacOSX/CFBundle.cpp + MacOSX/CFString.cpp + MacOSX/Genealogy.cpp + MacOSX/MachException.cpp + MacOSX/MachProcess.mm + MacOSX/MachTask.mm + MacOSX/MachThread.cpp + MacOSX/MachThreadList.cpp + MacOSX/MachVMMemory.cpp + MacOSX/MachVMRegion.cpp + MacOSX/OsLogger.cpp + ${generated_mach_interfaces} + ${DEBUGSERVER_VERS_GENERATED_FILE}) + +add_library(lldbDebugserverCommon ${lldbDebugserverCommonSources}) + +target_link_libraries(lldbDebugserverCommon + INTERFACE ${COCOA_LIBRARY} + lldbDebugserverMacOSX_I386 + lldbDebugserverMacOSX_X86_64 + lldbDebugserverMacOSX_DarwinLog) + +set(LLVM_OPTIONAL_SOURCES ${lldbDebugserverCommonSources}) +add_lldb_tool(debugserver INCLUDE_IN_FRAMEWORK + debugserver.cpp LINK_LIBS - lldbHost - ${COCOA_LIBRARY} + lldbDebugserverCommon ) +set(LLDB_CODESIGN_IDENTITY "lldb_codesign" + CACHE STRING "Identity used for code signing. Set to empty string to skip the signing step.") +if (NOT ("${LLDB_CODESIGN_IDENTITY}" STREQUAL "")) + execute_process( + COMMAND xcrun -f codesign_allocate + OUTPUT_STRIP_TRAILING_WHITESPACE + OUTPUT_VARIABLE CODESIGN_ALLOCATE + ) + add_custom_command(TARGET debugserver + POST_BUILD + # Note: --entitlements option removed (see comment above). + COMMAND ${CMAKE_COMMAND} -E env CODESIGN_ALLOCATE=${CODESIGN_ALLOCATE} + codesign --force --sign ${LLDB_CODESIGN_IDENTITY} + $<TARGET_FILE:debugserver> + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin + ) +endif()
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits