-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
debugutil,scpb: add debugutil package, adopt in scpb
This commit adds a debugutil package which exposes a flag which is set when the cockroach process is spawned by the delve debugger. This functionality is used by the scpb package to decorate data structures with useful but expensive debugging information on an as-needed basis. Release note: None
- Loading branch information
Marius Posta
committed
Jul 5, 2023
1 parent
dacd2f6
commit 6fe3f4b
Showing
5 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "debugutil", | ||
srcs = ["debugutil.go"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/util/debugutil", | ||
visibility = ["//visibility:public"], | ||
deps = ["@com_github_elastic_gosigar//:gosigar"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package debugutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sync/atomic" | ||
|
||
"github.com/elastic/gosigar" | ||
) | ||
|
||
// IsLaunchedByDebugger returns true in cases where the delve debugger | ||
// was used to launch the cockroach process. | ||
func IsLaunchedByDebugger() bool { | ||
return isLaunchedByDebugger.Load() | ||
} | ||
|
||
var isLaunchedByDebugger atomic.Bool | ||
|
||
func init() { | ||
isLaunchedByDebugger.Store(func(maybeDelvePID int) bool { | ||
// We loop in case there were intermediary processes like the gopls | ||
// language server. | ||
for maybeDelvePID != 0 { | ||
var exe gosigar.ProcExe | ||
if err := exe.Get(maybeDelvePID); err != nil { | ||
break | ||
} | ||
switch filepath.Base(exe.Name) { | ||
case "dlv": | ||
return true | ||
} | ||
var state gosigar.ProcState | ||
if err := state.Get(maybeDelvePID); err != nil { | ||
break | ||
} | ||
maybeDelvePID = state.Ppid | ||
} | ||
return false | ||
}(os.Getppid())) | ||
} |