Skip to content

[Linux] Enable frame pointers when building Swift libraries. #68988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ function(_add_target_variant_c_compile_link_flags)
"-fcoverage-mapping")
endif()

# Use frame pointers on Linux
if("${CFLAGS_SDK}" STREQUAL "LINUX")
list(APPEND result "-fno-omit-frame-pointer")
endif()

_compute_lto_flag("${CFLAGS_ENABLE_LTO}" _lto_flag_out)
if (_lto_flag_out)
list(APPEND result "${_lto_flag_out}")
Expand Down Expand Up @@ -310,6 +315,11 @@ function(_add_target_variant_c_compile_flags)
"-fcoverage-mapping")
endif()

# Use frame pointers on Linux
if("${CFLAGS_SDK}" STREQUAL "LINUX")
list(APPEND result "-fno-omit-frame-pointer")
endif()

if((CFLAGS_ARCH STREQUAL "armv7" OR CFLAGS_ARCH STREQUAL "aarch64") AND
(CFLAGS_SDK STREQUAL "LINUX" OR CFLAGS_SDK STREQUAL "ANDROID"))
list(APPEND result -funwind-tables)
Expand Down Expand Up @@ -924,6 +934,11 @@ function(add_swift_target_library_single target name)
set(install_in_component "${SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT}")
endif()

# Use frame pointers on Linux
if ("${SWIFTLIB_SINGLE_SDK}" STREQUAL "LINUX")
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS "-Xcc" "-fno-omit-frame-pointer")
endif()

# FIXME: don't actually depend on the libraries in SWIFTLIB_SINGLE_LINK_LIBRARIES,
# just any swiftmodule files that are associated with them.
handle_swift_sources(
Expand Down Expand Up @@ -2602,6 +2617,10 @@ function(_add_swift_target_executable_single name)
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}")
endif()

if ("${SWIFTEXE_SINGLE_SDK}" STREQUAL "LINUX")
list(APPEND SWIFTEXE_SINGLE_COMPILE_FLAGS "-Xcc" "-fno-omit-frame-pointer")
endif()

handle_swift_sources(
dependency_target
unused_module_dependency_target
Expand Down
53 changes: 53 additions & 0 deletions test/Backtracing/FatalError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -parse-as-library -Onone -g -o %t/FatalError
// RUN: %target-codesign %t/FatalError
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/FatalError 2>&1 || true) | %FileCheck %s

// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
// UNSUPPORTED: asan
// REQUIRES: executable_test
// REQUIRES: backtracing
// REQUIRES: OS=macosx || OS=linux-gnu

func level1() {
level2()
}

func level2() {
level3()
}

func level3() {
level4()
}

func level4() {
level5()
}

func level5() {
fatalError("Going to crash")
}

@main
struct FatalError {
static func main() {
level1()
}
}

// CHECK: *** Program crashed: Illegal instruction at 0x{{[0-9a-f]+}} ***

// CHECK: Thread 0 {{(".*" )?}}crashed:

// CHECK: 0 0x{{[0-9a-f]+}} _assertionFailure(_:_:file:line:flags:) + {{[0-9]+}} in libswiftCore.{{so|dylib}}
// CHECK-NEXT: 1 [ra] 0x{{[0-9a-f]+}} level5() + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift:30:3
// CHECK-NEXT: 2 [ra] 0x{{[0-9a-f]+}} level4() + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift:26:3
// CHECK-NEXT: 3 [ra] 0x{{[0-9a-f]+}} level3() + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift:22:3
// CHECK-NEXT: 4 [ra] 0x{{[0-9a-f]+}} level2() + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift:18:3
// CHECK-NEXT: 5 [ra] 0x{{[0-9a-f]+}} level1() + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift:14:3
// CHECK-NEXT: 6 [ra] 0x{{[0-9a-f]+}} static FatalError.main() + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift:36:5
// CHECK-NEXT: 7 [ra] [system] 0x{{[0-9a-f]+}} static FatalError.$main() + {{[0-9]+}} in FatalError at {{.*}}/<compiler-generated>
// CHECK-NEXT: 8 [ra] 0x{{[0-9a-f]+}} main + {{[0-9]+}} in FatalError at {{.*}}/FatalError.swift

1 change: 0 additions & 1 deletion test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,6 @@ else:

config.substitutions.append( ('%llvm_obj_root', config.llvm_obj_root) )
config.substitutions.append( ('%swift-lib-dir', config.swift_lib_dir) )
config.substitutions.append( ('%swift-host-lib-dir', config.swift_host_lib_dir))
config.substitutions.append( ('%swift-libexec-dir', config.swift_lib_dir) )
config.substitutions.append( ('%swift-plugin-dir', config.swift_plugin_dir) )
config.substitutions.append( ('%llvm_src_root', config.llvm_src_root) )
Expand Down