conversions.go 5.7 KB
Newer Older
1 2
package api

A
aarzilli 已提交
3
import (
4
	"bytes"
A
aarzilli 已提交
5
	"debug/gosym"
6
	"go/constant"
7 8
	"go/printer"
	"go/token"
9
	"reflect"
10
	"strconv"
D
Derek Parker 已提交
11

D
Derek Parker 已提交
12 13
	"github.com/derekparker/delve/pkg/proc"

A
aarzilli 已提交
14
	"golang.org/x/debug/dwarf"
A
aarzilli 已提交
15
)
16

D
Derek Parker 已提交
17 18
// ConvertBreakpoint converts from a proc.Breakpoint to
// an api.Breakpoint.
19
func ConvertBreakpoint(bp *proc.Breakpoint) *Breakpoint {
20
	b := &Breakpoint{
21
		Name:          bp.Name,
22 23 24 25 26 27 28 29 30
		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,
31 32
		LoadArgs:      LoadConfigFromProc(bp.LoadArgs),
		LoadLocals:    LoadConfigFromProc(bp.LoadLocals),
33
		TotalHitCount: bp.TotalHitCount,
34
	}
35 36 37 38 39 40

	b.HitCount = map[string]uint64{}
	for idx := range bp.HitCount {
		b.HitCount[strconv.Itoa(idx)] = bp.HitCount[idx]
	}

41 42 43 44
	var buf bytes.Buffer
	printer.Fprint(&buf, token.NewFileSet(), bp.Cond)
	b.Cond = buf.String()

45
	return b
46 47
}

D
Derek Parker 已提交
48 49
// ConvertThread converts a proc.Thread into an
// api thread.
50
func ConvertThread(th proc.IThread) *Thread {
51 52 53 54 55
	var (
		function *Function
		file     string
		line     int
		pc       uint64
56
		gid      int
57 58 59 60 61 62 63
	)

	loc, err := th.Location()
	if err == nil {
		pc = loc.PC
		file = loc.File
		line = loc.Line
A
aarzilli 已提交
64
		function = ConvertFunction(loc.Fn)
65 66
	}

67 68
	var bp *Breakpoint

69 70
	if b, active, _ := th.Breakpoint(); active {
		bp = ConvertBreakpoint(b)
71 72
	}

73
	if g, _ := proc.GetG(th); g != nil {
D
Derek Parker 已提交
74
		gid = g.ID
75 76
	}

77
	return &Thread{
78
		ID:          th.ThreadID(),
79 80 81 82 83 84
		PC:          pc,
		File:        file,
		Line:        line,
		Function:    function,
		GoroutineID: gid,
		Breakpoint:  bp,
85 86 87
	}
}

88 89 90 91
func prettyTypeName(typ dwarf.Type) string {
	if typ == nil {
		return ""
	}
92 93 94
	if typ.Common().Name != "" {
		return typ.Common().Name
	}
95 96 97 98 99 100 101
	r := typ.String()
	if r == "*void" {
		return "unsafe.Pointer"
	}
	return r
}

102 103 104 105 106 107 108 109 110 111 112 113 114
func convertFloatValue(v *proc.Variable, sz int) string {
	switch v.FloatSpecial {
	case proc.FloatIsPosInf:
		return "+Inf"
	case proc.FloatIsNegInf:
		return "-Inf"
	case proc.FloatIsNaN:
		return "NaN"
	}
	f, _ := constant.Float64Val(v.Value)
	return strconv.FormatFloat(f, 'f', -1, sz)
}

