Skip to content

Commit

Permalink
Merge #106169
Browse files Browse the repository at this point in the history
106169: debugutil,scpb: add debugutil package, adopt in scpb r=postamar a=postamar

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

Epic: CRDB-27601

Co-authored-by: Marius Posta <[email protected]>
  • Loading branch information
craig[bot] and Marius Posta committed Jul 5, 2023
2 parents 042cb00 + 6fe3f4b commit 0777400
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,7 @@ GO_TARGETS = [
"//pkg/util/ctxlog:ctxlog_test",
"//pkg/util/ctxutil:ctxutil",
"//pkg/util/ctxutil:ctxutil_test",
"//pkg/util/debugutil:debugutil",
"//pkg/util/duration:duration",
"//pkg/util/duration:duration_test",
"//pkg/util/encoding/csv:csv",
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/schemachanger/scpb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ go_library(
"//pkg/sql/privilege", # keep
"//pkg/sql/sem/catid", # keep
"//pkg/sql/sem/tree", # keep
"//pkg/util/debugutil",
"//pkg/util/protoutil",
"@com_github_cockroachdb_errors//:errors",
],
Expand Down
36 changes: 32 additions & 4 deletions pkg/sql/schemachanger/scpb/element_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@

package scpb

import "github.com/cockroachdb/errors"
import (
"github.com/cockroachdb/cockroach/pkg/util/debugutil"
"github.com/cockroachdb/errors"
)

// ElementCollection represents an ordered set of
// (current status, target status, element) tuples,
// looked up in `g` by iterating through `indexes`.
type ElementCollection[E Element] struct {
g ElementCollectionGetter
indexes []int

// debugInfo is only populated when using a debugger.
debugInfo []debugInfo
}

type debugInfo struct {
element Element
target TargetStatus
status Status
}

// ElementCollectionGetter abstracts the actual mechanism for obtaining
Expand All @@ -38,10 +50,26 @@ func NewElementCollection(g ElementCollectionGetter, indexes []int) *ElementColl
if g == nil || len(indexes) == 0 {
return nil
}
return &ElementCollection[Element]{
ret := &ElementCollection[Element]{
g: g,
indexes: indexes,
}
return ret.decorate()
}

func (c *ElementCollection[E]) decorate() *ElementCollection[E] {
if c != nil && debugutil.IsLaunchedByDebugger() {
c.debugInfo = make([]debugInfo, len(c.indexes))
for i, idx := range c.indexes {
s, t, e := c.g.Get(idx)
c.debugInfo[i] = debugInfo{
element: e,
target: t,
status: s,
}
}
}
return c
}

// ElementCollection is an ElementCollectionGetter.
Expand Down Expand Up @@ -98,7 +126,7 @@ func (c *ElementCollection[E]) genericFilter(
if c == nil || len(c.indexes) == 0 {
return nil
}
ret := ElementCollection[E]{
ret := &ElementCollection[E]{
g: c.g,
indexes: make([]int, 0, len(c.indexes)),
}
Expand All @@ -110,7 +138,7 @@ func (c *ElementCollection[E]) genericFilter(
if len(ret.indexes) == 0 {
return nil
}
return &ret
return ret.decorate()
}

// ForEach iterates through the collection and applies fn
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/debugutil/BUILD.bazel
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"],
)
50 changes: 50 additions & 0 deletions pkg/util/debugutil/debugutil.go
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()))
}

0 comments on commit 0777400

Please sign in to comment.