Compare commits
58 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f25620d5d0 | |||
| 3bd448574b | |||
| c085164685 | |||
| baf9597d12 | |||
| 7ba397e50d | |||
| ad485ae151 | |||
| 662e436989 | |||
| 923624a26e | |||
| e2621e2fa1 | |||
| ea4824d488 | |||
| f1db8bf93d | |||
| 99a6c79fc5 | |||
| d3e04a9d4b | |||
| b3b195e0e6 | |||
| e434780428 | |||
| ceaaf36f0f | |||
| bb3bd2357e | |||
| 5f7686a18b | |||
| d9655c7867 | |||
| 84bcaad52f | |||
| 7152ac0033 | |||
| 54a65d6391 | |||
| 1ca1dbd9ff | |||
| 182caea1a4 | |||
| 5b47490c74 | |||
| b097912943 | |||
| a4f7b4204e | |||
| 9a2ef536ee | |||
| c859218177 | |||
| 1d5fbd8c42 | |||
| 93b3d9a764 | |||
| 4353fd38d5 | |||
| 97a034c8a4 | |||
| 566d2454b2 | |||
| c8d8ddd88b | |||
| 4915cdd24e | |||
| 7a48ec7baa | |||
| c6559c866c | |||
| 232bd5bdbc | |||
| f2fd8c4845 | |||
| 7faa1c401b | |||
| 1afd60a0bb | |||
| 5074f997bf | |||
| f5462810b8 | |||
| e735696872 | |||
| 383950b36a | |||
| feaee74aa6 | |||
| eaf2cef011 | |||
| f244ad1074 | |||
| 39bf306f55 | |||
| a28e8f99bb | |||
| 4f9b59d324 | |||
| 932f170ce0 | |||
| ce7ff11ed7 | |||
| fabd575ba5 | |||
| df959cd60d | |||
| c5b4501408 | |||
| 4e9985ae9e |
@@ -2,3 +2,5 @@
|
||||
/.build
|
||||
/Packages
|
||||
/*.xcodeproj
|
||||
.swiftpm
|
||||
.*.sw[nop]
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
Tomer Doron <tomer@apple.com> <tomer@apple.com>
|
||||
Tomer Doron <tomer@apple.com> <tomerd@apple.com>
|
||||
Tomer Doron <tomer@apple.com> <tomer.doron@gmail.com>
|
||||
@@ -0,0 +1,13 @@
|
||||
# file options
|
||||
|
||||
--swiftversion 5.0
|
||||
--exclude .build
|
||||
|
||||
# format options
|
||||
|
||||
--self insert
|
||||
--patternlet inline
|
||||
--stripunusedargs unnamed-only
|
||||
--ifdef no-indent
|
||||
|
||||
# rules
|
||||
@@ -0,0 +1,21 @@
|
||||
For the purpose of tracking copyright, this is the list of individuals and
|
||||
organizations who have contributed source code to SwiftLinuxBacktrace.
|
||||
|
||||
For employees of an organization/company where the copyright of work done
|
||||
by employees of that company is held by the company itself, only the company
|
||||
needs to be listed here.
|
||||
|
||||
## COPYRIGHT HOLDERS
|
||||
|
||||
- Apple Inc. (all contributors with '@apple.com')
|
||||
|
||||
### Contributors
|
||||
|
||||
- Ian Partridge <i.partridge@uk.ibm.com>
|
||||
- Tanner <me@tanner.xyz>
|
||||
- Tomer Doron <tomer@apple.com>
|
||||
- Zsolt Váradi <vzsg@users.noreply.github.com>
|
||||
|
||||
**Updating this list**
|
||||
|
||||
Please do not edit this file manually. It is generated using `./scripts/generate_contributors_list.sh`. If a name is misspelled or appearing multiple times: add an entry in `./.mailmap`
|
||||
@@ -0,0 +1,6 @@
|
||||
This product contains a derivation of "libbacktrace" from Ian Lance Taylor.
|
||||
|
||||
* LICENSE (BSD-3-Clause):
|
||||
* https://github.com/ianlancetaylor/libbacktrace/blob/master/LICENSE
|
||||
* HOMEPAGE:
|
||||
* https://github.com/ianlancetaylor/libbacktrace
|
||||
+10
-8
@@ -7,16 +7,18 @@ let package = Package(
|
||||
products: [
|
||||
.library(
|
||||
name: "Backtrace",
|
||||
targets: ["Backtrace"]),
|
||||
targets: ["Backtrace"]
|
||||
),
|
||||
],
|
||||
dependencies: [],
|
||||
targets: [
|
||||
.systemLibrary(name: "CBacktrace"),
|
||||
.target(
|
||||
name: "Backtrace",
|
||||
dependencies: [.target(name: "CBacktrace")]),
|
||||
.testTarget(
|
||||
name: "BacktraceTests",
|
||||
dependencies: ["Backtrace"])
|
||||
.target(name: "Backtrace",
|
||||
dependencies: ["CBacktrace"]),
|
||||
.target(name: "CBacktrace",
|
||||
dependencies: []),
|
||||
.target(name: "Sample",
|
||||
dependencies: ["Backtrace"]),
|
||||
.testTarget(name: "BacktraceTests",
|
||||
dependencies: ["Backtrace"]),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
// swift-tools-version:4.2
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "swift-backtrace",
|
||||
products: [
|
||||
.library(
|
||||
name: "Backtrace",
|
||||
targets: ["Backtrace"]),
|
||||
],
|
||||
dependencies: [],
|
||||
targets: [
|
||||
.systemLibrary(name: "CBacktrace"),
|
||||
.target(
|
||||
name: "Backtrace",
|
||||
dependencies: [.target(name: "CBacktrace")]),
|
||||
.testTarget(
|
||||
name: "BacktraceTests",
|
||||
dependencies: ["Backtrace"])
|
||||
]
|
||||
)
|
||||
@@ -2,11 +2,19 @@
|
||||
|
||||
This Swift package provides support for automatically printing crash backtraces of Swift programs.
|
||||
|
||||
The library is designed to fill a gap in backtraces support for Swift on non-Darwin platforms.
|
||||
When this gap is closed at the language runtime level, this library will become redundant and be deprecated.
|
||||
|
||||
## Usage
|
||||
|
||||
Add `https://github.com/ianpartridge/swift-backtrace.git` as a dependency in your `Package.swift`.
|
||||
When building web-services and daemons, direct usage of this library is discouraged.
|
||||
Instead, use [swift-service-lifecycle](https://github.com/swift-server/swift-service-lifecycle) which helps manage the application lifecycle including setting up backtraces hooks when needed.
|
||||
|
||||
Then, in your `main.swift`, do:
|
||||
Add `https://github.com/swift-server/swift-backtrace.git` as a dependency in your `Package.swift`.
|
||||
|
||||
### Crash backtraces
|
||||
|
||||
In your `main.swift`, do:
|
||||
|
||||
```swift
|
||||
import Backtrace
|
||||
@@ -15,12 +23,22 @@ import Backtrace
|
||||
Backtrace.install()
|
||||
```
|
||||
|
||||
Finally, make sure you build your application with debug symbols enabled:
|
||||
Finally, for Swift < 5.2, make sure you build your application with debug symbols enabled. Debug symbols are automatically included for Swift 5.2 and above.
|
||||
|
||||
```
|
||||
$ swift build -c release -Xswiftc -g
|
||||
```
|
||||
|
||||
When your app crashes, a stacktrace will be printed to `stderr`.
|
||||
|
||||
## Security
|
||||
|
||||
Please see [SECURITY.md](SECURITY.md) for details on the security process.
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
@weissi, for the code!
|
||||
Ian Partridge ([GitHub](https://github.com/ianpartridge/), [Twitter](https://twitter.com/alfa)) the original author of this package.
|
||||
|
||||
Johannes Weiss ([GitHub](https://github.com/weissi), [Twitter](https://twitter.com/johannesweiss)) for the signal handling code.
|
||||
|
||||
Saleem Abdulrasool ([GitHub](https://github.com/compnerd), [Twitter](https://twitter.com/compnerd)) for the Windows port.
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
# Security
|
||||
|
||||
This document specifies the security process for the Backtrace project.
|
||||
|
||||
## Disclosures
|
||||
|
||||
### Private Disclosure Process
|
||||
|
||||
The Backtrace maintainers ask that known and suspected vulnerabilities be
|
||||
privately and responsibly disclosed by emailing
|
||||
[sswg-security-reports@forums.swift.org](mailto:sswg-security-reports@forums.swift.org)
|
||||
with the all the required detail.
|
||||
**Do not file a public issue.**
|
||||
|
||||
#### When to report a vulnerability
|
||||
|
||||
* You think you have discovered a potential security vulnerability in Backtrace.
|
||||
* You are unsure how a vulnerability affects Backtrace.
|
||||
|
||||
#### What happens next?
|
||||
|
||||
* A member of the team will acknowledge receipt of the report within 3
|
||||
working days (United States). This may include a request for additional
|
||||
information about reproducing the vulnerability.
|
||||
* We will privately inform the Swift Server Work Group ([SSWG][sswg]) of the
|
||||
vulnerability within 10 days of the report as per their [security
|
||||
guidelines][sswg-security].
|
||||
* Once we have identified a fix we may ask you to validate it. We aim to do this
|
||||
within 30 days. In some cases this may not be possible, for example when the
|
||||
vulnerability exists at the protocol level and the industry must coordinate on
|
||||
the disclosure process.
|
||||
* If a CVE number is required, one will be requested from [MITRE][mitre]
|
||||
providing you with full credit for the discovery.
|
||||
* We will decide on a planned release date and let you know when it is.
|
||||
* Prior to release, we will inform major dependents that a security-related
|
||||
patch is impending.
|
||||
* Once the fix has been released we will publish a security advisory on GitHub
|
||||
and in the Server → Security Updates category on the [Swift forums][swift-forums-sec].
|
||||
|
||||
[sswg]: https://github.com/swift-server/sswg
|
||||
[sswg-security]: https://github.com/swift-server/sswg/blob/main/security/README.md
|
||||
[swift-forums-sec]: https://forums.swift.org/c/server/security-updates/
|
||||
[mitre]: https://cveform.mitre.org/
|
||||
@@ -1,23 +1,93 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLinuxBacktrace open source project
|
||||
//
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if os(Linux)
|
||||
import Glibc
|
||||
import CBacktrace
|
||||
import Glibc
|
||||
|
||||
typealias CBacktraceErrorCallback = @convention(c) (_ data: UnsafeMutableRawPointer?, _ msg: UnsafePointer<CChar>?, _ errnum: CInt) -> Void
|
||||
typealias CBacktraceFullCallback = @convention(c) (_ data: UnsafeMutableRawPointer?, _ pc: UInt, _ filename: UnsafePointer<CChar>?, _ lineno: CInt, _ function: UnsafePointer<CChar>?) -> CInt
|
||||
typealias CBacktraceSimpleCallback = @convention(c) (_ data: UnsafeMutableRawPointer?, _ pc: UInt) -> CInt
|
||||
typealias CBacktraceSyminfoCallback = @convention(c) (_ data: UnsafeMutableRawPointer?, _ pc: UInt, _ filename: UnsafePointer<CChar>?, _ symval: UInt, _ symsize: UInt) -> Void
|
||||
|
||||
private let state = backtrace_create_state(nil, /* BACKTRACE_SUPPORTS_THREADS */ 1, nil, nil)
|
||||
|
||||
private let fullCallback: CBacktraceFullCallback? = {
|
||||
_, pc, filename, lineno, function in
|
||||
|
||||
var str = "0x"
|
||||
str.append(String(pc, radix: 16))
|
||||
if let function = function {
|
||||
str.append(", ")
|
||||
var fn = String(cString: function)
|
||||
if fn.hasPrefix("$s") || fn.hasPrefix("$S") {
|
||||
fn = _stdlib_demangleName(fn)
|
||||
}
|
||||
str.append(fn)
|
||||
}
|
||||
if let filename = filename {
|
||||
str.append(" at ")
|
||||
str.append(String(cString: filename))
|
||||
str.append(":")
|
||||
str.append(String(lineno))
|
||||
}
|
||||
str.append("\n")
|
||||
|
||||
str.withCString { ptr in
|
||||
_ = withVaList([ptr]) { vaList in
|
||||
vfprintf(stderr, "%s", vaList)
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
private let errorCallback: CBacktraceErrorCallback? = {
|
||||
_, msg, errNo in
|
||||
if let msg = msg {
|
||||
_ = withVaList([msg, errNo]) { vaList in
|
||||
vfprintf(stderr, "SwiftBacktrace ERROR: %s (errno: %d)\n", vaList)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func printBacktrace(signal: CInt) {
|
||||
_ = fputs("Received signal \(signal). Backtrace:\n", stderr)
|
||||
backtrace_full(state, /* skip */ 0, fullCallback, errorCallback, nil)
|
||||
fflush(stderr)
|
||||
}
|
||||
|
||||
public enum Backtrace {
|
||||
/// Install the backtrace handler on default signals: `SIGILL`, `SIGSEGV`, `SIGBUS`, `SIGFPE`.
|
||||
public static func install() {
|
||||
setupHandler(signal: SIGILL) { _ in
|
||||
// this is all undefined behaviour, not allowed to malloc or call backtrace here...
|
||||
let maxFrames = 50
|
||||
let stackSymbols: UnsafeMutableBufferPointer<UnsafeMutableRawPointer?> = .allocate(capacity: maxFrames)
|
||||
stackSymbols.initialize(repeating: nil)
|
||||
let howMany = backtrace(stackSymbols.baseAddress!, CInt(maxFrames))
|
||||
let ptr = backtrace_symbols(stackSymbols.baseAddress!, howMany)
|
||||
let realAddresses = Array(UnsafeBufferPointer(start: ptr, count: Int(howMany))).compactMap { $0 }
|
||||
realAddresses.forEach {
|
||||
print(String(cString: $0))
|
||||
Backtrace.install(signals: [SIGILL, SIGSEGV, SIGBUS, SIGFPE])
|
||||
}
|
||||
|
||||
/// Install the backtrace handler when any of `signals` happen.
|
||||
public static func install(signals: [CInt]) {
|
||||
for signal in signals {
|
||||
self.setupHandler(signal: signal) { signal in
|
||||
printBacktrace(signal: signal)
|
||||
raise(signal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, deprecated, message: "This method will be removed in the next major version.")
|
||||
public static func print() {
|
||||
backtrace_full(state, /* skip */ 0, fullCallback, errorCallback, nil)
|
||||
}
|
||||
|
||||
private static func setupHandler(signal: Int32, handler: @escaping @convention(c) (CInt) -> Void) {
|
||||
typealias sigaction_t = sigaction
|
||||
let sa_flags = CInt(SA_NODEFER) | CInt(bitPattern: CUnsignedInt(SA_RESETHAND))
|
||||
@@ -30,9 +100,175 @@ public enum Backtrace {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#elseif os(Windows)
|
||||
#if swift(<5.4)
|
||||
#error("unsupported Swift version")
|
||||
#else
|
||||
@_implementationOnly import CRT
|
||||
@_implementationOnly import WinSDK
|
||||
#endif
|
||||
|
||||
public enum Backtrace {
|
||||
public static func install() {
|
||||
private static var MachineType: DWORD {
|
||||
#if arch(arm)
|
||||
DWORD(IMAGE_FILE_MACHINE_ARMNT)
|
||||
#elseif arch(arm64)
|
||||
DWORD(IMAGE_FILE_MACHINE_ARM64)
|
||||
#elseif arch(i386)
|
||||
DWORD(IMAGE_FILE_MACHINE_I386)
|
||||
#elseif arch(x86_64)
|
||||
DWORD(IMAGE_FILE_MACHINE_AMD64)
|
||||
#else
|
||||
#error("unsupported architecture")
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(*, deprecated, message: "signal selection unavailable on Windows")
|
||||
public static func install(signals: [CInt]) {
|
||||
Backtrace.install()
|
||||
}
|
||||
|
||||
public static func install() {
|
||||
// Install a last-chance vectored exception handler to capture the error
|
||||
// before the termination and report the stack trace. It is unlikely
|
||||
// that this will be recovered at this point by a SEH handler.
|
||||
_ = AddVectoredExceptionHandler(0) { _ in
|
||||
// NOTE: GetCurrentProcess does not increment the reference count on
|
||||
// the process. This handle should _not_ be closed upon completion.
|
||||
let hProcess: HANDLE = GetCurrentProcess()
|
||||
|
||||
var cxr: CONTEXT = CONTEXT()
|
||||
cxr.ContextFlags =
|
||||
DWORD(CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT)
|
||||
RtlCaptureContext(&cxr)
|
||||
|
||||
_ = SymInitializeW(hProcess, nil, true)
|
||||
_ = SymSetOptions(DWORD(SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME))
|
||||
|
||||
var Frame: STACKFRAME64 = STACKFRAME64()
|
||||
#if arch(arm)
|
||||
Frame.AddrPC.Offset = cxr.Pc
|
||||
Frame.AddrFrame.Offset = cxr.R11
|
||||
Frame.AddrStack.Offset = cxr.Sp
|
||||
#elseif arch(arm64)
|
||||
Frame.AddrPC.Offset = cxr.Pc
|
||||
Frame.AddrFrame.Offset = cxr.Fp
|
||||
Frame.AddrStack.Offset = cxr.Sp
|
||||
#elseif arch(i386)
|
||||
Frame.AddrPC.Offset = cxr.Eip
|
||||
Frame.AddrFrame.Offset = cxr.Ebp
|
||||
Frame.AddrStack.Offset = cxr.Esp
|
||||
#elseif arch(x86_64)
|
||||
Frame.AddrPC.Offset = cxr.Rip
|
||||
Frame.AddrFrame.Offset = cxr.Rbp
|
||||
Frame.AddrStack.Offset = cxr.Rsp
|
||||
#else
|
||||
#error("unsupported architecture")
|
||||
#endif
|
||||
Frame.AddrPC.Mode = AddrModeFlat
|
||||
Frame.AddrFrame.Mode = AddrModeFlat
|
||||
Frame.AddrStack.Mode = AddrModeFlat
|
||||
|
||||
// Constant indicating the maximum symbol length that we expect
|
||||
// during symbolication of the stack trace.
|
||||
let kMaxSymbolLength: Int = 255
|
||||
|
||||
// Heap allocate the buffer as we need to account for the trailing
|
||||
// storage that we need to provide.
|
||||
let pSymbolBuffer: UnsafeMutableRawPointer =
|
||||
UnsafeMutableRawPointer.allocate(byteCount: MemoryLayout<IMAGEHLP_SYMBOL64>.size + kMaxSymbolLength,
|
||||
alignment: 1)
|
||||
defer { pSymbolBuffer.deallocate() }
|
||||
|
||||
let pSymbol: UnsafeMutablePointer<IMAGEHLP_SYMBOL64> =
|
||||
pSymbolBuffer.bindMemory(to: IMAGEHLP_SYMBOL64.self,
|
||||
capacity: 1)
|
||||
|
||||
let hThread: HANDLE = GetCurrentThread()
|
||||
while StackWalk64(Backtrace.MachineType, hProcess, hThread,
|
||||
&Frame, &cxr, nil, SymFunctionTableAccess64,
|
||||
SymGetModuleBase64, nil) {
|
||||
var qwModuleBase: DWORD64 =
|
||||
SymGetModuleBase64(hProcess, Frame.AddrPC.Offset)
|
||||
|
||||
let module: String = withUnsafeMutablePointer(to: &qwModuleBase) {
|
||||
$0.withMemoryRebound(to: HINSTANCE.self, capacity: 1) { hInstance in
|
||||
String(decoding: [WCHAR](unsafeUninitializedCapacity: Int(MAX_PATH + 1)) {
|
||||
$1 = Int(GetModuleFileNameW(hInstance.pointee,
|
||||
$0.baseAddress,
|
||||
DWORD($0.count)))
|
||||
}, as: UTF16.self)
|
||||
}
|
||||
}
|
||||
|
||||
pSymbol.pointee.SizeOfStruct =
|
||||
DWORD(MemoryLayout<IMAGEHLP_SYMBOL64>.size)
|
||||
pSymbol.pointee.MaxNameLength = DWORD(kMaxSymbolLength)
|
||||
_ = SymGetSymFromAddr64(hProcess, Frame.AddrPC.Offset, nil,
|
||||
pSymbol)
|
||||
|
||||
var symbol: String =
|
||||
withUnsafePointer(to: &pSymbol.pointee.Name) {
|
||||
String(cString: $0)
|
||||
}
|
||||
|
||||
// Undecorate Swift 3+ names only. Earlier Swift decorations
|
||||
// are unsupported. Any MSVC name decoration has been
|
||||
// unperformed during the DbgHelp operation through the use of
|
||||
// the `SYMOPT_UNDNAME` option.
|
||||
if symbol.hasPrefix("$s") || symbol.hasPrefix("$S") {
|
||||
symbol = _stdlib_demangleName(symbol)
|
||||
}
|
||||
|
||||
var Displacement: DWORD = 0
|
||||
var Line: IMAGEHLP_LINE64 = IMAGEHLP_LINE64()
|
||||
Line.SizeOfStruct = DWORD(MemoryLayout<IMAGEHLP_LINE64>.size)
|
||||
_ = SymGetLineFromAddr64(hProcess, Frame.AddrPC.Offset,
|
||||
&Displacement, &Line)
|
||||
|
||||
var details: String = ""
|
||||
|
||||
if !symbol.isEmpty {
|
||||
// Truncate the module path to the filename. The
|
||||
// `PathFindFileNameW` call will return the beginning of the
|
||||
// string if a path separator character is not found.
|
||||
if let pszModule = module.withCString(encodedAs: UTF16.self,
|
||||
PathFindFileNameW) {
|
||||
details.append(", \(String(decodingCString: pszModule, as: UTF16.self))!\(symbol)")
|
||||
}
|
||||
}
|
||||
|
||||
if let szFileName = Line.FileName {
|
||||
details.append(" at \(String(cString: szFileName)):\(Line.LineNumber)")
|
||||
}
|
||||
|
||||
_ = details.withCString { pszDetails in
|
||||
withVaList([Frame.AddrPC.Offset, pszDetails]) {
|
||||
#if arch(arm64) || arch(x86_64)
|
||||
vfprintf(stderr, "%#016x%s\n", $0)
|
||||
#else
|
||||
vfprintf(stderr, "%#08x%s\n", $0)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_ = SymCleanup(hProcess)
|
||||
|
||||
// We have not handled the exception, continue the search.
|
||||
return EXCEPTION_CONTINUE_SEARCH
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
public enum Backtrace {
|
||||
public static func install() {}
|
||||
|
||||
public static func install(signals: [CInt]) {}
|
||||
|
||||
@available(*, deprecated, message: "This method will be removed in the next major version.")
|
||||
public static func print() {}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLinuxBacktrace open source project
|
||||
//
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if os(Linux)
|
||||
import Glibc
|
||||
#elseif os(Windows)
|
||||
#if swift(<5.4)
|
||||
#error("unsupported Swift version")
|
||||
#else
|
||||
@_implementationOnly
|
||||
import ucrt
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if os(Linux) || os(Windows)
|
||||
@_silgen_name("swift_demangle")
|
||||
public
|
||||
func _stdlib_demangleImpl(
|
||||
mangledName: UnsafePointer<CChar>?,
|
||||
mangledNameLength: UInt,
|
||||
outputBuffer: UnsafeMutablePointer<CChar>?,
|
||||
outputBufferSize: UnsafeMutablePointer<UInt>?,
|
||||
flags: UInt32
|
||||
) -> UnsafeMutablePointer<CChar>?
|
||||
|
||||
internal func _stdlib_demangleName(_ mangledName: String) -> String {
|
||||
return mangledName.utf8CString.withUnsafeBufferPointer {
|
||||
mangledNameUTF8CStr in
|
||||
|
||||
let demangledNamePtr = _stdlib_demangleImpl(
|
||||
mangledName: mangledNameUTF8CStr.baseAddress,
|
||||
mangledNameLength: UInt(mangledNameUTF8CStr.count - 1),
|
||||
outputBuffer: nil,
|
||||
outputBufferSize: nil,
|
||||
flags: 0
|
||||
)
|
||||
|
||||
if let demangledNamePtr = demangledNamePtr {
|
||||
let demangledName = String(cString: demangledNamePtr)
|
||||
free(demangledNamePtr)
|
||||
return demangledName
|
||||
}
|
||||
return mangledName
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
# Copyright (C) 2012-2016 Free Software Foundation, Inc.
|
||||
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
|
||||
# (1) Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
|
||||
# (2) Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
|
||||
# (3) The name of the author may not be used to
|
||||
# endorse or promote products derived from this software without
|
||||
# specific prior written permission.
|
||||
|
||||
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,115 @@
|
||||
#ifdef __linux__
|
||||
/* atomic.c -- Support for atomic functions if not present.
|
||||
Copyright (C) 2013-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "include/backtrace.h"
|
||||
#include "backtrace-supported.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* This file holds implementations of the atomic functions that are
|
||||
used if the host compiler has the sync functions but not the atomic
|
||||
functions, as is true of versions of GCC before 4.7. */
|
||||
|
||||
#if !defined (HAVE_ATOMIC_FUNCTIONS) && defined (HAVE_SYNC_FUNCTIONS)
|
||||
|
||||
/* Do an atomic load of a pointer. */
|
||||
|
||||
void *
|
||||
backtrace_atomic_load_pointer (void *arg)
|
||||
{
|
||||
void **pp;
|
||||
void *p;
|
||||
|
||||
pp = (void **) arg;
|
||||
p = *pp;
|
||||
while (!__sync_bool_compare_and_swap (pp, p, p))
|
||||
p = *pp;
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Do an atomic load of an int. */
|
||||
|
||||
int
|
||||
backtrace_atomic_load_int (int *p)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = *p;
|
||||
while (!__sync_bool_compare_and_swap (p, i, i))
|
||||
i = *p;
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Do an atomic store of a pointer. */
|
||||
|
||||
void
|
||||
backtrace_atomic_store_pointer (void *arg, void *p)
|
||||
{
|
||||
void **pp;
|
||||
void *old;
|
||||
|
||||
pp = (void **) arg;
|
||||
old = *pp;
|
||||
while (!__sync_bool_compare_and_swap (pp, old, p))
|
||||
old = *pp;
|
||||
}
|
||||
|
||||
/* Do an atomic store of a size_t value. */
|
||||
|
||||
void
|
||||
backtrace_atomic_store_size_t (size_t *p, size_t v)
|
||||
{
|
||||
size_t old;
|
||||
|
||||
old = *p;
|
||||
while (!__sync_bool_compare_and_swap (p, old, v))
|
||||
old = *p;
|
||||
}
|
||||
|
||||
/* Do an atomic store of a int value. */
|
||||
|
||||
void
|
||||
backtrace_atomic_store_int (int *p, int v)
|
||||
{
|
||||
size_t old;
|
||||
|
||||
old = *p;
|
||||
while (!__sync_bool_compare_and_swap (p, old, v))
|
||||
old = *p;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,68 @@
|
||||
#ifdef __linux__
|
||||
/* backtrace-supported.h.in -- Whether stack backtrace is supported.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
/* The file backtrace-supported.h.in is used by configure to generate
|
||||
the file backtrace-supported.h. The file backtrace-supported.h may
|
||||
be #include'd to see whether the backtrace library will be able to
|
||||
get a backtrace and produce symbolic information. */
|
||||
|
||||
|
||||
/* BACKTRACE_SUPPORTED will be #define'd as 1 if the backtrace library
|
||||
should work, 0 if it will not. Libraries may #include this to make
|
||||
other arrangements. */
|
||||
|
||||
#define BACKTRACE_SUPPORTED 1
|
||||
|
||||
/* BACKTRACE_USES_MALLOC will be #define'd as 1 if the backtrace
|
||||
library will call malloc as it works, 0 if it will call mmap
|
||||
instead. This may be used to determine whether it is safe to call
|
||||
the backtrace functions from a signal handler. In general this
|
||||
only applies to calls like backtrace and backtrace_pcinfo. It does
|
||||
not apply to backtrace_simple, which never calls malloc. It does
|
||||
not apply to backtrace_print, which always calls fprintf and
|
||||
therefore malloc. */
|
||||
|
||||
#define BACKTRACE_USES_MALLOC 0
|
||||
|
||||
/* BACKTRACE_SUPPORTS_THREADS will be #define'd as 1 if the backtrace
|
||||
library is configured with threading support, 0 if not. If this is
|
||||
0, the threaded parameter to backtrace_create_state must be passed
|
||||
as 0. */
|
||||
|
||||
#define BACKTRACE_SUPPORTS_THREADS 1
|
||||
|
||||
/* BACKTRACE_SUPPORTS_DATA will be #defined'd as 1 if the backtrace_syminfo
|
||||
will work for variables. It will always work for functions. */
|
||||
|
||||
#define BACKTRACE_SUPPORTS_DATA 1
|
||||
#endif
|
||||
@@ -0,0 +1,131 @@
|
||||
#ifdef __linux__
|
||||
/* backtrace.c -- Entry point for stack backtrace library.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "unwind.h"
|
||||
#include "include/backtrace.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* The main backtrace_full routine. */
|
||||
|
||||
/* Data passed through _Unwind_Backtrace. */
|
||||
|
||||
struct backtrace_data
|
||||
{
|
||||
/* Number of frames to skip. */
|
||||
int skip;
|
||||
/* Library state. */
|
||||
struct backtrace_state *state;
|
||||
/* Callback routine. */
|
||||
backtrace_full_callback callback;
|
||||
/* Error callback routine. */
|
||||
backtrace_error_callback error_callback;
|
||||
/* Data to pass to callback routines. */
|
||||
void *data;
|
||||
/* Value to return from backtrace_full. */
|
||||
int ret;
|
||||
/* Whether there is any memory available. */
|
||||
int can_alloc;
|
||||
};
|
||||
|
||||
/* Unwind library callback routine. This is passed to
|
||||
_Unwind_Backtrace. */
|
||||
|
||||
static _Unwind_Reason_Code
|
||||
unwind (struct _Unwind_Context *context, void *vdata)
|
||||
{
|
||||
struct backtrace_data *bdata = (struct backtrace_data *) vdata;
|
||||
uintptr_t pc;
|
||||
int ip_before_insn = 0;
|
||||
|
||||
#ifdef HAVE_GETIPINFO
|
||||
pc = _Unwind_GetIPInfo (context, &ip_before_insn);
|
||||
#else
|
||||
pc = _Unwind_GetIP (context);
|
||||
#endif
|
||||
|
||||
if (bdata->skip > 0)
|
||||
{
|
||||
--bdata->skip;
|
||||
return _URC_NO_REASON;
|
||||
}
|
||||
|
||||
if (!ip_before_insn)
|
||||
--pc;
|
||||
|
||||
if (!bdata->can_alloc)
|
||||
bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL);
|
||||
else
|
||||
bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,
|
||||
bdata->error_callback, bdata->data);
|
||||
if (bdata->ret != 0)
|
||||
return _URC_END_OF_STACK;
|
||||
|
||||
return _URC_NO_REASON;
|
||||
}
|
||||
|
||||
/* Get a stack backtrace. */
|
||||
|
||||
int __attribute__((noinline))
|
||||
backtrace_full (struct backtrace_state *state, int skip,
|
||||
backtrace_full_callback callback,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
struct backtrace_data bdata;
|
||||
void *p;
|
||||
|
||||
bdata.skip = skip + 1;
|
||||
bdata.state = state;
|
||||
bdata.callback = callback;
|
||||
bdata.error_callback = error_callback;
|
||||
bdata.data = data;
|
||||
bdata.ret = 0;
|
||||
|
||||
/* If we can't allocate any memory at all, don't try to produce
|
||||
file/line information. */
|
||||
p = backtrace_alloc (state, 4096, NULL, NULL);
|
||||
if (p == NULL)
|
||||
bdata.can_alloc = 0;
|
||||
else
|
||||
{
|
||||
backtrace_free (state, p, 4096, NULL, NULL);
|
||||
bdata.can_alloc = 1;
|
||||
}
|
||||
|
||||
_Unwind_Backtrace (unwind, &bdata);
|
||||
return bdata.ret;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,172 @@
|
||||
#ifdef __linux__
|
||||
/* config.h. Generated from config.h.in by configure. */
|
||||
/* config.h.in. Generated from configure.ac by autoheader. */
|
||||
|
||||
/* ELF size: 32 or 64 */
|
||||
#define BACKTRACE_ELF_SIZE 64
|
||||
|
||||
/* XCOFF size: 32 or 64 */
|
||||
#define BACKTRACE_XCOFF_SIZE unused
|
||||
|
||||
/* Define to 1 if you have the __atomic functions */
|
||||
#define HAVE_ATOMIC_FUNCTIONS 1
|
||||
|
||||
/* Define to 1 if you have the `clock_gettime' function. */
|
||||
#define HAVE_CLOCK_GETTIME 1
|
||||
|
||||
/* Define to 1 if you have the declaration of `getpagesize', and to 0 if you
|
||||
don't. */
|
||||
#define HAVE_DECL_GETPAGESIZE 1
|
||||
|
||||
/* Define to 1 if you have the declaration of `strnlen', and to 0 if you
|
||||
don't. */
|
||||
#define HAVE_DECL_STRNLEN 1
|
||||
|
||||
/* Define to 1 if you have the <dlfcn.h> header file. */
|
||||
#define HAVE_DLFCN_H 1
|
||||
|
||||
/* Define if dl_iterate_phdr is available. */
|
||||
#define HAVE_DL_ITERATE_PHDR 1
|
||||
|
||||
/* Define to 1 if you have the fcntl function */
|
||||
#define HAVE_FCNTL 1
|
||||
|
||||
/* Define if getexecname is available. */
|
||||
/* #undef HAVE_GETEXECNAME */
|
||||
|
||||
/* Define if _Unwind_GetIPInfo is available. */
|
||||
#define HAVE_GETIPINFO 1
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#define HAVE_INTTYPES_H 1
|
||||
|
||||
/* Define to 1 if you have KERN_PROC and KERN_PROC_PATHNAME in <sys/sysctl.h>.
|
||||
*/
|
||||
/* #undef HAVE_KERN_PROC */
|
||||
|
||||
/* Define to 1 if you have KERN_PROCARGS and KERN_PROC_PATHNAME in
|
||||
<sys/sysctl.h>. */
|
||||
/* #undef HAVE_KERN_PROC_ARGS */
|
||||
|
||||
/* Define if -llzma is available. */
|
||||
/* #undef HAVE_LIBLZMA */
|
||||
|
||||
/* Define to 1 if you have the <link.h> header file. */
|
||||
#define HAVE_LINK_H 1
|
||||
|
||||
/* Define if AIX loadquery is available. */
|
||||
/* #undef HAVE_LOADQUERY */
|
||||
|
||||
/* Define to 1 if you have the `lstat' function. */
|
||||
#define HAVE_LSTAT 1
|
||||
|
||||
/* Define to 1 if you have the <mach-o/dyld.h> header file. */
|
||||
/* #undef HAVE_MACH_O_DYLD_H */
|
||||
|
||||
/* Define to 1 if you have the <memory.h> header file. */
|
||||
#define HAVE_MEMORY_H 1
|
||||
|
||||
/* Define to 1 if you have the `readlink' function. */
|
||||
#define HAVE_READLINK 1
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
#define HAVE_STDINT_H 1
|
||||
|
||||
/* Define to 1 if you have the <stdlib.h> header file. */
|
||||
#define HAVE_STDLIB_H 1
|
||||
|
||||
/* Define to 1 if you have the <strings.h> header file. */
|
||||
#define HAVE_STRINGS_H 1
|
||||
|
||||
/* Define to 1 if you have the <string.h> header file. */
|
||||
#define HAVE_STRING_H 1
|
||||
|
||||
/* Define to 1 if you have the __sync functions */
|
||||
#define HAVE_SYNC_FUNCTIONS 1
|
||||
|
||||
/* Define to 1 if you have the <sys/ldr.h> header file. */
|
||||
/* #undef HAVE_SYS_LDR_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/mman.h> header file. */
|
||||
#define HAVE_SYS_MMAN_H 1
|
||||
|
||||
/* Define to 1 if you have the <sys/stat.h> header file. */
|
||||
#define HAVE_SYS_STAT_H 1
|
||||
|
||||
/* Define to 1 if you have the <sys/types.h> header file. */
|
||||
#define HAVE_SYS_TYPES_H 1
|
||||
|
||||
/* Define to 1 if you have the <unistd.h> header file. */
|
||||
#define HAVE_UNISTD_H 1
|
||||
|
||||
/* Define if -lz is available. */
|
||||
#define HAVE_ZLIB 1
|
||||
|
||||
/* Define to the sub-directory in which libtool stores uninstalled libraries.
|
||||
*/
|
||||
#define LT_OBJDIR ".libs/"
|
||||
|
||||
/* Define to the address where bug reports for this package should be sent. */
|
||||
#define PACKAGE_BUGREPORT ""
|
||||
|
||||
/* Define to the full name of this package. */
|
||||
#define PACKAGE_NAME "package-unused"
|
||||
|
||||
/* Define to the full name and version of this package. */
|
||||
#define PACKAGE_STRING "package-unused version-unused"
|
||||
|
||||
/* Define to the one symbol short name of this package. */
|
||||
#define PACKAGE_TARNAME "libbacktrace"
|
||||
|
||||
/* Define to the home page for this package. */
|
||||
#define PACKAGE_URL ""
|
||||
|
||||
/* Define to the version of this package. */
|
||||
#define PACKAGE_VERSION "version-unused"
|
||||
|
||||
/* Define to 1 if you have the ANSI C header files. */
|
||||
#define STDC_HEADERS 1
|
||||
|
||||
/* Enable extensions on AIX 3, Interix. */
|
||||
#ifndef _ALL_SOURCE
|
||||
# define _ALL_SOURCE 1
|
||||
#endif
|
||||
/* Enable GNU extensions on systems that have them. */
|
||||
#ifndef _GNU_SOURCE
|
||||
# define _GNU_SOURCE 1
|
||||
#endif
|
||||
/* Enable threading extensions on Solaris. */
|
||||
#ifndef _POSIX_PTHREAD_SEMANTICS
|
||||
# define _POSIX_PTHREAD_SEMANTICS 1
|
||||
#endif
|
||||
/* Enable extensions on HP NonStop. */
|
||||
#ifndef _TANDEM_SOURCE
|
||||
# define _TANDEM_SOURCE 1
|
||||
#endif
|
||||
/* Enable general extensions on Solaris. */
|
||||
#ifndef __EXTENSIONS__
|
||||
# define __EXTENSIONS__ 1
|
||||
#endif
|
||||
|
||||
|
||||
/* Enable large inode numbers on Mac OS X 10.5. */
|
||||
#ifndef _DARWIN_USE_64_BIT_INODE
|
||||
# define _DARWIN_USE_64_BIT_INODE 1
|
||||
#endif
|
||||
|
||||
/* Number of bits in a file offset, on hosts where this is settable. */
|
||||
/* #undef _FILE_OFFSET_BITS */
|
||||
|
||||
/* Define for large files, on AIX-style hosts. */
|
||||
/* #undef _LARGE_FILES */
|
||||
|
||||
/* Define to 1 if on MINIX. */
|
||||
/* #undef _MINIX */
|
||||
|
||||
/* Define to 2 if the system does not provide POSIX.1 features except with
|
||||
this defined. */
|
||||
/* #undef _POSIX_1_SOURCE */
|
||||
|
||||
/* Define to 1 if you need to in order for `stat' and other things to work. */
|
||||
/* #undef _POSIX_SOURCE */
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,348 @@
|
||||
#ifdef __linux__
|
||||
/* fileline.c -- Get file and line number information in a backtrace.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if defined (HAVE_KERN_PROC_ARGS) || defined (HAVE_KERN_PROC)
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_MACH_O_DYLD_H
|
||||
#include <mach-o/dyld.h>
|
||||
#endif
|
||||
|
||||
#include "include/backtrace.h"
|
||||
#include "internal.h"
|
||||
|
||||
#ifndef HAVE_GETEXECNAME
|
||||
#define getexecname() NULL
|
||||
#endif
|
||||
|
||||
#if !defined (HAVE_KERN_PROC_ARGS) && !defined (HAVE_KERN_PROC)
|
||||
|
||||
#define sysctl_exec_name1(state, error_callback, data) NULL
|
||||
#define sysctl_exec_name2(state, error_callback, data) NULL
|
||||
|
||||
#else /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
|
||||
|
||||
static char *
|
||||
sysctl_exec_name (struct backtrace_state *state,
|
||||
int mib0, int mib1, int mib2, int mib3,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
int mib[4];
|
||||
size_t len;
|
||||
char *name;
|
||||
size_t rlen;
|
||||
|
||||
mib[0] = mib0;
|
||||
mib[1] = mib1;
|
||||
mib[2] = mib2;
|
||||
mib[3] = mib3;
|
||||
|
||||
if (sysctl (mib, 4, NULL, &len, NULL, 0) < 0)
|
||||
return NULL;
|
||||
name = (char *) backtrace_alloc (state, len, error_callback, data);
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
rlen = len;
|
||||
if (sysctl (mib, 4, name, &rlen, NULL, 0) < 0)
|
||||
{
|
||||
backtrace_free (state, name, len, error_callback, data);
|
||||
return NULL;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
#ifdef HAVE_KERN_PROC_ARGS
|
||||
|
||||
static char *
|
||||
sysctl_exec_name1 (struct backtrace_state *state,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
/* This variant is used on NetBSD. */
|
||||
return sysctl_exec_name (state, CTL_KERN, KERN_PROC_ARGS, -1,
|
||||
KERN_PROC_PATHNAME, error_callback, data);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define sysctl_exec_name1(state, error_callback, data) NULL
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_KERN_PROC
|
||||
|
||||
static char *
|
||||
sysctl_exec_name2 (struct backtrace_state *state,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
/* This variant is used on FreeBSD. */
|
||||
return sysctl_exec_name (state, CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1,
|
||||
error_callback, data);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define sysctl_exec_name2(state, error_callback, data) NULL
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
|
||||
|
||||
#ifdef HAVE_MACH_O_DYLD_H
|
||||
|
||||
static char *
|
||||
macho_get_executable_path (struct backtrace_state *state,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
uint32_t len;
|
||||
char *name;
|
||||
|
||||
len = 0;
|
||||
if (_NSGetExecutablePath (NULL, &len) == 0)
|
||||
return NULL;
|
||||
name = (char *) backtrace_alloc (state, len, error_callback, data);
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
if (_NSGetExecutablePath (name, &len) != 0)
|
||||
{
|
||||
backtrace_free (state, name, len, error_callback, data);
|
||||
return NULL;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
#else /* !defined (HAVE_MACH_O_DYLD_H) */
|
||||
|
||||
#define macho_get_executable_path(state, error_callback, data) NULL
|
||||
|
||||
#endif /* !defined (HAVE_MACH_O_DYLD_H) */
|
||||
|
||||
/* Initialize the fileline information from the executable. Returns 1
|
||||
on success, 0 on failure. */
|
||||
|
||||
static int
|
||||
fileline_initialize (struct backtrace_state *state,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
int failed;
|
||||
fileline fileline_fn;
|
||||
int pass;
|
||||
int called_error_callback;
|
||||
int descriptor;
|
||||
const char *filename;
|
||||
char buf[64];
|
||||
|
||||
if (!state->threaded)
|
||||
failed = state->fileline_initialization_failed;
|
||||
else
|
||||
failed = backtrace_atomic_load_int (&state->fileline_initialization_failed);
|
||||
|
||||
if (failed)
|
||||
{
|
||||
error_callback (data, "failed to read executable information", -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!state->threaded)
|
||||
fileline_fn = state->fileline_fn;
|
||||
else
|
||||
fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
|
||||
if (fileline_fn != NULL)
|
||||
return 1;
|
||||
|
||||
/* We have not initialized the information. Do it now. */
|
||||
|
||||
descriptor = -1;
|
||||
called_error_callback = 0;
|
||||
for (pass = 0; pass < 8; ++pass)
|
||||
{
|
||||
int does_not_exist;
|
||||
|
||||
switch (pass)
|
||||
{
|
||||
case 0:
|
||||
filename = state->filename;
|
||||
break;
|
||||
case 1:
|
||||
filename = getexecname ();
|
||||
break;
|
||||
case 2:
|
||||
filename = "/proc/self/exe";
|
||||
break;
|
||||
case 3:
|
||||
filename = "/proc/curproc/file";
|
||||
break;
|
||||
case 4:
|
||||
snprintf (buf, sizeof (buf), "/proc/%ld/object/a.out",
|
||||
(long) getpid ());
|
||||
filename = buf;
|
||||
break;
|
||||
case 5:
|
||||
filename = sysctl_exec_name1 (state, error_callback, data);
|
||||
break;
|
||||
case 6:
|
||||
filename = sysctl_exec_name2 (state, error_callback, data);
|
||||
break;
|
||||
case 7:
|
||||
filename = macho_get_executable_path (state, error_callback, data);
|
||||
break;
|
||||
default:
|
||||
abort ();
|
||||
}
|
||||
|
||||
if (filename == NULL)
|
||||
continue;
|
||||
|
||||
descriptor = backtrace_open (filename, error_callback, data,
|
||||
&does_not_exist);
|
||||
if (descriptor < 0 && !does_not_exist)
|
||||
{
|
||||
called_error_callback = 1;
|
||||
break;
|
||||
}
|
||||
if (descriptor >= 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (descriptor < 0)
|
||||
{
|
||||
if (!called_error_callback)
|
||||
{
|
||||
if (state->filename != NULL)
|
||||
error_callback (data, state->filename, ENOENT);
|
||||
else
|
||||
error_callback (data,
|
||||
"libbacktrace could not find executable to open",
|
||||
0);
|
||||
}
|
||||
failed = 1;
|
||||
}
|
||||
|
||||
if (!failed)
|
||||
{
|
||||
if (!backtrace_initialize (state, filename, descriptor, error_callback,
|
||||
data, &fileline_fn))
|
||||
failed = 1;
|
||||
}
|
||||
|
||||
if (failed)
|
||||
{
|
||||
if (!state->threaded)
|
||||
state->fileline_initialization_failed = 1;
|
||||
else
|
||||
backtrace_atomic_store_int (&state->fileline_initialization_failed, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!state->threaded)
|
||||
state->fileline_fn = fileline_fn;
|
||||
else
|
||||
{
|
||||
backtrace_atomic_store_pointer (&state->fileline_fn, fileline_fn);
|
||||
|
||||
/* Note that if two threads initialize at once, one of the data
|
||||
sets may be leaked. */
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Given a PC, find the file name, line number, and function name. */
|
||||
|
||||
int
|
||||
backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
|
||||
backtrace_full_callback callback,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
if (!fileline_initialize (state, error_callback, data))
|
||||
return 0;
|
||||
|
||||
if (state->fileline_initialization_failed)
|
||||
return 0;
|
||||
|
||||
return state->fileline_fn (state, pc, callback, error_callback, data);
|
||||
}
|
||||
|
||||
/* Given a PC, find the symbol for it, and its value. */
|
||||
|
||||
int
|
||||
backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
|
||||
backtrace_syminfo_callback callback,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
if (!fileline_initialize (state, error_callback, data))
|
||||
return 0;
|
||||
|
||||
if (state->fileline_initialization_failed)
|
||||
return 0;
|
||||
|
||||
state->syminfo_fn (state, pc, callback, error_callback, data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* A backtrace_syminfo_callback that can call into a
|
||||
backtrace_full_callback, used when we have a symbol table but no
|
||||
debug info. */
|
||||
|
||||
void
|
||||
backtrace_syminfo_to_full_callback (void *data, uintptr_t pc,
|
||||
const char *symname,
|
||||
uintptr_t symval ATTRIBUTE_UNUSED,
|
||||
uintptr_t symsize ATTRIBUTE_UNUSED)
|
||||
{
|
||||
struct backtrace_call_full *bdata = (struct backtrace_call_full *) data;
|
||||
|
||||
bdata->ret = bdata->full_callback (bdata->full_data, pc, NULL, 0, symname);
|
||||
}
|
||||
|
||||
/* An error callback that corresponds to
|
||||
backtrace_syminfo_to_full_callback. */
|
||||
|
||||
void
|
||||
backtrace_syminfo_to_full_error_callback (void *data, const char *msg,
|
||||
int errnum)
|
||||
{
|
||||
struct backtrace_call_full *bdata = (struct backtrace_call_full *) data;
|
||||
|
||||
bdata->full_error_callback (bdata->full_data, msg, errnum);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
#ifdef __linux__
|
||||
/* btest.c -- Filename header for libbacktrace library
|
||||
Copyright (C) 2012-2018 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#ifndef GCC_VERSION
|
||||
# define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
|
||||
#endif
|
||||
|
||||
#if (GCC_VERSION < 2007)
|
||||
# define __attribute__(x)
|
||||
#endif
|
||||
|
||||
#ifndef ATTRIBUTE_UNUSED
|
||||
# define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
|
||||
#endif
|
||||
|
||||
#if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__) || defined (__CYGWIN__)
|
||||
# define IS_DIR_SEPARATOR(c) ((c) == '/' || (c) == '\\')
|
||||
# define HAS_DRIVE_SPEC(f) ((f)[0] != '\0' && (f)[1] == ':')
|
||||
# define IS_ABSOLUTE_PATH(f) (IS_DIR_SEPARATOR((f)[0]) || HAS_DRIVE_SPEC(f))
|
||||
#else
|
||||
# define IS_DIR_SEPARATOR(c) ((c) == '/')
|
||||
# define IS_ABSOLUTE_PATH(f) (IS_DIR_SEPARATOR((f)[0]))
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,191 @@
|
||||
#ifdef __linux__
|
||||
/* backtrace.h -- Public header file for stack backtrace library.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#ifndef BACKTRACE_H
|
||||
#define BACKTRACE_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* The backtrace state. This struct is intentionally not defined in
|
||||
the public interface. */
|
||||
|
||||
struct backtrace_state;
|
||||
|
||||
/* The type of the error callback argument to backtrace functions.
|
||||
This function, if not NULL, will be called for certain error cases.
|
||||
The DATA argument is passed to the function that calls this one.
|
||||
The MSG argument is an error message. The ERRNUM argument, if
|
||||
greater than 0, holds an errno value. The MSG buffer may become
|
||||
invalid after this function returns.
|
||||
|
||||
As a special case, the ERRNUM argument will be passed as -1 if no
|
||||
debug info can be found for the executable, or if the debug info
|
||||
exists but has an unsupported version, but the function requires
|
||||
debug info (e.g., backtrace_full, backtrace_pcinfo). The MSG in
|
||||
this case will be something along the lines of "no debug info".
|
||||
Similarly, ERRNUM will be passed as -1 if there is no symbol table,
|
||||
but the function requires a symbol table (e.g., backtrace_syminfo).
|
||||
This may be used as a signal that some other approach should be
|
||||
tried. */
|
||||
|
||||
typedef void (*backtrace_error_callback) (void *data, const char *msg,
|
||||
int errnum);
|
||||
|
||||
/* Create state information for the backtrace routines. This must be
|
||||
called before any of the other routines, and its return value must
|
||||
be passed to all of the other routines. FILENAME is the path name
|
||||
of the executable file; if it is NULL the library will try
|
||||
system-specific path names. If not NULL, FILENAME must point to a
|
||||
permanent buffer. If THREADED is non-zero the state may be
|
||||
accessed by multiple threads simultaneously, and the library will
|
||||
use appropriate atomic operations. If THREADED is zero the state
|
||||
may only be accessed by one thread at a time. This returns a state
|
||||
pointer on success, NULL on error. If an error occurs, this will
|
||||
call the ERROR_CALLBACK routine.
|
||||
|
||||
Calling this function allocates resources that cannot be freed.
|
||||
There is no backtrace_free_state function. The state is used to
|
||||
cache information that is expensive to recompute. Programs are
|
||||
expected to call this function at most once and to save the return
|
||||
value for all later calls to backtrace functions. */
|
||||
|
||||
extern struct backtrace_state *backtrace_create_state (
|
||||
const char *filename, int threaded,
|
||||
backtrace_error_callback error_callback, void *data);
|
||||
|
||||
/* The type of the callback argument to the backtrace_full function.
|
||||
DATA is the argument passed to backtrace_full. PC is the program
|
||||
counter. FILENAME is the name of the file containing PC, or NULL
|
||||
if not available. LINENO is the line number in FILENAME containing
|
||||
PC, or 0 if not available. FUNCTION is the name of the function
|
||||
containing PC, or NULL if not available. This should return 0 to
|
||||
continuing tracing. The FILENAME and FUNCTION buffers may become
|
||||
invalid after this function returns. */
|
||||
|
||||
typedef int (*backtrace_full_callback) (void *data, uintptr_t pc,
|
||||
const char *filename, int lineno,
|
||||
const char *function);
|
||||
|
||||
/* Get a full stack backtrace. SKIP is the number of frames to skip;
|
||||
passing 0 will start the trace with the function calling
|
||||
backtrace_full. DATA is passed to the callback routine. If any
|
||||
call to CALLBACK returns a non-zero value, the stack backtrace
|
||||
stops, and backtrace returns that value; this may be used to limit
|
||||
the number of stack frames desired. If all calls to CALLBACK
|
||||
return 0, backtrace returns 0. The backtrace_full function will
|
||||
make at least one call to either CALLBACK or ERROR_CALLBACK. This
|
||||
function requires debug info for the executable. */
|
||||
|
||||
extern int backtrace_full (struct backtrace_state *state, int skip,
|
||||
backtrace_full_callback callback,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
/* The type of the callback argument to the backtrace_simple function.
|
||||
DATA is the argument passed to simple_backtrace. PC is the program
|
||||
counter. This should return 0 to continue tracing. */
|
||||
|
||||
typedef int (*backtrace_simple_callback) (void *data, uintptr_t pc);
|
||||
|
||||
/* Get a simple backtrace. SKIP is the number of frames to skip, as
|
||||
in backtrace. DATA is passed to the callback routine. If any call
|
||||
to CALLBACK returns a non-zero value, the stack backtrace stops,
|
||||
and backtrace_simple returns that value. Otherwise
|
||||
backtrace_simple returns 0. The backtrace_simple function will
|
||||
make at least one call to either CALLBACK or ERROR_CALLBACK. This
|
||||
function does not require any debug info for the executable. */
|
||||
|
||||
extern int backtrace_simple (struct backtrace_state *state, int skip,
|
||||
backtrace_simple_callback callback,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
/* Print the current backtrace in a user readable format to a FILE.
|
||||
SKIP is the number of frames to skip, as in backtrace_full. Any
|
||||
error messages are printed to stderr. This function requires debug
|
||||
info for the executable. */
|
||||
|
||||
extern void backtrace_print (struct backtrace_state *state, int skip, FILE *);
|
||||
|
||||
/* Given PC, a program counter in the current program, call the
|
||||
callback function with filename, line number, and function name
|
||||
information. This will normally call the callback function exactly
|
||||
once. However, if the PC happens to describe an inlined call, and
|
||||
the debugging information contains the necessary information, then
|
||||
this may call the callback function multiple times. This will make
|
||||
at least one call to either CALLBACK or ERROR_CALLBACK. This
|
||||
returns the first non-zero value returned by CALLBACK, or 0. */
|
||||
|
||||
extern int backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
|
||||
backtrace_full_callback callback,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
/* The type of the callback argument to backtrace_syminfo. DATA and
|
||||
PC are the arguments passed to backtrace_syminfo. SYMNAME is the
|
||||
name of the symbol for the corresponding code. SYMVAL is the
|
||||
value and SYMSIZE is the size of the symbol. SYMNAME will be NULL
|
||||
if no error occurred but the symbol could not be found. */
|
||||
|
||||
typedef void (*backtrace_syminfo_callback) (void *data, uintptr_t pc,
|
||||
const char *symname,
|
||||
uintptr_t symval,
|
||||
uintptr_t symsize);
|
||||
|
||||
/* Given ADDR, an address or program counter in the current program,
|
||||
call the callback information with the symbol name and value
|
||||
describing the function or variable in which ADDR may be found.
|
||||
This will call either CALLBACK or ERROR_CALLBACK exactly once.
|
||||
This returns 1 on success, 0 on failure. This function requires
|
||||
the symbol table but does not require the debug info. Note that if
|
||||
the symbol table is present but ADDR could not be found in the
|
||||
table, CALLBACK will be called with a NULL SYMNAME argument.
|
||||
Returns 1 on success, 0 on error. */
|
||||
|
||||
extern int backtrace_syminfo (struct backtrace_state *state, uintptr_t addr,
|
||||
backtrace_syminfo_callback callback,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* End extern "C". */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,382 @@
|
||||
#ifdef __linux__
|
||||
/* internal.h -- Internal header file for stack backtrace library.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#ifndef BACKTRACE_INTERNAL_H
|
||||
#define BACKTRACE_INTERNAL_H
|
||||
|
||||
/* We assume that <sys/types.h> and "include/backtrace.h" have already been
|
||||
included. */
|
||||
|
||||
#ifndef GCC_VERSION
|
||||
# define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
|
||||
#endif
|
||||
|
||||
#if (GCC_VERSION < 2007)
|
||||
# define __attribute__(x)
|
||||
#endif
|
||||
|
||||
#ifndef ATTRIBUTE_UNUSED
|
||||
# define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
|
||||
#endif
|
||||
|
||||
#ifndef ATTRIBUTE_MALLOC
|
||||
# if (GCC_VERSION >= 2096)
|
||||
# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
|
||||
# else
|
||||
# define ATTRIBUTE_MALLOC
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef ATTRIBUTE_FALLTHROUGH
|
||||
# if (GCC_VERSION >= 7000)
|
||||
# define ATTRIBUTE_FALLTHROUGH __attribute__ ((__fallthrough__))
|
||||
# else
|
||||
# define ATTRIBUTE_FALLTHROUGH
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_SYNC_FUNCTIONS
|
||||
|
||||
/* Define out the sync functions. These should never be called if
|
||||
they are not available. */
|
||||
|
||||
#define __sync_bool_compare_and_swap(A, B, C) (abort(), 1)
|
||||
#define __sync_lock_test_and_set(A, B) (abort(), 0)
|
||||
#define __sync_lock_release(A) abort()
|
||||
|
||||
#endif /* !defined (HAVE_SYNC_FUNCTIONS) */
|
||||
|
||||
#ifdef HAVE_ATOMIC_FUNCTIONS
|
||||
|
||||
/* We have the atomic builtin functions. */
|
||||
|
||||
#define backtrace_atomic_load_pointer(p) \
|
||||
__atomic_load_n ((p), __ATOMIC_ACQUIRE)
|
||||
#define backtrace_atomic_load_int(p) \
|
||||
__atomic_load_n ((p), __ATOMIC_ACQUIRE)
|
||||
#define backtrace_atomic_store_pointer(p, v) \
|
||||
__atomic_store_n ((p), (v), __ATOMIC_RELEASE)
|
||||
#define backtrace_atomic_store_size_t(p, v) \
|
||||
__atomic_store_n ((p), (v), __ATOMIC_RELEASE)
|
||||
#define backtrace_atomic_store_int(p, v) \
|
||||
__atomic_store_n ((p), (v), __ATOMIC_RELEASE)
|
||||
|
||||
#else /* !defined (HAVE_ATOMIC_FUNCTIONS) */
|
||||
#ifdef HAVE_SYNC_FUNCTIONS
|
||||
|
||||
/* We have the sync functions but not the atomic functions. Define
|
||||
the atomic ones in terms of the sync ones. */
|
||||
|
||||
extern void *backtrace_atomic_load_pointer (void *);
|
||||
extern int backtrace_atomic_load_int (int *);
|
||||
extern void backtrace_atomic_store_pointer (void *, void *);
|
||||
extern void backtrace_atomic_store_size_t (size_t *, size_t);
|
||||
extern void backtrace_atomic_store_int (int *, int);
|
||||
|
||||
#else /* !defined (HAVE_SYNC_FUNCTIONS) */
|
||||
|
||||
/* We have neither the sync nor the atomic functions. These will
|
||||
never be called. */
|
||||
|
||||
#define backtrace_atomic_load_pointer(p) (abort(), (void *) NULL)
|
||||
#define backtrace_atomic_load_int(p) (abort(), 0)
|
||||
#define backtrace_atomic_store_pointer(p, v) abort()
|
||||
#define backtrace_atomic_store_size_t(p, v) abort()
|
||||
#define backtrace_atomic_store_int(p, v) abort()
|
||||
|
||||
#endif /* !defined (HAVE_SYNC_FUNCTIONS) */
|
||||
#endif /* !defined (HAVE_ATOMIC_FUNCTIONS) */
|
||||
|
||||
/* The type of the function that collects file/line information. This
|
||||
is like backtrace_pcinfo. */
|
||||
|
||||
typedef int (*fileline) (struct backtrace_state *state, uintptr_t pc,
|
||||
backtrace_full_callback callback,
|
||||
backtrace_error_callback error_callback, void *data);
|
||||
|
||||
/* The type of the function that collects symbol information. This is
|
||||
like backtrace_syminfo. */
|
||||
|
||||
typedef void (*syminfo) (struct backtrace_state *state, uintptr_t pc,
|
||||
backtrace_syminfo_callback callback,
|
||||
backtrace_error_callback error_callback, void *data);
|
||||
|
||||
/* What the backtrace state pointer points to. */
|
||||
|
||||
struct backtrace_state
|
||||
{
|
||||
/* The name of the executable. */
|
||||
const char *filename;
|
||||
/* Non-zero if threaded. */
|
||||
int threaded;
|
||||
/* The master lock for fileline_fn, fileline_data, syminfo_fn,
|
||||
syminfo_data, fileline_initialization_failed and everything the
|
||||
data pointers point to. */
|
||||
void *lock;
|
||||
/* The function that returns file/line information. */
|
||||
fileline fileline_fn;
|
||||
/* The data to pass to FILELINE_FN. */
|
||||
void *fileline_data;
|
||||
/* The function that returns symbol information. */
|
||||
syminfo syminfo_fn;
|
||||
/* The data to pass to SYMINFO_FN. */
|
||||
void *syminfo_data;
|
||||
/* Whether initializing the file/line information failed. */
|
||||
int fileline_initialization_failed;
|
||||
/* The lock for the freelist. */
|
||||
int lock_alloc;
|
||||
/* The freelist when using mmap. */
|
||||
struct backtrace_freelist_struct *freelist;
|
||||
};
|
||||
|
||||
/* Open a file for reading. Returns -1 on error. If DOES_NOT_EXIST
|
||||
is not NULL, *DOES_NOT_EXIST will be set to 0 normally and set to 1
|
||||
if the file does not exist. If the file does not exist and
|
||||
DOES_NOT_EXIST is not NULL, the function will return -1 and will
|
||||
not call ERROR_CALLBACK. On other errors, or if DOES_NOT_EXIST is
|
||||
NULL, the function will call ERROR_CALLBACK before returning. */
|
||||
extern int backtrace_open (const char *filename,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data,
|
||||
int *does_not_exist);
|
||||
|
||||
/* A view of the contents of a file. This supports mmap when
|
||||
available. A view will remain in memory even after backtrace_close
|
||||
is called on the file descriptor from which the view was
|
||||
obtained. */
|
||||
|
||||
struct backtrace_view
|
||||
{
|
||||
/* The data that the caller requested. */
|
||||
const void *data;
|
||||
/* The base of the view. */
|
||||
void *base;
|
||||
/* The total length of the view. */
|
||||
size_t len;
|
||||
};
|
||||
|
||||
/* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. Store the
|
||||
result in *VIEW. Returns 1 on success, 0 on error. */
|
||||
extern int backtrace_get_view (struct backtrace_state *state, int descriptor,
|
||||
off_t offset, uint64_t size,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data, struct backtrace_view *view);
|
||||
|
||||
/* Release a view created by backtrace_get_view. */
|
||||
extern void backtrace_release_view (struct backtrace_state *state,
|
||||
struct backtrace_view *view,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
/* Close a file opened by backtrace_open. Returns 1 on success, 0 on
|
||||
error. */
|
||||
|
||||
extern int backtrace_close (int descriptor,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
/* Sort without using memory. */
|
||||
|
||||
extern void backtrace_qsort (void *base, size_t count, size_t size,
|
||||
int (*compar) (const void *, const void *));
|
||||
|
||||
/* Allocate memory. This is like malloc. If ERROR_CALLBACK is NULL,
|
||||
this does not report an error, it just returns NULL. */
|
||||
|
||||
extern void *backtrace_alloc (struct backtrace_state *state, size_t size,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data) ATTRIBUTE_MALLOC;
|
||||
|
||||
/* Free memory allocated by backtrace_alloc. If ERROR_CALLBACK is
|
||||
NULL, this does not report an error. */
|
||||
|
||||
extern void backtrace_free (struct backtrace_state *state, void *mem,
|
||||
size_t size,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
/* A growable vector of some struct. This is used for more efficient
|
||||
allocation when we don't know the final size of some group of data
|
||||
that we want to represent as an array. */
|
||||
|
||||
struct backtrace_vector
|
||||
{
|
||||
/* The base of the vector. */
|
||||
void *base;
|
||||
/* The number of bytes in the vector. */
|
||||
size_t size;
|
||||
/* The number of bytes available at the current allocation. */
|
||||
size_t alc;
|
||||
};
|
||||
|
||||
/* Grow VEC by SIZE bytes. Return a pointer to the newly allocated
|
||||
bytes. Note that this may move the entire vector to a new memory
|
||||
location. Returns NULL on failure. */
|
||||
|
||||
extern void *backtrace_vector_grow (struct backtrace_state *state, size_t size,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data,
|
||||
struct backtrace_vector *vec);
|
||||
|
||||
/* Finish the current allocation on VEC. Prepare to start a new
|
||||
allocation. The finished allocation will never be freed. Returns
|
||||
a pointer to the base of the finished entries, or NULL on
|
||||
failure. */
|
||||
|
||||
extern void* backtrace_vector_finish (struct backtrace_state *state,
|
||||
struct backtrace_vector *vec,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
/* Release any extra space allocated for VEC. This may change
|
||||
VEC->base. Returns 1 on success, 0 on failure. */
|
||||
|
||||
extern int backtrace_vector_release (struct backtrace_state *state,
|
||||
struct backtrace_vector *vec,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data);
|
||||
|
||||
/* Free the space managed by VEC. This will reset VEC. */
|
||||
|
||||
static inline void
|
||||
backtrace_vector_free (struct backtrace_state *state,
|
||||
struct backtrace_vector *vec,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
vec->alc += vec->size;
|
||||
vec->size = 0;
|
||||
backtrace_vector_release (state, vec, error_callback, data);
|
||||
}
|
||||
|
||||
/* Read initial debug data from a descriptor, and set the
|
||||
fileline_data, syminfo_fn, and syminfo_data fields of STATE.
|
||||
Return the fileln_fn field in *FILELN_FN--this is done this way so
|
||||
that the synchronization code is only implemented once. This is
|
||||
called after the descriptor has first been opened. It will close
|
||||
the descriptor if it is no longer needed. Returns 1 on success, 0
|
||||
on error. There will be multiple implementations of this function,
|
||||
for different file formats. Each system will compile the
|
||||
appropriate one. */
|
||||
|
||||
extern int backtrace_initialize (struct backtrace_state *state,
|
||||
const char *filename,
|
||||
int descriptor,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data,
|
||||
fileline *fileline_fn);
|
||||
|
||||
/* An enum for the DWARF sections we care about. */
|
||||
|
||||
enum dwarf_section
|
||||
{
|
||||
DEBUG_INFO,
|
||||
DEBUG_LINE,
|
||||
DEBUG_ABBREV,
|
||||
DEBUG_RANGES,
|
||||
DEBUG_STR,
|
||||
DEBUG_ADDR,
|
||||
DEBUG_STR_OFFSETS,
|
||||
DEBUG_LINE_STR,
|
||||
DEBUG_RNGLISTS,
|
||||
|
||||
DEBUG_MAX
|
||||
};
|
||||
|
||||
/* Data for the DWARF sections we care about. */
|
||||
|
||||
struct dwarf_sections
|
||||
{
|
||||
const unsigned char *data[DEBUG_MAX];
|
||||
size_t size[DEBUG_MAX];
|
||||
};
|
||||
|
||||
/* DWARF data read from a file, used for .gnu_debugaltlink. */
|
||||
|
||||
struct dwarf_data;
|
||||
|
||||
/* Add file/line information for a DWARF module. */
|
||||
|
||||
extern int backtrace_dwarf_add (struct backtrace_state *state,
|
||||
uintptr_t base_address,
|
||||
const struct dwarf_sections *dwarf_sections,
|
||||
int is_bigendian,
|
||||
struct dwarf_data *fileline_altlink,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data, fileline *fileline_fn,
|
||||
struct dwarf_data **fileline_entry);
|
||||
|
||||
/* A data structure to pass to backtrace_syminfo_to_full. */
|
||||
|
||||
struct backtrace_call_full
|
||||
{
|
||||
backtrace_full_callback full_callback;
|
||||
backtrace_error_callback full_error_callback;
|
||||
void *full_data;
|
||||
int ret;
|
||||
};
|
||||
|
||||
/* A backtrace_syminfo_callback that can call into a
|
||||
backtrace_full_callback, used when we have a symbol table but no
|
||||
debug info. */
|
||||
|
||||
extern void backtrace_syminfo_to_full_callback (void *data, uintptr_t pc,
|
||||
const char *symname,
|
||||
uintptr_t symval,
|
||||
uintptr_t symsize);
|
||||
|
||||
/* An error callback that corresponds to
|
||||
backtrace_syminfo_to_full_callback. */
|
||||
|
||||
extern void backtrace_syminfo_to_full_error_callback (void *, const char *,
|
||||
int);
|
||||
|
||||
/* A test-only hook for elf_uncompress_zdebug. */
|
||||
|
||||
extern int backtrace_uncompress_zdebug (struct backtrace_state *,
|
||||
const unsigned char *compressed,
|
||||
size_t compressed_size,
|
||||
backtrace_error_callback, void *data,
|
||||
unsigned char **uncompressed,
|
||||
size_t *uncompressed_size);
|
||||
|
||||
/* A test-only hook for elf_uncompress_lzma. */
|
||||
|
||||
extern int backtrace_uncompress_lzma (struct backtrace_state *,
|
||||
const unsigned char *compressed,
|
||||
size_t compressed_size,
|
||||
backtrace_error_callback, void *data,
|
||||
unsigned char **uncompressed,
|
||||
size_t *uncompressed_size);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,333 @@
|
||||
#ifdef __linux__
|
||||
/* mmap.c -- Memory allocation with mmap.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "include/backtrace.h"
|
||||
#include "internal.h"
|
||||
|
||||
#ifndef HAVE_DECL_GETPAGESIZE
|
||||
extern int getpagesize (void);
|
||||
#endif
|
||||
|
||||
/* Memory allocation on systems that provide anonymous mmap. This
|
||||
permits the backtrace functions to be invoked from a signal
|
||||
handler, assuming that mmap is async-signal safe. */
|
||||
|
||||
#ifndef MAP_ANONYMOUS
|
||||
#define MAP_ANONYMOUS MAP_ANON
|
||||
#endif
|
||||
|
||||
#ifndef MAP_FAILED
|
||||
#define MAP_FAILED ((void *)-1)
|
||||
#endif
|
||||
|
||||
/* A list of free memory blocks. */
|
||||
|
||||
struct backtrace_freelist_struct
|
||||
{
|
||||
/* Next on list. */
|
||||
struct backtrace_freelist_struct *next;
|
||||
/* Size of this block, including this structure. */
|
||||
size_t size;
|
||||
};
|
||||
|
||||
/* Free memory allocated by backtrace_alloc. */
|
||||
|
||||
static void
|
||||
backtrace_free_locked (struct backtrace_state *state, void *addr, size_t size)
|
||||
{
|
||||
/* Just leak small blocks. We don't have to be perfect. Don't put
|
||||
more than 16 entries on the free list, to avoid wasting time
|
||||
searching when allocating a block. If we have more than 16
|
||||
entries, leak the smallest entry. */
|
||||
|
||||
if (size >= sizeof (struct backtrace_freelist_struct))
|
||||
{
|
||||
size_t c;
|
||||
struct backtrace_freelist_struct **ppsmall;
|
||||
struct backtrace_freelist_struct **pp;
|
||||
struct backtrace_freelist_struct *p;
|
||||
|
||||
c = 0;
|
||||
ppsmall = NULL;
|
||||
for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
|
||||
{
|
||||
if (ppsmall == NULL || (*pp)->size < (*ppsmall)->size)
|
||||
ppsmall = pp;
|
||||
++c;
|
||||
}
|
||||
if (c >= 16)
|
||||
{
|
||||
if (size <= (*ppsmall)->size)
|
||||
return;
|
||||
*ppsmall = (*ppsmall)->next;
|
||||
}
|
||||
|
||||
p = (struct backtrace_freelist_struct *) addr;
|
||||
p->next = state->freelist;
|
||||
p->size = size;
|
||||
state->freelist = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't
|
||||
report an error. */
|
||||
|
||||
void *
|
||||
backtrace_alloc (struct backtrace_state *state,
|
||||
size_t size, backtrace_error_callback error_callback,
|
||||
void *data)
|
||||
{
|
||||
void *ret;
|
||||
int locked;
|
||||
struct backtrace_freelist_struct **pp;
|
||||
size_t pagesize;
|
||||
size_t asksize;
|
||||
void *page;
|
||||
|
||||
ret = NULL;
|
||||
|
||||
/* If we can acquire the lock, then see if there is space on the
|
||||
free list. If we can't acquire the lock, drop straight into
|
||||
using mmap. __sync_lock_test_and_set returns the old state of
|
||||
the lock, so we have acquired it if it returns 0. */
|
||||
|
||||
if (!state->threaded)
|
||||
locked = 1;
|
||||
else
|
||||
locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
|
||||
|
||||
if (locked)
|
||||
{
|
||||
for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
|
||||
{
|
||||
if ((*pp)->size >= size)
|
||||
{
|
||||
struct backtrace_freelist_struct *p;
|
||||
|
||||
p = *pp;
|
||||
*pp = p->next;
|
||||
|
||||
/* Round for alignment; we assume that no type we care about
|
||||
is more than 8 bytes. */
|
||||
size = (size + 7) & ~ (size_t) 7;
|
||||
if (size < p->size)
|
||||
backtrace_free_locked (state, (char *) p + size,
|
||||
p->size - size);
|
||||
|
||||
ret = (void *) p;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (state->threaded)
|
||||
__sync_lock_release (&state->lock_alloc);
|
||||
}
|
||||
|
||||
if (ret == NULL)
|
||||
{
|
||||
/* Allocate a new page. */
|
||||
|
||||
pagesize = getpagesize ();
|
||||
asksize = (size + pagesize - 1) & ~ (pagesize - 1);
|
||||
page = mmap (NULL, asksize, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (page == MAP_FAILED)
|
||||
{
|
||||
if (error_callback)
|
||||
error_callback (data, "mmap", errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
size = (size + 7) & ~ (size_t) 7;
|
||||
if (size < asksize)
|
||||
backtrace_free (state, (char *) page + size, asksize - size,
|
||||
error_callback, data);
|
||||
|
||||
ret = page;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Free memory allocated by backtrace_alloc. */
|
||||
|
||||
void
|
||||
backtrace_free (struct backtrace_state *state, void *addr, size_t size,
|
||||
backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
|
||||
void *data ATTRIBUTE_UNUSED)
|
||||
{
|
||||
int locked;
|
||||
|
||||
/* If we are freeing a large aligned block, just release it back to
|
||||
the system. This case arises when growing a vector for a large
|
||||
binary with lots of debug info. Calling munmap here may cause us
|
||||
to call mmap again if there is also a large shared library; we
|
||||
just live with that. */
|
||||
if (size >= 16 * 4096)
|
||||
{
|
||||
size_t pagesize;
|
||||
|
||||
pagesize = getpagesize ();
|
||||
if (((uintptr_t) addr & (pagesize - 1)) == 0
|
||||
&& (size & (pagesize - 1)) == 0)
|
||||
{
|
||||
/* If munmap fails for some reason, just add the block to
|
||||
the freelist. */
|
||||
if (munmap (addr, size) == 0)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we can acquire the lock, add the new space to the free list.
|
||||
If we can't acquire the lock, just leak the memory.
|
||||
__sync_lock_test_and_set returns the old state of the lock, so we
|
||||
have acquired it if it returns 0. */
|
||||
|
||||
if (!state->threaded)
|
||||
locked = 1;
|
||||
else
|
||||
locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
|
||||
|
||||
if (locked)
|
||||
{
|
||||
backtrace_free_locked (state, addr, size);
|
||||
|
||||
if (state->threaded)
|
||||
__sync_lock_release (&state->lock_alloc);
|
||||
}
|
||||
}
|
||||
|
||||
/* Grow VEC by SIZE bytes. */
|
||||
|
||||
void *
|
||||
backtrace_vector_grow (struct backtrace_state *state,size_t size,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data, struct backtrace_vector *vec)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
if (size > vec->alc)
|
||||
{
|
||||
size_t pagesize;
|
||||
size_t alc;
|
||||
void *base;
|
||||
|
||||
pagesize = getpagesize ();
|
||||
alc = vec->size + size;
|
||||
if (vec->size == 0)
|
||||
alc = 16 * size;
|
||||
else if (alc < pagesize)
|
||||
{
|
||||
alc *= 2;
|
||||
if (alc > pagesize)
|
||||
alc = pagesize;
|
||||
}
|
||||
else
|
||||
{
|
||||
alc *= 2;
|
||||
alc = (alc + pagesize - 1) & ~ (pagesize - 1);
|
||||
}
|
||||
base = backtrace_alloc (state, alc, error_callback, data);
|
||||
if (base == NULL)
|
||||
return NULL;
|
||||
if (vec->base != NULL)
|
||||
{
|
||||
memcpy (base, vec->base, vec->size);
|
||||
backtrace_free (state, vec->base, vec->size + vec->alc,
|
||||
error_callback, data);
|
||||
}
|
||||
vec->base = base;
|
||||
vec->alc = alc - vec->size;
|
||||
}
|
||||
|
||||
ret = (char *) vec->base + vec->size;
|
||||
vec->size += size;
|
||||
vec->alc -= size;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Finish the current allocation on VEC. */
|
||||
|
||||
void *
|
||||
backtrace_vector_finish (
|
||||
struct backtrace_state *state ATTRIBUTE_UNUSED,
|
||||
struct backtrace_vector *vec,
|
||||
backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
|
||||
void *data ATTRIBUTE_UNUSED)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
ret = vec->base;
|
||||
vec->base = (char *) vec->base + vec->size;
|
||||
vec->size = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Release any extra space allocated for VEC. */
|
||||
|
||||
int
|
||||
backtrace_vector_release (struct backtrace_state *state,
|
||||
struct backtrace_vector *vec,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data)
|
||||
{
|
||||
size_t size;
|
||||
size_t alc;
|
||||
size_t aligned;
|
||||
|
||||
/* Make sure that the block that we free is aligned on an 8-byte
|
||||
boundary. */
|
||||
size = vec->size;
|
||||
alc = vec->alc;
|
||||
aligned = (size + 7) & ~ (size_t) 7;
|
||||
alc -= aligned - size;
|
||||
|
||||
backtrace_free (state, (char *) vec->base + aligned, alc,
|
||||
error_callback, data);
|
||||
vec->alc = 0;
|
||||
if (vec->size == 0)
|
||||
vec->base = NULL;
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,112 @@
|
||||
#ifdef __linux__
|
||||
/* mmapio.c -- File views using mmap.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "include/backtrace.h"
|
||||
#include "internal.h"
|
||||
|
||||
#ifndef HAVE_DECL_GETPAGESIZE
|
||||
extern int getpagesize (void);
|
||||
#endif
|
||||
|
||||
#ifndef MAP_FAILED
|
||||
#define MAP_FAILED ((void *)-1)
|
||||
#endif
|
||||
|
||||
/* This file implements file views and memory allocation when mmap is
|
||||
available. */
|
||||
|
||||
/* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. */
|
||||
|
||||
int
|
||||
backtrace_get_view (struct backtrace_state *state ATTRIBUTE_UNUSED,
|
||||
int descriptor, off_t offset, uint64_t size,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data, struct backtrace_view *view)
|
||||
{
|
||||
size_t pagesize;
|
||||
unsigned int inpage;
|
||||
off_t pageoff;
|
||||
void *map;
|
||||
|
||||
if ((uint64_t) (size_t) size != size)
|
||||
{
|
||||
error_callback (data, "file size too large", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pagesize = getpagesize ();
|
||||
inpage = offset % pagesize;
|
||||
pageoff = offset - inpage;
|
||||
|
||||
size += inpage;
|
||||
size = (size + (pagesize - 1)) & ~ (pagesize - 1);
|
||||
|
||||
map = mmap (NULL, size, PROT_READ, MAP_PRIVATE, descriptor, pageoff);
|
||||
if (map == MAP_FAILED)
|
||||
{
|
||||
error_callback (data, "mmap", errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
view->data = (char *) map + inpage;
|
||||
view->base = map;
|
||||
view->len = size;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Release a view read by backtrace_get_view. */
|
||||
|
||||
void
|
||||
backtrace_release_view (struct backtrace_state *state ATTRIBUTE_UNUSED,
|
||||
struct backtrace_view *view,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data)
|
||||
{
|
||||
union {
|
||||
const void *cv;
|
||||
void *v;
|
||||
} const_cast;
|
||||
|
||||
const_cast.cv = view->base;
|
||||
if (munmap (const_cast.v, view->len) < 0)
|
||||
error_callback (data, "munmap", errno);
|
||||
}
|
||||
#endif
|
||||
@@ -1,4 +0,0 @@
|
||||
module CBacktrace {
|
||||
header "shim.h"
|
||||
export *
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
#ifdef __linux__
|
||||
/* posix.c -- POSIX file I/O routines for the backtrace library.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "include/backtrace.h"
|
||||
#include "internal.h"
|
||||
|
||||
#ifndef O_BINARY
|
||||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
#ifndef O_CLOEXEC
|
||||
#define O_CLOEXEC 0
|
||||
#endif
|
||||
|
||||
#ifndef FD_CLOEXEC
|
||||
#define FD_CLOEXEC 1
|
||||
#endif
|
||||
|
||||
/* Open a file for reading. */
|
||||
|
||||
int
|
||||
backtrace_open (const char *filename, backtrace_error_callback error_callback,
|
||||
void *data, int *does_not_exist)
|
||||
{
|
||||
int descriptor;
|
||||
|
||||
if (does_not_exist != NULL)
|
||||
*does_not_exist = 0;
|
||||
|
||||
descriptor = open (filename, (int) (O_RDONLY | O_BINARY | O_CLOEXEC));
|
||||
if (descriptor < 0)
|
||||
{
|
||||
/* If DOES_NOT_EXIST is not NULL, then don't call ERROR_CALLBACK
|
||||
if the file does not exist. We treat lacking permission to
|
||||
open the file as the file not existing; this case arises when
|
||||
running the libgo syscall package tests as root. */
|
||||
if (does_not_exist != NULL && (errno == ENOENT || errno == EACCES))
|
||||
*does_not_exist = 1;
|
||||
else
|
||||
error_callback (data, filename, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef HAVE_FCNTL
|
||||
/* Set FD_CLOEXEC just in case the kernel does not support
|
||||
O_CLOEXEC. It doesn't matter if this fails for some reason.
|
||||
FIXME: At some point it should be safe to only do this if
|
||||
O_CLOEXEC == 0. */
|
||||
fcntl (descriptor, F_SETFD, FD_CLOEXEC);
|
||||
#endif
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/* Close DESCRIPTOR. */
|
||||
|
||||
int
|
||||
backtrace_close (int descriptor, backtrace_error_callback error_callback,
|
||||
void *data)
|
||||
{
|
||||
if (close (descriptor) < 0)
|
||||
{
|
||||
error_callback (data, "close", errno);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,94 @@
|
||||
#ifdef __linux__
|
||||
/* print.c -- Print the current backtrace.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "include/backtrace.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* Passed to callbacks. */
|
||||
|
||||
struct print_data
|
||||
{
|
||||
struct backtrace_state *state;
|
||||
FILE *f;
|
||||
};
|
||||
|
||||
/* Print one level of a backtrace. */
|
||||
|
||||
static int
|
||||
print_callback (void *data, uintptr_t pc, const char *filename, int lineno,
|
||||
const char *function)
|
||||
{
|
||||
struct print_data *pdata = (struct print_data *) data;
|
||||
|
||||
fprintf (pdata->f, "0x%lx %s\n\t%s:%d\n",
|
||||
(unsigned long) pc,
|
||||
function == NULL ? "???" : function,
|
||||
filename == NULL ? "???" : filename,
|
||||
lineno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Print errors to stderr. */
|
||||
|
||||
static void
|
||||
error_callback (void *data, const char *msg, int errnum)
|
||||
{
|
||||
struct print_data *pdata = (struct print_data *) data;
|
||||
|
||||
if (pdata->state->filename != NULL)
|
||||
fprintf (stderr, "%s: ", pdata->state->filename);
|
||||
fprintf (stderr, "libbacktrace: %s", msg);
|
||||
if (errnum > 0)
|
||||
fprintf (stderr, ": %s", strerror (errnum));
|
||||
fputc ('\n', stderr);
|
||||
}
|
||||
|
||||
/* Print a backtrace. */
|
||||
|
||||
void __attribute__((noinline))
|
||||
backtrace_print (struct backtrace_state *state, int skip, FILE *f)
|
||||
{
|
||||
struct print_data data;
|
||||
|
||||
data.state = state;
|
||||
data.f = f;
|
||||
backtrace_full (state, skip + 1, print_callback, error_callback,
|
||||
(void *) &data);
|
||||
}
|
||||
#endif
|
||||
@@ -1,6 +0,0 @@
|
||||
#ifndef shim_h
|
||||
#define shim_h
|
||||
|
||||
#include <execinfo.h>
|
||||
|
||||
#endif /* shim_h */
|
||||
@@ -0,0 +1,110 @@
|
||||
#ifdef __linux__
|
||||
/* simple.c -- The backtrace_simple function.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "unwind.h"
|
||||
#include "include/backtrace.h"
|
||||
|
||||
/* The simple_backtrace routine. */
|
||||
|
||||
/* Data passed through _Unwind_Backtrace. */
|
||||
|
||||
struct backtrace_simple_data
|
||||
{
|
||||
/* Number of frames to skip. */
|
||||
int skip;
|
||||
/* Library state. */
|
||||
struct backtrace_state *state;
|
||||
/* Callback routine. */
|
||||
backtrace_simple_callback callback;
|
||||
/* Error callback routine. */
|
||||
backtrace_error_callback error_callback;
|
||||
/* Data to pass to callback routine. */
|
||||
void *data;
|
||||
/* Value to return from backtrace. */
|
||||
int ret;
|
||||
};
|
||||
|
||||
/* Unwind library callback routine. This is passed to
|
||||
_Unwind_Backtrace. */
|
||||
|
||||
static _Unwind_Reason_Code
|
||||
simple_unwind (struct _Unwind_Context *context, void *vdata)
|
||||
{
|
||||
struct backtrace_simple_data *bdata = (struct backtrace_simple_data *) vdata;
|
||||
uintptr_t pc;
|
||||
int ip_before_insn = 0;
|
||||
|
||||
#ifdef HAVE_GETIPINFO
|
||||
pc = _Unwind_GetIPInfo (context, &ip_before_insn);
|
||||
#else
|
||||
pc = _Unwind_GetIP (context);
|
||||
#endif
|
||||
|
||||
if (bdata->skip > 0)
|
||||
{
|
||||
--bdata->skip;
|
||||
return _URC_NO_REASON;
|
||||
}
|
||||
|
||||
if (!ip_before_insn)
|
||||
--pc;
|
||||
|
||||
bdata->ret = bdata->callback (bdata->data, pc);
|
||||
|
||||
if (bdata->ret != 0)
|
||||
return _URC_END_OF_STACK;
|
||||
|
||||
return _URC_NO_REASON;
|
||||
}
|
||||
|
||||
/* Get a simple stack backtrace. */
|
||||
|
||||
int __attribute__((noinline))
|
||||
backtrace_simple (struct backtrace_state *state, int skip,
|
||||
backtrace_simple_callback callback,
|
||||
backtrace_error_callback error_callback, void *data)
|
||||
{
|
||||
struct backtrace_simple_data bdata;
|
||||
|
||||
bdata.skip = skip + 1;
|
||||
bdata.state = state;
|
||||
bdata.callback = callback;
|
||||
bdata.error_callback = error_callback;
|
||||
bdata.data = data;
|
||||
bdata.ret = 0;
|
||||
_Unwind_Backtrace (simple_unwind, &bdata);
|
||||
return bdata.ret;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,110 @@
|
||||
#ifdef __linux__
|
||||
/* sort.c -- Sort without allocating memory
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "include/backtrace.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* The GNU glibc version of qsort allocates memory, which we must not
|
||||
do if we are invoked by a signal handler. So provide our own
|
||||
sort. */
|
||||
|
||||
static void
|
||||
swap (char *a, char *b, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < size; i++, a++, b++)
|
||||
{
|
||||
char t;
|
||||
|
||||
t = *a;
|
||||
*a = *b;
|
||||
*b = t;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
backtrace_qsort (void *basearg, size_t count, size_t size,
|
||||
int (*compar) (const void *, const void *))
|
||||
{
|
||||
char *base = (char *) basearg;
|
||||
size_t i;
|
||||
size_t mid;
|
||||
|
||||
tail_recurse:
|
||||
if (count < 2)
|
||||
return;
|
||||
|
||||
/* The symbol table and DWARF tables, which is all we use this
|
||||
routine for, tend to be roughly sorted. Pick the middle element
|
||||
in the array as our pivot point, so that we are more likely to
|
||||
cut the array in half for each recursion step. */
|
||||
swap (base, base + (count / 2) * size, size);
|
||||
|
||||
mid = 0;
|
||||
for (i = 1; i < count; i++)
|
||||
{
|
||||
if ((*compar) (base, base + i * size) > 0)
|
||||
{
|
||||
++mid;
|
||||
if (i != mid)
|
||||
swap (base + mid * size, base + i * size, size);
|
||||
}
|
||||
}
|
||||
|
||||
if (mid > 0)
|
||||
swap (base, base + mid * size, size);
|
||||
|
||||
/* Recurse with the smaller array, loop with the larger one. That
|
||||
ensures that our maximum stack depth is log count. */
|
||||
if (2 * mid < count)
|
||||
{
|
||||
backtrace_qsort (base, mid, size, compar);
|
||||
base += (mid + 1) * size;
|
||||
count -= mid + 1;
|
||||
goto tail_recurse;
|
||||
}
|
||||
else
|
||||
{
|
||||
backtrace_qsort (base + (mid + 1) * size, count - (mid + 1),
|
||||
size, compar);
|
||||
count = mid;
|
||||
goto tail_recurse;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,74 @@
|
||||
#ifdef __linux__
|
||||
/* state.c -- Create the backtrace state.
|
||||
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
Written by Ian Lance Taylor, Google.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
(1) Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
(3) The name of the author may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE. */
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "include/backtrace.h"
|
||||
#include "backtrace-supported.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* Create the backtrace state. This will then be passed to all the
|
||||
other routines. */
|
||||
|
||||
struct backtrace_state *
|
||||
backtrace_create_state (const char *filename, int threaded,
|
||||
backtrace_error_callback error_callback,
|
||||
void *data)
|
||||
{
|
||||
struct backtrace_state init_state;
|
||||
struct backtrace_state *state;
|
||||
|
||||
#ifndef HAVE_SYNC_FUNCTIONS
|
||||
if (threaded)
|
||||
{
|
||||
error_callback (data, "backtrace library does not support threads", 0);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
memset (&init_state, 0, sizeof init_state);
|
||||
init_state.filename = filename;
|
||||
init_state.threaded = threaded;
|
||||
|
||||
state = ((struct backtrace_state *)
|
||||
backtrace_alloc (&init_state, sizeof *state, error_callback, data));
|
||||
if (state == NULL)
|
||||
return NULL;
|
||||
*state = init_state;
|
||||
|
||||
return state;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLinuxBacktrace open source project
|
||||
//
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Backtrace
|
||||
#if canImport(Darwin)
|
||||
import Darwin
|
||||
#elseif os(Linux)
|
||||
import Glibc
|
||||
#endif
|
||||
|
||||
Backtrace.install()
|
||||
|
||||
func raiseSignal(_ signal: Int32) {
|
||||
raise(signal)
|
||||
}
|
||||
|
||||
let reason = CommandLine.arguments.count == 2 ? CommandLine.arguments[1] : "unknown"
|
||||
switch reason.uppercased() {
|
||||
case "SIGILL":
|
||||
raiseSignal(SIGILL)
|
||||
case "SIGSEGV":
|
||||
raiseSignal(SIGSEGV)
|
||||
case "SIGBUS":
|
||||
raiseSignal(SIGBUS)
|
||||
case "SIGFPE":
|
||||
raiseSignal(SIGFPE)
|
||||
default:
|
||||
fatalError(reason)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLinuxBacktrace open source project
|
||||
//
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// BacktraceTests+XCTest.swift
|
||||
//
|
||||
import XCTest
|
||||
|
||||
///
|
||||
/// NOTE: This file was generated by generate_linux_tests.rb
|
||||
///
|
||||
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
|
||||
///
|
||||
|
||||
extension BacktraceTests {
|
||||
public static var allTests: [(String, (BacktraceTests) -> () throws -> Void)] {
|
||||
return [
|
||||
("testFatalError", testFatalError),
|
||||
("testSIGILL", testSIGILL),
|
||||
("testSIGSEGV", testSIGSEGV),
|
||||
("testSIGBUS", testSIGBUS),
|
||||
("testSIGFPE", testSIGFPE),
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,90 @@
|
||||
import XCTest
|
||||
@testable import Backtrace
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLinuxBacktrace open source project
|
||||
//
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
final class BacktraceTests: XCTestCase {
|
||||
func testExample() {
|
||||
// This is an example of a functional test case.
|
||||
// Use XCTAssert and related functions to verify your tests produce the correct
|
||||
// results.
|
||||
XCTAssert(true)
|
||||
import XCTest
|
||||
|
||||
public final class BacktraceTests: XCTestCase {
|
||||
func testFatalError() throws {
|
||||
#if !os(Linux)
|
||||
try XCTSkipIf(true, "test is only supported on Linux")
|
||||
#endif
|
||||
|
||||
let expectedError = UUID().uuidString
|
||||
let stderr = try runSample(reason: expectedError)
|
||||
print(stderr)
|
||||
|
||||
XCTAssert(stderr.contains("Received signal 4. Backtrace:"))
|
||||
XCTAssert(stderr.contains("Current stack trace:"), "expected stanard error to include backtrace")
|
||||
XCTAssert(stderr.contains("Fatal error: \(expectedError)"), "expected stanard error to include error information")
|
||||
}
|
||||
|
||||
static var allTests = [
|
||||
("testExample", testExample),
|
||||
]
|
||||
func testSIGILL() throws {
|
||||
#if !os(Linux)
|
||||
try XCTSkipIf(true, "test is only supported on Linux")
|
||||
#endif
|
||||
|
||||
let stderr = try runSample(reason: "SIGILL")
|
||||
print(stderr)
|
||||
|
||||
XCTAssert(stderr.contains("Received signal \(SIGILL). Backtrace:"))
|
||||
XCTAssert(stderr.contains("Sample.raiseSignal"))
|
||||
}
|
||||
|
||||
func testSIGSEGV() throws {
|
||||
#if !os(Linux)
|
||||
try XCTSkipIf(true, "test is only supported on Linux")
|
||||
#endif
|
||||
|
||||
let stderr = try runSample(reason: "SIGSEGV")
|
||||
print(stderr)
|
||||
|
||||
XCTAssert(stderr.contains("Received signal \(SIGSEGV). Backtrace:"))
|
||||
XCTAssert(stderr.contains("Sample.raiseSignal"))
|
||||
}
|
||||
|
||||
func testSIGBUS() throws {
|
||||
#if !os(Linux)
|
||||
try XCTSkipIf(true, "test is only supported on Linux")
|
||||
#endif
|
||||
|
||||
let stderr = try runSample(reason: "SIGBUS")
|
||||
print(stderr)
|
||||
|
||||
XCTAssert(stderr.contains("Received signal \(SIGBUS). Backtrace:"))
|
||||
XCTAssert(stderr.contains("Sample.raiseSignal"))
|
||||
}
|
||||
|
||||
func testSIGFPE() throws {
|
||||
#if !os(Linux)
|
||||
try XCTSkipIf(true, "test is only supported on Linux")
|
||||
#endif
|
||||
|
||||
let stderr = try runSample(reason: "SIGFPE")
|
||||
print(stderr)
|
||||
|
||||
XCTAssert(stderr.contains("Received signal \(SIGFPE). Backtrace:"))
|
||||
XCTAssert(stderr.contains("Sample.raiseSignal"))
|
||||
}
|
||||
|
||||
func runSample(reason: String) throws -> String {
|
||||
let pipe = Pipe()
|
||||
let process = Process()
|
||||
process.executableURL = URL(fileURLWithPath: "/usr/bin/swift")
|
||||
process.arguments = ["run", "Sample", reason]
|
||||
process.standardError = pipe
|
||||
try process.run()
|
||||
process.waitUntilExit()
|
||||
return String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import XCTest
|
||||
|
||||
#if !canImport(ObjectiveC)
|
||||
public func allTests() -> [XCTestCaseEntry] {
|
||||
return [
|
||||
testCase(BacktraceTests.allTests),
|
||||
]
|
||||
}
|
||||
#endif
|
||||
+27
-3
@@ -1,7 +1,31 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLinuxBacktrace open source project
|
||||
//
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// LinuxMain.swift
|
||||
//
|
||||
import XCTest
|
||||
|
||||
///
|
||||
/// NOTE: This file was generated by generate_linux_tests.rb
|
||||
///
|
||||
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
|
||||
///
|
||||
|
||||
#if os(Linux) || os(FreeBSD)
|
||||
import BacktraceTests
|
||||
|
||||
var tests = [XCTestCaseEntry]()
|
||||
tests += BacktraceTests.allTests()
|
||||
XCTMain(tests)
|
||||
XCTMain([
|
||||
testCase(BacktraceTests.allTests),
|
||||
])
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
ARG swift_version=5.0
|
||||
ARG ubuntu_version=bionic
|
||||
ARG base_image=swift:$swift_version-$ubuntu_version
|
||||
FROM $base_image
|
||||
# needed to do again after FROM due to docker limitation
|
||||
ARG swift_version
|
||||
ARG ubuntu_version
|
||||
|
||||
# set as UTF-8
|
||||
RUN apt-get update && apt-get install -y locales locales-all
|
||||
ENV LC_ALL en_US.UTF-8
|
||||
ENV LANG en_US.UTF-8
|
||||
ENV LANGUAGE en_US.UTF-8
|
||||
|
||||
# dependencies
|
||||
RUN apt-get update && apt-get install -y wget
|
||||
RUN apt-get update && apt-get install -y lsof dnsutils netcat-openbsd net-tools curl jq # used by integration tests
|
||||
|
||||
# ruby and jazzy for docs generation
|
||||
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev build-essential
|
||||
# jazzy no longer works on older version of ubuntu as ruby is too old.
|
||||
RUN if [ "${ubuntu_version}" = "focal" ] ; then echo "gem: --no-document" > ~/.gemrc ; fi
|
||||
RUN if [ "${ubuntu_version}" = "focal" ] ; then gem install jazzy ; fi
|
||||
|
||||
# tools
|
||||
RUN mkdir -p $HOME/.tools
|
||||
RUN echo 'export PATH="$HOME/.tools:$PATH"' >> $HOME/.profile
|
||||
|
||||
# swiftformat (until part of the toolchain)
|
||||
|
||||
ARG swiftformat_version=0.44.6
|
||||
RUN git clone --branch $swiftformat_version --depth 1 https://github.com/nicklockwood/SwiftFormat $HOME/.tools/swift-format
|
||||
RUN cd $HOME/.tools/swift-format && swift build -c release
|
||||
RUN ln -s $HOME/.tools/swift-format/.build/release/swiftformat $HOME/.tools/swiftformat
|
||||
@@ -0,0 +1,18 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:16.04-5.1
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "xenial"
|
||||
swift_version: "5.1"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:16.04-5.1
|
||||
environment:
|
||||
- SANITIZER_ARG=--sanitize=thread
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:16.04-5.1
|
||||
@@ -0,0 +1,16 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:18.04-5.0
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "bionic"
|
||||
swift_version: "5.0"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:18.04-5.0
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:18.04-5.0
|
||||
@@ -0,0 +1,16 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:18.04-5.2
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "bionic"
|
||||
swift_version: "5.2"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:18.04-5.2
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:18.04-5.2
|
||||
@@ -0,0 +1,16 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:18.04-5.3
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "bionic"
|
||||
swift_version: "5.3"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:18.04-5.3
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:18.04-5.3
|
||||
@@ -0,0 +1,16 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:20.04-5.4
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "focal"
|
||||
swift_version: "5.4"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:20.04-5.4
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:20.04-5.4
|
||||
@@ -0,0 +1,16 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:20.04-5.5
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "focal"
|
||||
swift_version: "5.5"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:20.04-5.5
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:20.04-5.5
|
||||
@@ -0,0 +1,16 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:20.04-5.6
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "focal"
|
||||
swift_version: "5.6"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:20.04-5.6
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:20.04-5.6
|
||||
@@ -0,0 +1,15 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:20.04-5.7
|
||||
build:
|
||||
args:
|
||||
base_image: "swiftlang/swift:nightly-main-focal"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:20.04-5.7
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:20.04-5.7
|
||||
@@ -0,0 +1,15 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:20.04-main
|
||||
build:
|
||||
args:
|
||||
base_image: "swiftlang/swift:nightly-main-focal"
|
||||
|
||||
test:
|
||||
image: swift-linux-backtrace:20.04-main
|
||||
|
||||
shell:
|
||||
image: swift-linux-backtrace:20.04-main
|
||||
@@ -0,0 +1,37 @@
|
||||
# this file is not designed to be run directly
|
||||
# instead, use the docker-compose.<os>.<swift> files
|
||||
# eg docker-compose -f docker/docker-compose.yaml -f docker/docker-compose.1804.50.yaml run test
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-linux-backtrace:default
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
|
||||
common: &common
|
||||
image: swift-linux-backtrace:default
|
||||
depends_on: [runtime-setup]
|
||||
volumes:
|
||||
- ~/.ssh:/root/.ssh
|
||||
- ..:/code:z
|
||||
working_dir: /code
|
||||
cap_drop:
|
||||
- CAP_NET_RAW
|
||||
- CAP_NET_BIND_SERVICE
|
||||
|
||||
sanity:
|
||||
<<: *common
|
||||
command: /bin/bash -cl "./scripts/sanity.sh"
|
||||
|
||||
test:
|
||||
<<: *common
|
||||
command: /bin/bash -cl "swift test -c release -Xswiftc -g -Xswiftc -warnings-as-errors $${SANITIZER_ARG-}"
|
||||
|
||||
# util
|
||||
|
||||
shell:
|
||||
<<: *common
|
||||
entrypoint: /bin/bash -l
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
##===----------------------------------------------------------------------===##
|
||||
##
|
||||
## This source file is part of the SwiftLinuxBacktrace open source project
|
||||
##
|
||||
## Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
## Licensed under Apache License v2.0
|
||||
##
|
||||
## See LICENSE.txt for license information
|
||||
## See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
##
|
||||
## SPDX-License-Identifier: Apache-2.0
|
||||
##
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
set -eu
|
||||
here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
contributors=$( cd "$here"/.. && git shortlog -es | cut -f2 | sed 's/^/- /' )
|
||||
|
||||
cat > "$here/../CONTRIBUTORS.txt" <<- EOF
|
||||
For the purpose of tracking copyright, this is the list of individuals and
|
||||
organizations who have contributed source code to SwiftLinuxBacktrace.
|
||||
|
||||
For employees of an organization/company where the copyright of work done
|
||||
by employees of that company is held by the company itself, only the company
|
||||
needs to be listed here.
|
||||
|
||||
## COPYRIGHT HOLDERS
|
||||
|
||||
- Apple Inc. (all contributors with '@apple.com')
|
||||
|
||||
### Contributors
|
||||
|
||||
$contributors
|
||||
|
||||
**Updating this list**
|
||||
|
||||
Please do not edit this file manually. It is generated using \`./scripts/generate_contributors_list.sh\`. If a name is misspelled or appearing multiple times: add an entry in \`./.mailmap\`
|
||||
EOF
|
||||
Executable
+231
@@ -0,0 +1,231 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
#
|
||||
# process_test_files.rb
|
||||
#
|
||||
# Copyright 2016 Tony Stone
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# Created by Tony Stone on 5/4/16.
|
||||
#
|
||||
require 'getoptlong'
|
||||
require 'fileutils'
|
||||
require 'pathname'
|
||||
|
||||
include FileUtils
|
||||
|
||||
#
|
||||
# This ruby script will auto generate LinuxMain.swift and the +XCTest.swift extension files for Swift Package Manager on Linux platforms.
|
||||
#
|
||||
# See https://github.com/apple/swift-corelibs-xctest/blob/master/Documentation/Linux.md
|
||||
#
|
||||
def header(fileName)
|
||||
string = <<-eos
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLinuxBacktrace open source project
|
||||
//
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// <FileName>
|
||||
//
|
||||
import XCTest
|
||||
|
||||
///
|
||||
/// NOTE: This file was generated by generate_linux_tests.rb
|
||||
///
|
||||
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
|
||||
///
|
||||
eos
|
||||
|
||||
string
|
||||
.sub('<FileName>', File.basename(fileName))
|
||||
.sub('<Date>', Time.now.to_s)
|
||||
end
|
||||
|
||||
def createExtensionFile(fileName, classes)
|
||||
extensionFile = fileName.sub! '.swift', '+XCTest.swift'
|
||||
print 'Creating file: ' + extensionFile + "\n"
|
||||
|
||||
File.open(extensionFile, 'w') do |file|
|
||||
file.write header(extensionFile)
|
||||
file.write "\n"
|
||||
|
||||
for classArray in classes
|
||||
file.write 'extension ' + classArray[0] + " {\n"
|
||||
file.write ' public static var allTests: [(String, (' + classArray[0] + ") -> () throws -> Void)] {\n"
|
||||
file.write " return [\n"
|
||||
|
||||
for funcName in classArray[1]
|
||||
file.write ' ("' + funcName + '", ' + funcName + "),\n"
|
||||
end
|
||||
|
||||
file.write " ]\n"
|
||||
file.write " }\n"
|
||||
file.write "}\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def createLinuxMain(testsDirectory, allTestSubDirectories, files)
|
||||
fileName = testsDirectory + '/LinuxMain.swift'
|
||||
print 'Creating file: ' + fileName + "\n"
|
||||
|
||||
File.open(fileName, 'w') do |file|
|
||||
file.write header(fileName)
|
||||
file.write "\n"
|
||||
|
||||
file.write "#if os(Linux) || os(FreeBSD)\n"
|
||||
for testSubDirectory in allTestSubDirectories.sort { |x, y| x <=> y }
|
||||
file.write 'import ' + testSubDirectory + "\n"
|
||||
end
|
||||
file.write "\n"
|
||||
file.write "XCTMain([\n"
|
||||
|
||||
testCases = []
|
||||
for classes in files
|
||||
for classArray in classes
|
||||
testCases << classArray[0]
|
||||
end
|
||||
end
|
||||
|
||||
for testCase in testCases.sort { |x, y| x <=> y }
|
||||
file.write ' testCase(' + testCase + ".allTests),\n"
|
||||
end
|
||||
file.write "])\n"
|
||||
file.write "#endif\n"
|
||||
end
|
||||
end
|
||||
|
||||
def parseSourceFile(fileName)
|
||||
puts 'Parsing file: ' + fileName + "\n"
|
||||
|
||||
classes = []
|
||||
currentClass = nil
|
||||
inIfLinux = false
|
||||
inElse = false
|
||||
ignore = false
|
||||
|
||||
#
|
||||
# Read the file line by line
|
||||
# and parse to find the class
|
||||
# names and func names
|
||||
#
|
||||
File.readlines(fileName).each do |line|
|
||||
if inIfLinux
|
||||
if /\#else/.match(line)
|
||||
inElse = true
|
||||
ignore = true
|
||||
else
|
||||
if /\#end/.match(line)
|
||||
inElse = false
|
||||
inIfLinux = false
|
||||
ignore = false
|
||||
end
|
||||
end
|
||||
else
|
||||
if /\#if[ \t]+os\(Linux\)/.match(line)
|
||||
inIfLinux = true
|
||||
ignore = false
|
||||
end
|
||||
end
|
||||
|
||||
next if ignore
|
||||
# Match class or func
|
||||
match = line[/class[ \t]+[a-zA-Z0-9_]*(?=[ \t]*:[ \t]*XCTestCase)|func[ \t]+test[a-zA-Z0-9_]*(?=[ \t]*\(\))/, 0]
|
||||
if match
|
||||
|
||||
if match[/class/, 0] == 'class'
|
||||
className = match.sub(/^class[ \t]+/, '')
|
||||
#
|
||||
# Create a new class / func structure
|
||||
# and add it to the classes array.
|
||||
#
|
||||
currentClass = [className, []]
|
||||
classes << currentClass
|
||||
else # Must be a func
|
||||
funcName = match.sub(/^func[ \t]+/, '')
|
||||
#
|
||||
# Add each func name the the class / func
|
||||
# structure created above.
|
||||
#
|
||||
currentClass[1] << funcName
|
||||
end
|
||||
end
|
||||
end
|
||||
classes
|
||||
end
|
||||
|
||||
#
|
||||
# Main routine
|
||||
#
|
||||
#
|
||||
|
||||
testsDirectory = 'Tests'
|
||||
|
||||
options = GetoptLong.new(['--tests-dir', GetoptLong::OPTIONAL_ARGUMENT])
|
||||
options.quiet = true
|
||||
|
||||
begin
|
||||
options.each do |option, value|
|
||||
case option
|
||||
when '--tests-dir'
|
||||
testsDirectory = value
|
||||
end
|
||||
end
|
||||
rescue GetoptLong::InvalidOption
|
||||
end
|
||||
|
||||
allTestSubDirectories = []
|
||||
allFiles = []
|
||||
|
||||
Dir[testsDirectory + '/*'].each do |subDirectory|
|
||||
next unless File.directory?(subDirectory)
|
||||
directoryHasClasses = false
|
||||
Dir[subDirectory + '/*Test{s,}.swift'].each do |fileName|
|
||||
next unless File.file? fileName
|
||||
fileClasses = parseSourceFile(fileName)
|
||||
|
||||
#
|
||||
# If there are classes in the
|
||||
# test source file, create an extension
|
||||
# file for it.
|
||||
#
|
||||
next unless fileClasses.count > 0
|
||||
createExtensionFile(fileName, fileClasses)
|
||||
directoryHasClasses = true
|
||||
allFiles << fileClasses
|
||||
end
|
||||
|
||||
if directoryHasClasses
|
||||
allTestSubDirectories << Pathname.new(subDirectory).split.last.to_s
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Last step is the create a LinuxMain.swift file that
|
||||
# references all the classes and funcs in the source files.
|
||||
#
|
||||
if allFiles.count > 0
|
||||
createLinuxMain(testsDirectory, allTestSubDirectories, allFiles)
|
||||
end
|
||||
# eof
|
||||
Executable
+140
@@ -0,0 +1,140 @@
|
||||
#!/bin/bash
|
||||
##===----------------------------------------------------------------------===##
|
||||
##
|
||||
## This source file is part of the SwiftLinuxBacktrace open source project
|
||||
##
|
||||
## Copyright (c) 2017-2018 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
## Licensed under Apache License v2.0
|
||||
##
|
||||
## See LICENSE.txt for license information
|
||||
## See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
##
|
||||
## SPDX-License-Identifier: Apache-2.0
|
||||
##
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
set -eu
|
||||
|
||||
here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
function replace_acceptable_years() {
|
||||
# this needs to replace all acceptable forms with 'YEARS'
|
||||
sed -e 's/2017-2018/YEARS/' -e 's/2019-2020/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/'
|
||||
}
|
||||
|
||||
printf "=> Checking linux tests... "
|
||||
FIRST_OUT="$(git status --porcelain)"
|
||||
ruby "$here/../scripts/generate_linux_tests.rb" > /dev/null
|
||||
SECOND_OUT="$(git status --porcelain)"
|
||||
if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then
|
||||
printf "\033[0;31mmissing changes!\033[0m\n"
|
||||
git --no-pager diff
|
||||
exit 1
|
||||
else
|
||||
printf "\033[0;32mokay.\033[0m\n"
|
||||
fi
|
||||
|
||||
printf "=> Checking format... "
|
||||
FIRST_OUT="$(git status --porcelain)"
|
||||
swiftformat . > /dev/null 2>&1
|
||||
SECOND_OUT="$(git status --porcelain)"
|
||||
if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then
|
||||
printf "\033[0;31mformatting issues!\033[0m\n"
|
||||
git --no-pager diff
|
||||
exit 1
|
||||
else
|
||||
printf "\033[0;32mokay.\033[0m\n"
|
||||
fi
|
||||
|
||||
printf "=> Checking license headers\n"
|
||||
tmp=$(mktemp /tmp/.swift-aws-lambda-sanity_XXXXXX)
|
||||
|
||||
for language in swift-or-c bash dtrace; do
|
||||
printf " * $language... "
|
||||
declare -a matching_files
|
||||
declare -a exceptions
|
||||
expections=( )
|
||||
matching_files=( -name '*' )
|
||||
case "$language" in
|
||||
swift-or-c)
|
||||
exceptions=( -name Package.swift -o -path './Sources/CBacktrace/*' )
|
||||
matching_files=( -name '*.swift' -o -name '*.c' -o -name '*.h' )
|
||||
cat > "$tmp" <<"EOF"
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLinuxBacktrace open source project
|
||||
//
|
||||
// Copyright (c) YEARS Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
EOF
|
||||
;;
|
||||
bash)
|
||||
matching_files=( -name '*.sh' )
|
||||
cat > "$tmp" <<"EOF"
|
||||
#!/bin/bash
|
||||
##===----------------------------------------------------------------------===##
|
||||
##
|
||||
## This source file is part of the SwiftLinuxBacktrace open source project
|
||||
##
|
||||
## Copyright (c) YEARS Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
## Licensed under Apache License v2.0
|
||||
##
|
||||
## See LICENSE.txt for license information
|
||||
## See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
##
|
||||
## SPDX-License-Identifier: Apache-2.0
|
||||
##
|
||||
##===----------------------------------------------------------------------===##
|
||||
EOF
|
||||
;;
|
||||
dtrace)
|
||||
matching_files=( -name '*.d' )
|
||||
cat > "$tmp" <<"EOF"
|
||||
#!/usr/sbin/dtrace -q -s
|
||||
/*===----------------------------------------------------------------------===*
|
||||
*
|
||||
* This source file is part of the SwiftLinuxBacktrace open source project
|
||||
*
|
||||
* Copyright (c) YEARS Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
* Licensed under Apache License v2.0
|
||||
*
|
||||
* See LICENSE.txt for license information
|
||||
* See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
*===----------------------------------------------------------------------===*/
|
||||
EOF
|
||||
;;
|
||||
*)
|
||||
echo >&2 "ERROR: unknown language '$language'"
|
||||
;;
|
||||
esac
|
||||
|
||||
expected_lines=$(cat "$tmp" | wc -l)
|
||||
expected_sha=$(cat "$tmp" | shasum)
|
||||
|
||||
(
|
||||
cd "$here/.."
|
||||
find . \
|
||||
\( \! -path './.build/*' -a \
|
||||
\( "${matching_files[@]}" \) -a \
|
||||
\( \! \( "${exceptions[@]}" \) \) \) | while read line; do
|
||||
if [[ "$(cat "$line" | replace_acceptable_years | head -n $expected_lines | shasum)" != "$expected_sha" ]]; then
|
||||
printf "\033[0;31mmissing headers in file '$line'!\033[0m\n"
|
||||
diff -u <(cat "$line" | replace_acceptable_years | head -n $expected_lines) "$tmp"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
printf "\033[0;32mokay.\033[0m\n"
|
||||
)
|
||||
done
|
||||
|
||||
rm "$tmp"
|
||||
Executable
+93
@@ -0,0 +1,93 @@
|
||||
#!/bin/bash
|
||||
##===----------------------------------------------------------------------===##
|
||||
##
|
||||
## This source file is part of the SwiftLinuxBacktrace open source project
|
||||
##
|
||||
## Copyright (c) 2019-2020 Apple Inc. and the SwiftLinuxBacktrace project authors
|
||||
## Licensed under Apache License v2.0
|
||||
##
|
||||
## See LICENSE.txt for license information
|
||||
## See CONTRIBUTORS.txt for the list of SwiftLinuxBacktrace project authors
|
||||
##
|
||||
## SPDX-License-Identifier: Apache-2.0
|
||||
##
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
# This script creates a vendored copy of libbacktrace that is
|
||||
# suitable for building with the Swift Package Manager.
|
||||
#
|
||||
# Usage:
|
||||
# 1. Run this script in the package root. It will place
|
||||
# a local copy of the libbacktrace sources in Sources/CBacktrace.
|
||||
# Any prior contents of Sources/CBacktrace will be deleted.
|
||||
|
||||
set -eou pipefail
|
||||
|
||||
if [ "$(uname -s)" != "Linux" ]
|
||||
then
|
||||
echo "This script may only be run on Linux"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
HERE=$(pwd)
|
||||
DSTROOT=Sources/CBacktrace
|
||||
TMPDIR=$(mktemp -d /tmp/.workingXXXXXX)
|
||||
SRCROOT="${TMPDIR}/src/libbacktrace"
|
||||
|
||||
echo "REMOVING any previously-vendored libbacktrace code"
|
||||
rm -rf "${DSTROOT:?}/"*
|
||||
|
||||
echo "CLONING libbacktrace"
|
||||
mkdir -p "$SRCROOT"
|
||||
git clone https://github.com/ianlancetaylor/libbacktrace.git "$SRCROOT"
|
||||
echo "CLONED libbacktrace"
|
||||
|
||||
echo "CONFIGURING libbacktrace"
|
||||
cd "$SRCROOT"
|
||||
./configure > configure_log.txt 2>&1
|
||||
cd "$HERE"
|
||||
|
||||
PATTERNS=(
|
||||
'*.c'
|
||||
'*.h'
|
||||
'LICENSE'
|
||||
)
|
||||
|
||||
for pattern in "${PATTERNS[@]}"
|
||||
do
|
||||
echo "COPYING $pattern"
|
||||
cp "$SRCROOT"/$pattern "$DSTROOT"
|
||||
done
|
||||
|
||||
EXCLUDES=(
|
||||
'*test*'
|
||||
'nounwind.c'
|
||||
'pecoff.c'
|
||||
'read.c'
|
||||
'unknown.c'
|
||||
'xcoff.c'
|
||||
'macho.c'
|
||||
'alloc.c'
|
||||
'allocfail.c'
|
||||
'instrumented_alloc.c'
|
||||
)
|
||||
|
||||
for exclude in "${EXCLUDES[@]}"
|
||||
do
|
||||
echo "EXCLUDING $exclude"
|
||||
find $DSTROOT -name "$exclude" -exec rm -rf {} \;
|
||||
done
|
||||
|
||||
echo "MOVING backtrace.h"
|
||||
mkdir -p "$DSTROOT/include"
|
||||
mv "$DSTROOT/backtrace.h" "$DSTROOT/include"
|
||||
|
||||
echo "REPLACING references to \"backtrace.h\" with \"include/backtrace.h\""
|
||||
find $DSTROOT -name "*.[ch]" -print0 | xargs -0 sed -i -e 's#"backtrace.h"#"include/backtrace.h"#g'
|
||||
|
||||
echo "ADDING preprocessor conditionals"
|
||||
find $DSTROOT -name "*.[ch]" -print0 | xargs -0 sed -i -e '1i#ifdef __linux__' -e '$a#endif'
|
||||
|
||||
echo "DONE"
|
||||
Reference in New Issue
Block a user