Skip to content

[Backtracing][Linux] Add Linux support. #66248

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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b071274
[Backtracing][Linux] Work in progress.
al45tair Apr 25, 2023
30037e1
[Backtracing] Fix some bugs.
al45tair Apr 25, 2023
6856d55
[Backtracing] Fix thread support.
al45tair Apr 25, 2023
7c8aecf
[Backtracing] Sort the threads and find the correct crashing thread.
al45tair Apr 25, 2023
2032d42
[Backtracing] Add tentative support for the other archs on Linux.
al45tair Apr 26, 2023
f47a6e3
[Backtracing][Linux] Restrict to 64-bit for now.
al45tair May 2, 2023
f0d4525
[Backtracing] Working on decompression for ELF debug data.
al45tair May 12, 2023
2f61674
[Backtracing][WIP] Debug information lookup support.
al45tair May 16, 2023
261e7c0
[Backtracing] Test debug section lookup.
al45tair May 18, 2023
fe4ae16
[Backtracing] Use the runtime's error output function for warnings.
al45tair May 18, 2023
934dc5c
[Backtracing] Add symbol lookup.
al45tair May 19, 2023
5092dd4
[Backtracing] Ignore non-function symbols.
al45tair May 19, 2023
89e6e24
[Backtracing] Add demangling support.
al45tair May 22, 2023
85238f4
[Backtracing] Switch to SIGPROF as libdispatch interferes with SIGUSR1
al45tair May 22, 2023
d5bb43a
[Backtracing] Async frame support on Linux.
al45tair May 22, 2023
8660688
[Backtracing] Add inline frame support.
al45tair May 30, 2023
753593c
[Backtracing] Now with line numbers.
al45tair May 31, 2023
a3fffac
[Backtracing] Made the tests work.
al45tair May 31, 2023
119aa12
[Backtracing] Tidy up a comment
al45tair Jun 1, 2023
7025164
[Backtracing] macOS fixes.
al45tair Jun 1, 2023
19b6705
[Backtracing] Fixes for issues found on ARM64 linux.
al45tair Jun 2, 2023
7fcd48a
[Backtracing] Fix a symbol lookup bug.
al45tair Jun 2, 2023
60dbd6f
[Backtracing] major:minor can have more than two digits each.
al45tair Jun 5, 2023
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
3 changes: 3 additions & 0 deletions include/swift/AST/SemanticAttrs.def
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,8 @@ SEMANTICS_ATTR(NO_PERFORMANCE_ANALYSIS, "no_performance_analysis")
// that may cause the user to think there is a bug in the compiler.
SEMANTICS_ATTR(NO_MOVEONLY_DIAGNOSTICS, "sil.optimizer.moveonly.diagnostic.ignore")

// Force the use of the frame pointer for the specified function
SEMANTICS_ATTR(USE_FRAME_POINTER, "use_frame_pointer")

#undef SEMANTICS_ATTR

22 changes: 20 additions & 2 deletions include/swift/Runtime/Backtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
#ifndef SWIFT_RUNTIME_BACKTRACE_H
#define SWIFT_RUNTIME_BACKTRACE_H

#ifdef __linux__
#include <sys/types.h>
#include <sys/wait.h>

#include <signal.h>
#endif // defined(__linux__)

#include "swift/Runtime/Config.h"
#include "swift/Runtime/CrashInfo.h"

#include "swift/shims/Visibility.h"
#include "swift/shims/CrashInfo.h"

#include <inttypes.h>

Expand Down Expand Up @@ -50,7 +57,11 @@ typedef int ErrorCode;

SWIFT_RUNTIME_STDLIB_INTERNAL ErrorCode _swift_installCrashHandler();

#ifdef __linux__
SWIFT_RUNTIME_STDLIB_INTERNAL bool _swift_spawnBacktracer(const ArgChar * const *argv, int memserver_fd);
#else
SWIFT_RUNTIME_STDLIB_INTERNAL bool _swift_spawnBacktracer(const ArgChar * const *argv);
#endif

