Skip to content

Commit

Permalink
proc,_scripts/rtype.go: add rtype annotations for g.atomicstatus (go-…
Browse files Browse the repository at this point in the history
…delve#3143)

Adds some rtype annotations for g.atomicstatus and update
_scripts/rtype.go to handle types outside of the runtime package.
  • Loading branch information
aarzilli authored Sep 26, 2022
1 parent 1effee3 commit 4372ce0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
6 changes: 5 additions & 1 deletion _scripts/rtype-out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type g struct {
waitsince int64
waitreason waitReason (optional)
stack stack
atomicstatus anytype
atomicstatus uint32|runtime/internal/atomic.Uint32
}

type gobuf struct {
Expand Down Expand Up @@ -58,6 +58,10 @@ type moduledata struct {
types uintptr
}

type runtime/internal/atomic.Uint32 struct {
value uint32
}

type stack struct {
hi uintptr
lo uintptr
Expand Down
38 changes: 31 additions & 7 deletions _scripts/rtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,20 +512,44 @@ func report() {
}
}

// check parses the runtime package and checks that all the rules retrieved
// from the 'proc' package pass.
func check() {
pkgs2, err := packages.Load(&packages.Config{Mode: packages.LoadSyntax, Fset: fset}, "runtime")
func lookupPackage(pkgmap map[string]*packages.Package, name string) *packages.Package {
if pkgmap[name] != nil {
return pkgmap[name]
}

pkgs, err := packages.Load(&packages.Config{Mode: packages.LoadSyntax, Fset: fset}, name)
if err != nil {
log.Fatalf("could not load runtime package: %v", err)
}
packages.Visit(pkgs, func(pkg *packages.Package) bool {
if pkgmap[pkg.ID] == nil {
pkgmap[pkg.ID] = pkg
}
return true
}, nil)

return pkgmap[name]
}

func lookupTypeDef(pkgmap map[string]*packages.Package, typ string) types.Object {
dot := strings.Index(typ, ".")
if dot < 0 {
return lookupPackage(pkgmap, "runtime").Types.Scope().Lookup(typ)
}

return lookupPackage(pkgmap, typ[:dot]).Types.Scope().Lookup(typ[dot+1:])
}

// check parses the runtime package and checks that all the rules retrieved
// from the 'proc' package pass.
func check() {
pkgmap := map[string]*packages.Package{}
allok := true

for _, rule := range checkVarTypeRules {
//TODO: implement
pos := fset.Position(rule.pos)
def := pkgs2[0].Types.Scope().Lookup(rule.V)
def := lookupPackage(pkgmap, "runtime").Types.Scope().Lookup(rule.V)
if def == nil {
fmt.Fprintf(os.Stderr, "%s:%d: could not find variable %s\n", pos.Filename, pos.Line, rule.V)
allok = false
Expand All @@ -547,7 +571,7 @@ func check() {
rules := checkFieldTypeRules[S]
pos := fset.Position(rules[0].pos)

def := pkgs2[0].Types.Scope().Lookup(S)
def := lookupTypeDef(pkgmap, S)
if def == nil {
fmt.Fprintf(os.Stderr, "%s:%d: could not find struct %s\n", pos.Filename, pos.Line, S)
allok = false
Expand Down Expand Up @@ -594,7 +618,7 @@ func check() {
for _, C := range Cs {
rules := checkConstValRules[C]
pos := fset.Position(rules[0].pos)
def := pkgs2[0].Types.Scope().Lookup(C)
def := lookupPackage(pkgmap, "runtime").Types.Scope().Lookup(C)
if def == nil {
fmt.Fprintf(os.Stderr, "%s:%d: could not find constant %s\n", pos.Filename, pos.Line, C)
allok = false
Expand Down
7 changes: 4 additions & 3 deletions cmd/dlv/dlv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,10 @@ func TestGeneratedDoc(t *testing.T) {
checkAutogenDoc(t, "pkg/terminal/starbind/starlark_mapping.go", "'go generate' inside pkg/terminal/starbind", runScript("_scripts/gen-starlark-bindings.go", "go", "-"))
checkAutogenDoc(t, "Documentation/cli/starlark.md", "'go generate' inside pkg/terminal/starbind", runScript("_scripts/gen-starlark-bindings.go", "doc/dummy", "Documentation/cli/starlark.md"))
checkAutogenDoc(t, "Documentation/backend_test_health.md", "go run _scripts/gen-backend_test_health.go", runScript("_scripts/gen-backend_test_health.go", "-"))
checkAutogenDoc(t, "_scripts/rtype-out.txt", "go run _scripts/rtype.go report _scripts/rtype-out.txt", runScript("_scripts/rtype.go", "report"))

runScript("_scripts/rtype.go", "check")
if goversion.VersionAfterOrEqual(runtime.Version(), 1, 18) {
checkAutogenDoc(t, "_scripts/rtype-out.txt", "go run _scripts/rtype.go report _scripts/rtype-out.txt", runScript("_scripts/rtype.go", "report"))
runScript("_scripts/rtype.go", "check")
}
}

func TestExitInInit(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/proc/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,11 +923,12 @@ func (v *Variable) parseG() (*G, error) {
}

status := uint64(0)
if atomicStatus := v.loadFieldNamed("atomicstatus"); atomicStatus != nil {
if atomicStatus := v.loadFieldNamed("atomicstatus"); /* +rtype uint32|runtime/internal/atomic.Uint32 */ atomicStatus != nil {
if constant.Val(atomicStatus.Value) != nil {
status, _ = constant.Uint64Val(atomicStatus.Value)
} else {
vv := atomicStatus.fieldVariable("value")
atomicStatus := atomicStatus // +rtype runtime/internal/atomic.Uint32
vv := atomicStatus.fieldVariable("value") // +rtype uint32
if vv == nil {
unreadable = true
} else {
Expand Down

0 comments on commit 4372ce0

Please sign in to comment.