1
0
mirror of https://github.com/golang/go synced 2024-11-13 17:20:22 -07:00

debug/gosym: parse remote packages correctly

Fixes #15675

Change-Id: I8bad220988e5d690f20804db970b2db037c81187
Reviewed-on: https://go-review.googlesource.com/23086
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Alessandro Arzilli 2016-05-14 07:54:02 +02:00 committed by Ian Lance Taylor
parent 7b597f4d92
commit f744717d19
2 changed files with 57 additions and 5 deletions

View File

@ -40,8 +40,13 @@ func (s *Sym) Static() bool { return s.Type >= 'a' }
// PackageName returns the package part of the symbol name, // PackageName returns the package part of the symbol name,
// or the empty string if there is none. // or the empty string if there is none.
func (s *Sym) PackageName() string { func (s *Sym) PackageName() string {
if i := strings.Index(s.Name, "."); i != -1 { pathend := strings.LastIndex(s.Name, "/")
return s.Name[0:i] if pathend < 0 {
pathend = 0
}
if i := strings.Index(s.Name[pathend:], "."); i != -1 {
return s.Name[:pathend+i]
} }
return "" return ""
} }
@ -49,12 +54,16 @@ func (s *Sym) PackageName() string {
// ReceiverName returns the receiver type name of this symbol, // ReceiverName returns the receiver type name of this symbol,
// or the empty string if there is none. // or the empty string if there is none.
func (s *Sym) ReceiverName() string { func (s *Sym) ReceiverName() string {
l := strings.Index(s.Name, ".") pathend := strings.LastIndex(s.Name, "/")
r := strings.LastIndex(s.Name, ".") if pathend < 0 {
pathend = 0
}
l := strings.Index(s.Name[pathend:], ".")
r := strings.LastIndex(s.Name[pathend:], ".")
if l == -1 || r == -1 || l == r { if l == -1 || r == -1 || l == r {
return "" return ""
} }
return s.Name[l+1 : r] return s.Name[pathend+l+1 : pathend+r]
} }
// BaseName returns the symbol name without the package or receiver name. // BaseName returns the symbol name without the package or receiver name.

View File

@ -0,0 +1,43 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gosym
import (
"fmt"
"testing"
)
func assertString(t *testing.T, dsc, out, tgt string) {
if out != tgt {
t.Fatalf("Expected: %q Actual: %q for %s", tgt, out, dsc)
}
}
func TestStandardLibPackage(t *testing.T) {
s1 := Sym{Name: "io.(*LimitedReader).Read"}
s2 := Sym{Name: "io.NewSectionReader"}
assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "io")
assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "io")
assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*LimitedReader)")
assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
}
func TestStandardLibPathPackage(t *testing.T) {
s1 := Sym{Name: "debug/gosym.(*LineTable).PCToLine"}
s2 := Sym{Name: "debug/gosym.NewTable"}
assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "debug/gosym")
assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "debug/gosym")
assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*LineTable)")
assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
}
func TestRemotePackage(t *testing.T) {
s1 := Sym{Name: "github.com/docker/doc.ker/pkg/mflag.(*FlagSet).PrintDefaults"}
s2 := Sym{Name: "github.com/docker/doc.ker/pkg/mflag.PrintDefaults"}
assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "github.com/docker/doc.ker/pkg/mflag")
assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "github.com/docker/doc.ker/pkg/mflag")
assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*FlagSet)")
assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
}