conversions.go 1.9 KB
Newer Older
1 2
package api

A
aarzilli 已提交
3 4
import (
	"debug/gosym"
D
Derek Parker 已提交
5 6
	"os"
	"strings"
D
Derek Parker 已提交
7

A
aarzilli 已提交
8 9
	"github.com/derekparker/delve/proc"
)
10 11 12 13 14 15

// convertBreakpoint converts an internal breakpoint to an API Breakpoint.
func ConvertBreakpoint(bp *proc.Breakpoint) *Breakpoint {
	return &Breakpoint{
		ID:           bp.ID,
		FunctionName: bp.FunctionName,
D
Derek Parker 已提交
16
		File:         shortenFilePath(bp.File),
17 18
		Line:         bp.Line,
		Addr:         bp.Addr,
A
aarzilli 已提交
19 20 21
		Tracepoint:   bp.Tracepoint,
		Stacktrace:   bp.Stacktrace,
		Goroutine:    bp.Goroutine,
D
Derek Parker 已提交
22
		Variables:    bp.Variables,
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	}
}

// convertThread converts an internal thread to an API Thread.
func ConvertThread(th *proc.Thread) *Thread {
	var (
		function *Function
		file     string
		line     int
		pc       uint64
	)

	loc, err := th.Location()
	if err == nil {
		pc = loc.PC
		file = loc.File
		line = loc.Line
A
aarzilli 已提交
40
		function = ConvertFunction(loc.Fn)
41 42 43 44 45
	}

	return &Thread{
		ID:       th.Id,
		PC:       pc,
D
Derek Parker 已提交
46
		File:     shortenFilePath(file),
47 48 49 50 51 52 53 54 55 56 57 58 59 60
		Line:     line,
		Function: function,
	}
}

// convertVar converts an internal variable to an API Variable.
func ConvertVar(v *proc.Variable) Variable {
	return Variable{
		Name:  v.Name,
		Value: v.Value,
		Type:  v.Type,
	}
}

A
aarzilli 已提交
61 62 63 64 65 66 67 68 69 70
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,
71
	}
A
aarzilli 已提交
72
}
73

A
aarzilli 已提交
74 75
// convertGoroutine converts an internal Goroutine to an API Goroutine.
func ConvertGoroutine(g *proc.G) *Goroutine {
76 77 78
	return &Goroutine{
		ID:       g.Id,
		PC:       g.PC,
D
Derek Parker 已提交
79
		File:     shortenFilePath(g.File),
80
		Line:     g.Line,
A
aarzilli 已提交
81 82 83 84 85 86 87
		Function: ConvertFunction(g.Func),
	}
}

func ConvertLocation(loc proc.Location) Location {
	return Location{
		PC:       loc.PC,
D
Derek Parker 已提交
88
		File:     shortenFilePath(loc.File),
A
aarzilli 已提交
89 90
		Line:     loc.Line,
		Function: ConvertFunction(loc.Fn),
91 92
	}
}
D
Derek Parker 已提交
93 94 95 96 97

func shortenFilePath(fullPath string) string {
	workingDir, _ := os.Getwd()
	return strings.Replace(fullPath, workingDir, ".", 1)
}