1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 21:51:30 +00:00
bee/vendor/github.com/derekparker/delve/service/api/conversions.go

315 lines
7.0 KiB
Go
Raw Normal View History

2017-03-19 22:45:54 +00:00
package api
import (
"bytes"
"go/constant"
"go/printer"
"go/token"
"reflect"
"strconv"
2018-10-13 13:45:53 +00:00
"github.com/derekparker/delve/pkg/dwarf/godwarf"
"github.com/derekparker/delve/pkg/proc"
2017-03-19 22:45:54 +00:00
)
// ConvertBreakpoint converts from a proc.Breakpoint to
// an api.Breakpoint.
func ConvertBreakpoint(bp *proc.Breakpoint) *Breakpoint {
b := &Breakpoint{
Name: bp.Name,
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,
LoadArgs: LoadConfigFromProc(bp.LoadArgs),
LoadLocals: LoadConfigFromProc(bp.LoadLocals),
TotalHitCount: bp.TotalHitCount,
}
b.HitCount = map[string]uint64{}
for idx := range bp.HitCount {
b.HitCount[strconv.Itoa(idx)] = bp.HitCount[idx]
}
var buf bytes.Buffer
printer.Fprint(&buf, token.NewFileSet(), bp.Cond)
b.Cond = buf.String()
return b
}
// ConvertThread converts a proc.Thread into an
// api thread.
2018-10-13 13:45:53 +00:00
func ConvertThread(th proc.Thread) *Thread {
2017-03-19 22:45:54 +00:00
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
2018-10-13 13:45:53 +00:00
if b := th.Breakpoint(); b.Active {
bp = ConvertBreakpoint(b.Breakpoint)
2017-03-19 22:45:54 +00:00
}
2018-10-13 13:45:53 +00:00
if g, _ := proc.GetG(th); g != nil {
2017-03-19 22:45:54 +00:00
gid = g.ID
}
return &Thread{
2018-10-13 13:45:53 +00:00
ID: th.ThreadID(),
2017-03-19 22:45:54 +00:00
PC: pc,
File: file,
Line: line,
Function: function,
GoroutineID: gid,
Breakpoint: bp,
}
}
2018-10-13 13:45:53 +00:00
func prettyTypeName(typ godwarf.Type) string {
2017-03-19 22:45:54 +00:00
if typ == nil {
return ""
}
if typ.Common().Name != "" {
return typ.Common().Name
}
r := typ.String()
if r == "*void" {
return "unsafe.Pointer"
}
return r
}
2018-10-13 13:45:53 +00:00
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)
}
2017-03-19 22:45:54 +00:00
// 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,
2018-10-13 13:45:53 +00:00
Flags: VariableFlags(v.Flags),
Base: v.Base,
LocationExpr: v.LocationExpr,
DeclLine: v.DeclLine,
2017-03-19 22:45:54 +00:00
}
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:
2018-10-13 13:45:53 +00:00
r.Value = convertFloatValue(v, 32)
2017-03-19 22:45:54 +00:00
case reflect.Float64:
2018-10-13 13:45:53 +00:00
r.Value = convertFloatValue(v, 64)
2017-03-19 22:45:54 +00:00
case reflect.String, reflect.Func:
r.Value = constant.StringVal(v.Value)
default:
2018-10-13 13:45:53 +00:00
r.Value = v.ConstDescr()
if r.Value == "" {
r.Value = v.Value.String()
}
2017-03-19 22:45:54 +00:00
}
}
switch v.Kind {
case reflect.Complex64:
r.Children = make([]Variable, 2)
r.Len = 2
r.Children[0].Name = "real"
r.Children[0].Kind = reflect.Float32
r.Children[1].Name = "imaginary"
r.Children[1].Kind = reflect.Float32
2018-10-13 13:45:53 +00:00
if v.Value != nil {
real, _ := constant.Float64Val(constant.Real(v.Value))
r.Children[0].Value = strconv.FormatFloat(real, 'f', -1, 32)
imag, _ := constant.Float64Val(constant.Imag(v.Value))
r.Children[1].Value = strconv.FormatFloat(imag, 'f', -1, 32)
} else {
r.Children[0].Value = "nil"
r.Children[1].Value = "nil"
}
2017-03-19 22:45:54 +00:00
case reflect.Complex128:
r.Children = make([]Variable, 2)
r.Len = 2
r.Children[0].Name = "real"
r.Children[0].Kind = reflect.Float64
r.Children[1].Name = "imaginary"
r.Children[1].Kind = reflect.Float64
2018-10-13 13:45:53 +00:00
if v.Value != nil {
real, _ := constant.Float64Val(constant.Real(v.Value))
r.Children[0].Value = strconv.FormatFloat(real, 'f', -1, 64)
imag, _ := constant.Float64Val(constant.Imag(v.Value))
r.Children[1].Value = strconv.FormatFloat(imag, 'f', -1, 64)
} else {
r.Children[0].Value = "nil"
r.Children[1].Value = "nil"
}
2017-03-19 22:45:54 +00:00
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.
2018-10-13 13:45:53 +00:00
func ConvertFunction(fn *proc.Function) *Function {
2017-03-19 22:45:54 +00:00
if fn == nil {
return nil
}
2018-10-13 13:45:53 +00:00
// fn here used to be a *gosym.Func, the fields Type and GoType below
// corresponded to the homonymous field of gosym.Func. Since the contents of
// those fields is not documented their value was replaced with 0 when
// gosym.Func was replaced by debug_info entries.
2017-03-19 22:45:54 +00:00
return &Function{
2018-10-13 13:45:53 +00:00
Name_: fn.Name,
Type: 0,
Value: fn.Entry,
GoType: 0,
Optimized: fn.Optimized(),
2017-03-19 22:45:54 +00:00
}
}
// ConvertGoroutine converts from proc.G to api.Goroutine.
func ConvertGoroutine(g *proc.G) *Goroutine {
2018-10-13 13:45:53 +00:00
th := g.Thread
2017-03-19 22:45:54 +00:00
tid := 0
if th != nil {
2018-10-13 13:45:53 +00:00
tid = th.ThreadID()
2017-03-19 22:45:54 +00:00
}
2018-10-13 13:45:53 +00:00
r := &Goroutine{
2017-03-19 22:45:54 +00:00
ID: g.ID,
CurrentLoc: ConvertLocation(g.CurrentLoc),
UserCurrentLoc: ConvertLocation(g.UserCurrent()),
GoStatementLoc: ConvertLocation(g.Go()),
2018-10-13 13:45:53 +00:00
StartLoc: ConvertLocation(g.StartLoc()),
2017-03-19 22:45:54 +00:00
ThreadID: tid,
}
2018-10-13 13:45:53 +00:00
if g.Unreadable != nil {
r.Unreadable = g.Unreadable.Error()
}
return r
2017-03-19 22:45:54 +00:00
}
// 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),
}
}
2018-10-13 13:45:53 +00:00
// ConvertAsmInstruction converts from proc.AsmInstruction to api.AsmInstruction.
2017-03-19 22:45:54 +00:00
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,
}
}
2018-10-13 13:45:53 +00:00
// LoadConfigToProc converts an api.LoadConfig to proc.LoadConfig.
2017-03-19 22:45:54 +00:00
func LoadConfigToProc(cfg *LoadConfig) *proc.LoadConfig {
if cfg == nil {
return nil
}
return &proc.LoadConfig{
cfg.FollowPointers,
cfg.MaxVariableRecurse,
cfg.MaxStringLen,
cfg.MaxArrayValues,
cfg.MaxStructFields,
}
}
2018-10-13 13:45:53 +00:00
// LoadConfigFromProc converts a proc.LoadConfig to api.LoadConfig.
2017-03-19 22:45:54 +00:00
func LoadConfigFromProc(cfg *proc.LoadConfig) *LoadConfig {
if cfg == nil {
return nil
}
return &LoadConfig{
cfg.FollowPointers,
cfg.MaxVariableRecurse,
cfg.MaxStringLen,
cfg.MaxArrayValues,
cfg.MaxStructFields,
}
}
2018-10-13 13:45:53 +00:00
// ConvertRegisters converts proc.Register to api.Register for a slice.
2017-03-19 22:45:54 +00:00
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
}
2018-10-13 13:45:53 +00:00
// ConvertCheckpoint converts proc.Chekcpoint to api.Checkpoint.
func ConvertCheckpoint(in proc.Checkpoint) (out Checkpoint) {
return Checkpoint(in)
}