From 2a9f9e22d315dc049ce35dc67d20de2f67b6d4a0 Mon Sep 17 00:00:00 2001 From: zzkcode Date: Thu, 30 Nov 2023 00:03:43 +0800 Subject: [PATCH] runtime: extract canGenerateCore --- src/runtime/runtime-gdb_unix_test.go | 115 ++++++++++----------------- 1 file changed, 43 insertions(+), 72 deletions(-) diff --git a/src/runtime/runtime-gdb_unix_test.go b/src/runtime/runtime-gdb_unix_test.go index 910a93d6288..2dc1e31e13f 100644 --- a/src/runtime/runtime-gdb_unix_test.go +++ b/src/runtime/runtime-gdb_unix_test.go @@ -20,6 +20,43 @@ import ( "testing" ) +func canGenerateCore(t *testing.T) bool { + // Ensure there is enough RLIMIT_CORE available to generate a full core. + var lim syscall.Rlimit + err := syscall.Getrlimit(syscall.RLIMIT_CORE, &lim) + if err != nil { + t.Fatalf("error getting rlimit: %v", err) + } + // Minimum RLIMIT_CORE max to allow. This is a conservative estimate. + // Most systems allow infinity. + const minRlimitCore = 100 << 20 // 100 MB + if lim.Max < minRlimitCore { + t.Skipf("RLIMIT_CORE max too low: %#+v", lim) + } + + // Make sure core pattern will send core to the current directory. + b, err := os.ReadFile("/proc/sys/kernel/core_pattern") + if err != nil { + t.Fatalf("error reading core_pattern: %v", err) + } + if string(b) != "core\n" { + t.Skipf("Unexpected core pattern %q", string(b)) + } + + coreUsesPID := false + b, err = os.ReadFile("/proc/sys/kernel/core_uses_pid") + if err == nil { + switch string(bytes.TrimSpace(b)) { + case "0": + case "1": + coreUsesPID = true + default: + t.Skipf("unexpected core_uses_pid value %q", string(b)) + } + } + return coreUsesPID +} + const coreSignalSource = ` package main @@ -81,45 +118,12 @@ func TestGdbCoreSignalBacktrace(t *testing.T) { t.Parallel() checkGdbVersion(t) - // Ensure there is enough RLIMIT_CORE available to generate a full core. - var lim syscall.Rlimit - err := syscall.Getrlimit(syscall.RLIMIT_CORE, &lim) - if err != nil { - t.Fatalf("error getting rlimit: %v", err) - } - // Minimum RLIMIT_CORE max to allow. This is a conservative estimate. - // Most systems allow infinity. - const minRlimitCore = 100 << 20 // 100 MB - if lim.Max < minRlimitCore { - t.Skipf("RLIMIT_CORE max too low: %#+v", lim) - } - - // Make sure core pattern will send core to the current directory. - b, err := os.ReadFile("/proc/sys/kernel/core_pattern") - if err != nil { - t.Fatalf("error reading core_pattern: %v", err) - } - if string(b) != "core\n" { - t.Skipf("Unexpected core pattern %q", string(b)) - } - - coreUsesPID := false - b, err = os.ReadFile("/proc/sys/kernel/core_uses_pid") - if err == nil { - switch string(bytes.TrimSpace(b)) { - case "0": - case "1": - coreUsesPID = true - default: - t.Skipf("unexpected core_uses_pid value %q", string(b)) - } - } - - dir := t.TempDir() + coreUsesPID := canGenerateCore(t) // Build the source code. + dir := t.TempDir() src := filepath.Join(dir, "main.go") - err = os.WriteFile(src, []byte(coreSignalSource), 0644) + err := os.WriteFile(src, []byte(coreSignalSource), 0644) if err != nil { t.Fatalf("failed to create file: %v", err) } @@ -296,45 +300,12 @@ func TestGdbCoreCrashThreadBacktrace(t *testing.T) { t.Parallel() checkGdbVersion(t) - // Ensure there is enough RLIMIT_CORE available to generate a full core. - var lim syscall.Rlimit - err := syscall.Getrlimit(syscall.RLIMIT_CORE, &lim) - if err != nil { - t.Fatalf("error getting rlimit: %v", err) - } - // Minimum RLIMIT_CORE max to allow. This is a conservative estimate. - // Most systems allow infinity. - const minRlimitCore = 100 << 20 // 100 MB - if lim.Max < minRlimitCore { - t.Skipf("RLIMIT_CORE max too low: %#+v", lim) - } - - // Make sure core pattern will send core to the current directory. - b, err := os.ReadFile("/proc/sys/kernel/core_pattern") - if err != nil { - t.Fatalf("error reading core_pattern: %v", err) - } - if string(b) != "core\n" { - t.Skipf("Unexpected core pattern %q", string(b)) - } - - coreUsesPID := false - b, err = os.ReadFile("/proc/sys/kernel/core_uses_pid") - if err == nil { - switch string(bytes.TrimSpace(b)) { - case "0": - case "1": - coreUsesPID = true - default: - t.Skipf("unexpected core_uses_pid value %q", string(b)) - } - } - - dir := t.TempDir() + coreUsesPID := canGenerateCore(t) // Build the source code. + dir := t.TempDir() src := filepath.Join(dir, "main.go") - err = os.WriteFile(src, []byte(coreCrashThreadSource), 0644) + err := os.WriteFile(src, []byte(coreCrashThreadSource), 0644) if err != nil { t.Fatalf("failed to create file: %v", err) }