Skip to content

Commit 48df4e0

Browse files
author
Jeff Wendling
committed
fix up our mapping of dwarf to reflect types
turns out i'm dumb and didn't check that the code actually worked when i changed from using golang.org/x/debug/dwarf to the stdlib dwarf package for things like fmt.Printf (aka stuff i put in the README!). well, this kinda fixes it. i didn't see many types that were reasonable or important missing in my tests. fixes #4
1 parent 1a4d4e4 commit 48df4e0

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

reflect_helpers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func (s sortTypesByString) Swap(i, j int) {
5555
s.types[i], s.types[j] = s.types[j], s.types[i]
5656
}
5757

58-
var unsafePointerType = reflect.TypeOf(new(unsafe.Pointer)).Elem()
58+
var (
59+
unsafePointerType = reflect.TypeOf((*unsafe.Pointer)(nil)).Elem()
60+
)
5961

6062
// dwarfName does a best effort to return the dwarf entry name for the reflect
6163
// type so that we can map between them. here's hoping it doesn't do it wrong

troop_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ func (suite) TestCall(t *testing.T) {
2727
}
2828
}
2929

30+
func (suite) TestCallPrintf(t *testing.T) {
31+
fmt.Printf("hello world\n")
32+
if _, err := troop.Call("fmt.Printf", "hello world\n"); err != nil {
33+
t.Fatal(err)
34+
}
35+
}
36+
3037
func (suite) TestCallFailures(t *testing.T) {
3138
type is []interface{}
3239

troop_types.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77
"sort"
8+
"strings"
89
"unsafe"
910
)
1011

@@ -21,6 +22,11 @@ func (t *Troop) addTypes() error {
2122
t.addType(typ)
2223
}
2324
}
25+
26+
// special cased types
27+
t.types["*void"] = unsafePointerType
28+
t.types["**void"] = reflect.PtrTo(unsafePointerType)
29+
2430
return nil
2531
}
2632

@@ -90,7 +96,25 @@ func (t *Troop) findDwarfType(dtyp dwarf.Type) (reflect.Type, error) {
9096
// TODO(jeff): we can synthesize some of these dwarf types at runtime,
9197
// but hopefully we got enough of them when we loaded up all of the
9298
// types. The problematic types are: 1. named types, 2. recursive types.
93-
dname := dtyp.Common().Name
99+
var dname string
100+
switch dtyp := dtyp.(type) {
101+
case *dwarf.StructType:
102+
if dtyp.StructName != "" {
103+
dname = dtyp.StructName
104+
} else {
105+
dname = dtyp.Defn()
106+
}
107+
default:
108+
dname = dtyp.String()
109+
}
110+
111+
// heh this is super hacky, but what isn't!?
112+
if strings.HasPrefix(dname, "*struct ") &&
113+
!strings.HasPrefix(dname, "*struct {") {
114+
115+
dname = "*" + dname[len("*struct "):]
116+
}
117+
94118
typ, ok := t.types[dname]
95119
if !ok {
96120
return nil, fmt.Errorf("dwarf type %q unknown", dname)

0 commit comments

Comments
 (0)