D
Derek Parker 已提交
115
// ConvertVar converts from proc.Variable to api.Variable.
116 117
func ConvertVar(v *proc.Variable) *Variable {
	r := Variable{
A
aarzilli 已提交
118 119 120 121 122 123
		Addr:     v.Addr,
		OnlyAddr: v.OnlyAddr,
		Name:     v.Name,
		Kind:     v.Kind,
		Len:      v.Len,
		Cap:      v.Cap,
124
	}
125

126 127
	r.Type = prettyTypeName(v.DwarfType)
	r.RealType = prettyTypeName(v.RealType)
128 129 130 131 132

	if v.Unreadable != nil {
		r.Unreadable = v.Unreadable.Error()
	}

133 134 135
	if v.Value != nil {
		switch v.Kind {
		case reflect.Float32:
136
			r.Value = convertFloatValue(v, 32)
137
		case reflect.Float64:
138
			r.Value = convertFloatValue(v, 64)
139 140 141 142
		case reflect.String, reflect.Func:
			r.Value = constant.StringVal(v.Value)
		default:
			r.Value = v.Value.String()
143 144 145
		}
	}

A
aarzilli 已提交
146 147 148 149
	switch v.Kind {
	case reflect.Complex64:
		r.Children = make([]Variable, 2)
		r.Len = 2
150

A
aarzilli 已提交
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
		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])
		}
182 183 184
	}

	return &r
185 186
}

D
Derek Parker 已提交
187 188
// ConvertFunction converts from gosym.Func to
// api.Function.
A
aarzilli 已提交
189 190 191 192 193 194 195 196 197 198
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,
199
	}
A
aarzilli 已提交
200
}
201

D
Derek Parker 已提交
202
// ConvertGoroutine converts from proc.G to api.Goroutine.
A
aarzilli 已提交
203
func ConvertGoroutine(g *proc.G) *Goroutine {
204 205 206
	th := g.Thread()
	tid := 0
	if th != nil {
207
		tid = th.ThreadID()
208
	}
209
	return &Goroutine{
D
Derek Parker 已提交
210
		ID:             g.ID,
211 212 213
		CurrentLoc:     ConvertLocation(g.CurrentLoc),
		UserCurrentLoc: ConvertLocation(g.UserCurrent()),
		GoStatementLoc: ConvertLocation(g.Go()),
A
aarzilli 已提交
214
		ThreadID:       tid,
A
aarzilli 已提交
215 216 217
	}
}

D
Derek Parker 已提交
218
// ConvertLocation converts from proc.Location to api.Location.
A
aarzilli 已提交
219 220 221
func ConvertLocation(loc proc.Location) Location {
	return Location{
		PC:       loc.PC,
222
		File:     loc.File,
A
aarzilli 已提交
223 224
		Line:     loc.Line,
		Function: ConvertFunction(loc.Fn),
225 226
	}
}
A
aarzilli 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

func ConvertAsmInstruction(inst proc.AsmInstruction, text string) AsmInstruction {
	var destloc *Location
	if inst.DestLoc != nil {
		r := ConvertLocation(*inst.DestLoc)
		destloc = &r
	}
	return AsmInstruction{
		Loc:        ConvertLocation(inst.Loc),
		DestLoc:    destloc,
		Text:       text,
		Bytes:      inst.Bytes,
		Breakpoint: inst.Breakpoint,
		AtPC:       inst.AtPC,
	}
}
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

func LoadConfigToProc(cfg *LoadConfig) *proc.LoadConfig {
	if cfg == nil {
		return nil
	}
	return &proc.LoadConfig{
		cfg.FollowPointers,
		cfg.MaxVariableRecurse,
		cfg.MaxStringLen,
		cfg.MaxArrayValues,
		cfg.MaxStructFields,
	}
}

func LoadConfigFromProc(cfg *proc.LoadConfig) *LoadConfig {
	if cfg == nil {
		return nil
	}
	return &LoadConfig{
		cfg.FollowPointers,
		cfg.MaxVariableRecurse,
		cfg.MaxStringLen,
		cfg.MaxArrayValues,
		cfg.MaxStructFields,
	}
}
A
aarzilli 已提交
269 270 271 272 273 274 275 276

func ConvertRegisters(in []proc.Register) (out []Register) {
	out = make([]Register, len(in))
	for i := range in {
		out[i] = Register{in[i].Name, in[i].Value}
	}
	return
}