forked from go-delve/delve
/
conversions.go
195 lines (165 loc) · 4.16 KB
/
conversions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package api
import (
"debug/gosym"
"go/constant"
"golang.org/x/debug/dwarf"
"reflect"
"strconv"
"github.com/derekparker/delve/proc"
)
// ConvertBreakpoint converts from a proc.Breakpoint to
// an api.Breakpoint.
func ConvertBreakpoint(bp *proc.Breakpoint) *Breakpoint {
b := &Breakpoint{
ID: bp.ID,
FunctionName: bp.FunctionName,
File: bp.File,
Line: bp.Line,
Addr: bp.Addr,
Tracepoint: bp.Tracepoint,
Stacktrace: bp.Stacktrace,
Goroutine: bp.Goroutine,
Variables: bp.Variables,
TotalHitCount: bp.TotalHitCount,
}
b.HitCount = map[string]uint64{}
for idx := range bp.HitCount {
b.HitCount[strconv.Itoa(idx)] = bp.HitCount[idx]
}
return b
}
// ConvertThread converts a proc.Thread into an
// api thread.
func ConvertThread(th *proc.Thread) *Thread {
var (
function *Function
file string
line int
pc uint64
gid int
)
loc, err := th.Location()
if err == nil {
pc = loc.PC
file = loc.File
line = loc.Line
function = ConvertFunction(loc.Fn)
}
var bp *Breakpoint
if th.CurrentBreakpoint != nil && th.BreakpointConditionMet {
bp = ConvertBreakpoint(th.CurrentBreakpoint)
}
if g, _ := th.GetG(); g != nil {
gid = g.ID
}
return &Thread{
ID: th.ID,
PC: pc,
File: file,
Line: line,
Function: function,
GoroutineID: gid,
Breakpoint: bp,
}
}
func prettyTypeName(typ dwarf.Type) string {
if typ == nil {
return ""
}
r := typ.String()
if r == "*void" {
return "unsafe.Pointer"
}
return r
}
// ConvertVar converts from proc.Variable to api.Variable.
func ConvertVar(v *proc.Variable) *Variable {
r := Variable{
Addr: v.Addr,
OnlyAddr: v.OnlyAddr,
Name: v.Name,
Kind: v.Kind,
Len: v.Len,
Cap: v.Cap,
}
r.Type = prettyTypeName(v.DwarfType)
r.RealType = prettyTypeName(v.RealType)
if v.Unreadable != nil {
r.Unreadable = v.Unreadable.Error()
}
if v.Value != nil {
switch v.Kind {
case reflect.Float32:
f, _ := constant.Float64Val(v.Value)
r.Value = strconv.FormatFloat(f, 'f', -1, 32)
case reflect.Float64:
f, _ := constant.Float64Val(v.Value)
r.Value = strconv.FormatFloat(f, 'f', -1, 64)
case reflect.String, reflect.Func:
r.Value = constant.StringVal(v.Value)
default:
r.Value = v.Value.String()
}
}
switch v.Kind {
case reflect.Complex64:
r.Children = make([]Variable, 2)
r.Len = 2
real, _ := constant.Float64Val(constant.Real(v.Value))
imag, _ := constant.Float64Val(constant.Imag(v.Value))
r.Children[0].Name = "real"
r.Children[0].Kind = reflect.Float32
r.Children[0].Value = strconv.FormatFloat(real, 'f', -1, 32)
r.Children[1].Name = "imaginary"
r.Children[1].Kind = reflect.Float32
r.Children[1].Value = strconv.FormatFloat(imag, 'f', -1, 32)
case reflect.Complex128:
r.Children = make([]Variable, 2)
r.Len = 2
real, _ := constant.Float64Val(constant.Real(v.Value))
imag, _ := constant.Float64Val(constant.Imag(v.Value))
r.Children[0].Name = "real"
r.Children[0].Kind = reflect.Float64
r.Children[0].Value = strconv.FormatFloat(real, 'f', -1, 64)
r.Children[1].Name = "imaginary"
r.Children[1].Kind = reflect.Float64
r.Children[1].Value = strconv.FormatFloat(imag, 'f', -1, 64)
default:
r.Children = make([]Variable, len(v.Children))
for i := range v.Children {
r.Children[i] = *ConvertVar(&v.Children[i])
}
}
return &r
}
// ConvertFunction converts from gosym.Func to
// api.Function.
func ConvertFunction(fn *gosym.Func) *Function {
if fn == nil {
return nil
}
return &Function{
Name: fn.Name,
Type: fn.Type,
Value: fn.Value,
GoType: fn.GoType,
}
}
// ConvertGoroutine converts from proc.G to api.Goroutine.
func ConvertGoroutine(g *proc.G) *Goroutine {
return &Goroutine{
ID: g.ID,
CurrentLoc: ConvertLocation(g.CurrentLoc),
UserCurrentLoc: ConvertLocation(g.UserCurrent()),
GoStatementLoc: ConvertLocation(g.Go()),
}
}
// ConvertLocation converts from proc.Location to api.Location.
func ConvertLocation(loc proc.Location) Location {
return Location{
PC: loc.PC,
File: loc.File,
Line: loc.Line,
Function: ConvertFunction(loc.Fn),
}
}