@@ -20,6 +20,43 @@ import (
20
20
"testing"
21
21
)
22
22
23
+ func canGenerateCore (t * testing.T ) bool {
24
+ // Ensure there is enough RLIMIT_CORE available to generate a full core.
25
+ var lim syscall.Rlimit
26
+ err := syscall .Getrlimit (syscall .RLIMIT_CORE , & lim )
27
+ if err != nil {
28
+ t .Fatalf ("error getting rlimit: %v" , err )
29
+ }
30
+ // Minimum RLIMIT_CORE max to allow. This is a conservative estimate.
31
+ // Most systems allow infinity.
32
+ const minRlimitCore = 100 << 20 // 100 MB
33
+ if lim .Max < minRlimitCore {
34
+ t .Skipf ("RLIMIT_CORE max too low: %#+v" , lim )
35
+ }
36
+
37
+ // Make sure core pattern will send core to the current directory.
38
+ b , err := os .ReadFile ("/proc/sys/kernel/core_pattern" )
39
+ if err != nil {
40
+ t .Fatalf ("error reading core_pattern: %v" , err )
41
+ }
42
+ if string (b ) != "core\n " {
43
+ t .Skipf ("Unexpected core pattern %q" , string (b ))
44
+ }
45
+
46
+ coreUsesPID := false
47
+ b , err = os .ReadFile ("/proc/sys/kernel/core_uses_pid" )
48
+ if err == nil {
49
+ switch string (bytes .TrimSpace (b )) {
50
+ case "0" :
51
+ case "1" :
52
+ coreUsesPID = true
53
+ default :
54
+ t .Skipf ("unexpected core_uses_pid value %q" , string (b ))
55
+ }
56
+ }
57
+ return coreUsesPID
58
+ }
59
+
23
60
const coreSignalSource = `
24
61
package main
25
62
@@ -81,45 +118,12 @@ func TestGdbCoreSignalBacktrace(t *testing.T) {
81
118
t .Parallel ()
82
119
checkGdbVersion (t )
83
120
84
- // Ensure there is enough RLIMIT_CORE available to generate a full core.
85
- var lim syscall.Rlimit
86
- err := syscall .Getrlimit (syscall .RLIMIT_CORE , & lim )
87
- if err != nil {
88
- t .Fatalf ("error getting rlimit: %v" , err )
89
- }
90
- // Minimum RLIMIT_CORE max to allow. This is a conservative estimate.
91
- // Most systems allow infinity.
92
- const minRlimitCore = 100 << 20 // 100 MB
93
- if lim .Max < minRlimitCore {
94
- t .Skipf ("RLIMIT_CORE max too low: %#+v" , lim )
95
- }
96
-
97
- // Make sure core pattern will send core to the current directory.
98
- b , err := os .ReadFile ("/proc/sys/kernel/core_pattern" )
99
- if err != nil {
100
- t .Fatalf ("error reading core_pattern: %v" , err )
101
- }
102
- if string (b ) != "core\n " {
103
- t .Skipf ("Unexpected core pattern %q" , string (b ))
104
- }
105
-
106
- coreUsesPID := false
107
- b , err = os .ReadFile ("/proc/sys/kernel/core_uses_pid" )
108
- if err == nil {
109
- switch string (bytes .TrimSpace (b )) {
110
- case "0" :
111
- case "1" :
112
- coreUsesPID = true
113
- default :
114
- t .Skipf ("unexpected core_uses_pid value %q" , string (b ))
115
- }
116
- }
117
-
118
- dir := t .TempDir ()
121
+ coreUsesPID := canGenerateCore (t )
119
122
120
123
// Build the source code.
124
+ dir := t .TempDir ()
121
125
src := filepath .Join (dir , "main.go" )
122
- err = os .WriteFile (src , []byte (coreSignalSource ), 0644 )
126
+ err : = os .WriteFile (src , []byte (coreSignalSource ), 0644 )
123
127
if err != nil {
124
128
t .Fatalf ("failed to create file: %v" , err )
125
129
}
@@ -296,45 +300,12 @@ func TestGdbCoreCrashThreadBacktrace(t *testing.T) {
296
300
t .Parallel ()
297
301
checkGdbVersion (t )
298
302
299
- // Ensure there is enough RLIMIT_CORE available to generate a full core.
300
- var lim syscall.Rlimit
301
- err := syscall .Getrlimit (syscall .RLIMIT_CORE , & lim )
302
- if err != nil {
303
- t .Fatalf ("error getting rlimit: %v" , err )
304
- }
305
- // Minimum RLIMIT_CORE max to allow. This is a conservative estimate.
306
- // Most systems allow infinity.
307
- const minRlimitCore = 100 << 20 // 100 MB
308
- if lim .Max < minRlimitCore {
309
- t .Skipf ("RLIMIT_CORE max too low: %#+v" , lim )
310
- }
311
-
312
- // Make sure core pattern will send core to the current directory.
313
- b , err := os .ReadFile ("/proc/sys/kernel/core_pattern" )
314
- if err != nil {
315
- t .Fatalf ("error reading core_pattern: %v" , err )
316
- }
317
- if string (b ) != "core\n " {
318
- t .Skipf ("Unexpected core pattern %q" , string (b ))
319
- }
320
-
321
- coreUsesPID := false
322
- b , err = os .ReadFile ("/proc/sys/kernel/core_uses_pid" )
323
- if err == nil {
324
- switch string (bytes .TrimSpace (b )) {
325
- case "0" :
326
- case "1" :
327
- coreUsesPID = true
328
- default :
329
- t .Skipf ("unexpected core_uses_pid value %q" , string (b ))
330
- }
331
- }
332
-
333
- dir := t .TempDir ()
303
+ coreUsesPID := canGenerateCore (t )
334
304
335
305
// Build the source code.
306
+ dir := t .TempDir ()
336
307
src := filepath .Join (dir , "main.go" )
337
- err = os .WriteFile (src , []byte (coreCrashThreadSource ), 0644 )
308
+ err : = os .WriteFile (src , []byte (coreCrashThreadSource ), 0644 )
338
309
if err != nil {
339
310
t .Fatalf ("failed to create file: %v" , err )
340
311
}
0 commit comments