Skip to content

Commit dd9543f

Browse files
authored
Merge pull request #85589 from carlpeto/eng/PR-164566321
Always show the crashed thread first
2 parents 53722a3 + ddc8c2b commit dd9543f

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

stdlib/public/libexec/swift-backtrace/main.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,11 @@ Generate a backtrace for the parent process.
894894
}
895895
}
896896

897+
dump(ndx: target.crashingThreadNdx, thread: crashingThread)
897898
if args.threads! {
898-
for (ndx, thread) in target.threads.enumerated() {
899+
for (ndx, thread) in target.threads.enumerated() where ndx != target.crashingThreadNdx {
899900
dump(ndx: ndx, thread: thread)
900901
}
901-
} else {
902-
dump(ndx: target.crashingThreadNdx, thread: crashingThread)
903902
}
904903

905904
if args.registers! == .crashedOnly {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -Onone -g -o %t/CrashWithThreads
3+
// RUN: %target-codesign %t/CrashWithThreads
4+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no,swift-backtrace=%backtracer %target-run %t/CrashWithThreads 2>&1 || true) | %FileCheck -vv %s -dump-input-filter=all
5+
6+
// UNSUPPORTED: use_os_stdlib
7+
// UNSUPPORTED: back_deployment_runtime
8+
// UNSUPPORTED: asan
9+
// REQUIRES: executable_test
10+
// REQUIRES: backtracing
11+
// REQUIRES: OS=macosx || OS=linux-gnu
12+
13+
#if canImport(Darwin)
14+
import Darwin
15+
#elseif canImport(Glibc)
16+
import Glibc
17+
#elseif canImport(Android)
18+
import Android
19+
#elseif os(Windows)
20+
import CRT
21+
#else
22+
#error("Unsupported platform")
23+
#endif
24+
25+
func reallyCrashMe() {
26+
print("I'm going to crash now")
27+
let ptr = UnsafeMutablePointer<Int>(bitPattern: 4)!
28+
ptr.pointee = 42
29+
}
30+
31+
func crashMe() {
32+
reallyCrashMe()
33+
}
34+
35+
func spawnThread(_ shouldCrash: Bool) {
36+
#if os(Linux)
37+
var thread: pthread_t = 0
38+
#elseif os(macOS)
39+
var thread = pthread_t(nil)
40+
#endif
41+
if shouldCrash {
42+
pthread_create(&thread, nil, { _ in
43+
crashMe()
44+
// this should not be run
45+
while (true) {
46+
sleep(10)
47+
}
48+
}, nil)
49+
} else {
50+
pthread_create(&thread, nil, { _ in
51+
while (true) {
52+
sleep(10)
53+
}
54+
}, nil)
55+
}
56+
}
57+
58+
let crashingThreadIndex = (1..<10).randomElement()
59+
60+
for threadIndex in 0..<10 {
61+
spawnThread(threadIndex == crashingThreadIndex)
62+
}
63+
64+
while (true) {
65+
sleep(10)
66+
}
67+
68+
// CHECK: *** Program crashed: Bad pointer dereference at 0x{{0+}}4 ***
69+
70+
// make sure there are no threads before the crashing thread (rdar://164566321)
71+
72+
// CHECK-NOT: Thread {{[0-9]+}}:
73+
74+
// CHECK: Thread {{[1-9]+}} {{(".*" )?}}crashed:
75+
76+
// CHECK: 0 0x{{[0-9a-f]+}} reallyCrashMe() + {{[0-9]+}} in CrashWithThreads at {{.*}}/CrashWithThreads
77+
// CHECK-NEXT: 1 [ra] 0x{{[0-9a-f]+}} crashMe() + {{[0-9]+}} in CrashWithThreads at {{.*}}/CrashWithThreads
78+
// CHECK-NEXT: 2 [ra] 0x{{[0-9a-f]+}} closure #{{[0-9]}} in spawnThread(_:) + {{[0-9]+}} in CrashWithThreads at {{.*}}/CrashWithThreads
79+
// CHECK-NEXT: 3 [ra] [thunk] 0x{{[0-9a-f]+}} @objc closure #{{[0-9]}} in spawnThread(_:) + {{[0-9]+}} in CrashWithThreads at {{.*}}<compiler-generated>
80+
81+
// CHECK: Registers:
82+
83+
// CHECK: Images ({{[0-9]+}} omitted):
84+
85+
// CHECK: {{0x[0-9a-f]+}}–{{0x[0-9a-f]+}}{{ +}}{{([0-9a-f]+|<no build ID>)}}{{ +}}CrashWithThreads{{ +}}{{.*}}/CrashWithThreads

0 commit comments

Comments
 (0)