enum class UnwindAlgorithm {
Auto = 0,
Expand Down Expand Up @@ -123,8 +134,15 @@ struct BacktraceSettings {

SWIFT_RUNTIME_STDLIB_INTERNAL BacktraceSettings _swift_backtraceSettings;

SWIFT_RUNTIME_STDLIB_SPI SWIFT_CC(swift) bool _swift_isThunkFunction(const char *mangledName);
SWIFT_RUNTIME_STDLIB_SPI
bool _swift_backtrace_isThunkFunction(const char *mangledName);

SWIFT_RUNTIME_STDLIB_SPI
char *_swift_backtrace_demangle(const char *mangledName,
size_t mangledNameLength,
char *outputBuffer,
size_t *outputBufferSize,
int *status);
#ifdef __cplusplus
} // namespace backtrace
} // namespace runtime
Expand Down
4 changes: 2 additions & 2 deletions include/swift/Runtime/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ swift_auth_code(T value, unsigned extra) {
#elif defined(_WIN32)
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 0
# define SWIFT_BACKTRACE_SECTION ".sw5bckt"
#elif defined(__linux__)
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 0
#elif defined(__linux__) && (defined(__aarch64__) || defined(__x86_64__))
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 1
# define SWIFT_BACKTRACE_SECTION "swift5_backtrace"
#else
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,52 @@
#ifndef SWIFT_CRASHINFO_H
#define SWIFT_CRASHINFO_H

#include "SwiftStdint.h"
#include <inttypes.h>

#ifdef __cplusplus
namespace swift {
namespace runtime {
namespace backtrace {
extern "C" {
#endif

struct CrashInfo {
__swift_uint64_t crashing_thread;
__swift_uint64_t signal;
__swift_uint64_t fault_address;
__swift_uint64_t mctx;
uint64_t crashing_thread;
uint64_t signal;
uint64_t fault_address;

#ifdef __APPLE__
uint64_t mctx;
#elif defined(__linux__)
uint64_t thread_list;
#endif
};

#ifdef __linux__

struct memserver_req {
uint64_t addr;
uint64_t len;
};

struct memserver_resp {
uint64_t addr;
int64_t len;
/* Then len bytes of data */
};

struct thread {
uint64_t next;
int64_t tid;
uint64_t uctx;
};

#endif

#ifdef __cplusplus
} // extern "C"
} // namespace backtrace
} // namespace runtime
} // namespace swift
#endif

Expand Down
3 changes: 3 additions & 0 deletions include/swift/Runtime/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ swift_dynamicCastFailure(const void *sourceType, const char *sourceName,
SWIFT_RUNTIME_EXPORT
void swift_reportError(uint32_t flags, const char *message);

SWIFT_RUNTIME_EXPORT
void swift_reportWarning(uint32_t flags, const char *message);

// Halt due to an overflow in swift_retain().
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
void swift_abortRetainOverflow();
Expand Down
6 changes: 6 additions & 0 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/Types.h"
#include "swift/Basic/ExternalUnion.h"
Expand Down Expand Up @@ -1835,6 +1836,11 @@ IRGenSILFunction::IRGenSILFunction(IRGenModule &IGM, SILFunction *f)
}
}

// If we have @_semantics("use_frame_pointer"), force the use of a
// frame pointer for this function.
if (f->hasSemanticsAttr(semantics::USE_FRAME_POINTER))
CurFn->addFnAttr("frame-pointer", "all");

// Disable inlining of coroutine functions until we split.
if (f->getLoweredFunctionType()->isCoroutine()) {
CurFn->addFnAttr(llvm::Attribute::NoInline);
Expand Down
50 changes: 50 additions & 0 deletions stdlib/public/Backtracing/ArrayImageSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//===--- ArrayImageSource.swift - An image source backed by an Array -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Defines ArrayImageSource, an image source that is backed by a Swift Array.
//
//===----------------------------------------------------------------------===//

import Swift

@_implementationOnly import OS.Libc

enum ArrayImageSourceError: Error {
case outOfBoundsRead(UInt64, UInt64)
}

struct ArrayImageSource<T>: ImageSource {
private var array: Array<T>

public init(array: Array<T>) {
self.array = array
}

public var isMappedImage: Bool { return false }
public var path: String? { return nil }
public var bounds: Bounds? {
return Bounds(base: 0, size: Size(array.count * MemoryLayout<T>.stride))
}

public func fetch<U>(from addr: Address,
into buffer: UnsafeMutableBufferPointer<U>) throws {
try array.withUnsafeBytes{
let size = Size($0.count)
let requested = Size(buffer.count * MemoryLayout<U>.stride)
if addr > size || requested > size - addr {
throw ArrayImageSourceError.outOfBoundsRead(addr, requested)
}

memcpy(buffer.baseAddress!, $0.baseAddress! + Int(addr), Int(requested))
}
}
}
Loading