types.go 9.0 KB
Newer Older
D
Dan Mace 已提交
1 2
package api

A
aarzilli 已提交
3
import (
4 5
	"errors"
	"fmt"
A
aarzilli 已提交
6
	"reflect"
7 8
	"strconv"
	"unicode"
H
Hubert Krauze 已提交
9 10

	"github.com/derekparker/delve/proc"
A
aarzilli 已提交
11
)
12

D
Dan Mace 已提交
13 14 15 16
// DebuggerState represents the current context of the debugger.
type DebuggerState struct {
	// CurrentThread is the currently selected debugger thread.
	CurrentThread *Thread `json:"currentThread,omitempty"`
17 18
	// SelectedGoroutine is the currently selected goroutine
	SelectedGoroutine *Goroutine `json:"currentGoroutine,omitempty"`
19 20
	// List of all the process threads
	Threads []*Thread
D
Dan Mace 已提交
21
	// Exited indicates whether the debugged process has exited.
22 23
	Exited     bool `json:"exited"`
	ExitStatus int  `json:"exitStatus"`
A
aarzilli 已提交
24 25
	// Filled by RPCClient.Continue, indicates an error
	Err error `json:"-"`
D
Dan Mace 已提交
26 27
}

D
Derek Parker 已提交
28
// Breakpoint addresses a location at which process execution may be
D
Dan Mace 已提交
29
// suspended.
D
Derek Parker 已提交
30
type Breakpoint struct {
D
Dan Mace 已提交
31 32
	// ID is a unique identifier for the breakpoint.
	ID int `json:"id"`
33 34
	// User defined name of the breakpoint
	Name string `json:"name"`
D
Dan Mace 已提交
35 36 37 38 39 40 41 42 43
	// Addr is the address of the breakpoint.
	Addr uint64 `json:"addr"`
	// File is the source file for the breakpoint.
	File string `json:"file"`
	// Line is a line in File for the breakpoint.
	Line int `json:"line"`
	// FunctionName is the name of the function at the current breakpoint, and
	// may not always be available.
	FunctionName string `json:"functionName,omitempty"`
A
aarzilli 已提交
44

45 46 47
	// Breakpoint condition
	Cond string

A
aarzilli 已提交
48 49 50 51
	// tracepoint flag
	Tracepoint bool `json:"continue"`
	// retrieve goroutine information
	Goroutine bool `json:"goroutine"`
H
Hubert Krauze 已提交
52 53
	// number of stack frames to retrieve
	Stacktrace int `json:"stacktrace"`
54
	// expressions to evaluate
D
Derek Parker 已提交
55
	Variables []string `json:"variables,omitempty"`
56 57 58 59
	// LoadArgs requests loading function arguments when the breakpoint is hit
	LoadArgs *LoadConfig
	// LoadLocals requests loading function locals when the breakpoint is hit
	LoadLocals *LoadConfig
60 61 62 63
	// number of times a breakpoint has been reached in a certain goroutine
	HitCount map[string]uint64 `json:"hitCount"`
	// number of times a breakpoint has been reached
	TotalHitCount uint64 `json:"totalHitCount"`
D
Dan Mace 已提交
64 65
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79
func ValidBreakpointName(name string) error {
	if _, err := strconv.Atoi(name); err == nil {
		return errors.New("breakpoint name can not be a number")
	}

	for _, ch := range name {
		if !(unicode.IsLetter(ch) || unicode.IsDigit(ch)) {
			return fmt.Errorf("invalid character in breakpoint name '%c'", ch)
		}
	}

	return nil
}

D
Dan Mace 已提交
80 81 82 83 84 85 86 87 88 89 90 91
// Thread is a thread within the debugged process.
type Thread struct {
	// ID is a unique identifier for the thread.
	ID int `json:"id"`
	// PC is the current program counter for the thread.
	PC uint64 `json:"pc"`
	// File is the file for the program counter.
	File string `json:"file"`
	// Line is the line number for the program counter.
	Line int `json:"line"`
	// Function is function information at the program counter. May be nil.
	Function *Function `json:"function,omitempty"`
92 93 94 95 96 97 98 99

	// ID of the goroutine running on this thread
	GoroutineID int `json:"goroutineID"`

	// Breakpoint this thread is stopped at
	Breakpoint *Breakpoint `json:"breakPoint,omitempty"`
	// Informations requested by the current breakpoint
	BreakpointInfo *BreakpointInfo `json:"breakPointInfo,omitrempty"`
D
Dan Mace 已提交
100 101
}

A
aarzilli 已提交
102 103 104 105 106 107 108
type Location struct {
	PC       uint64    `json:"pc"`
	File     string    `json:"file"`
	Line     int       `json:"line"`
	Function *Function `json:"function,omitempty"`
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
type Stackframe struct {
	Location
	Locals    []Variable
	Arguments []Variable
}

func (frame *Stackframe) Var(name string) *Variable {
	for i := range frame.Locals {
		if frame.Locals[i].Name == name {
			return &frame.Locals[i]
		}
	}
	for i := range frame.Arguments {
		if frame.Arguments[i].Name == name {
			return &frame.Arguments[i]
		}
	}
	return nil
}

D
Dan Mace 已提交
129 130 131 132 133 134 135 136 137 138 139
// Function represents thread-scoped function information.
type Function struct {
	// Name is the function name.
	Name   string `json:"name"`
	Value  uint64 `json:"value"`
	Type   byte   `json:"type"`
	GoType uint64 `json:"goType"`
}

// Variable describes a variable.
type Variable struct {
140 141 142 143
	// Name of the variable or struct member
	Name string `json:"name"`
	// Address of the variable or struct member
	Addr uintptr `json:"addr"`
A
aarzilli 已提交
144 145
	// Only the address field is filled (result of evaluating expressions like &<expr>)
	OnlyAddr bool `json:"onlyAddr"`
146 147 148 149 150 151 152 153 154
	// Go type of the variable
	Type string `json:"type"`
	// Type of the variable after resolving any typedefs
	RealType string `json:"realType"`

	Kind reflect.Kind `json:"kind"`

	//Strings have their length capped at proc.maxArrayValues, use Len for the real length of a string
	//Function variables will store the name of the function in this field
D
Dan Mace 已提交
155
	Value string `json:"value"`
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

	// Number of elements in an array or a slice, number of keys for a map, number of struct members for a struct, length of strings
	Len int64 `json:"len"`
	// Cap value for slices
	Cap int64 `json:"cap"`

	// Array and slice elements, member fields of structs, key/value pairs of maps, value of complex numbers
	// The Name field in this slice will always be the empty string except for structs (when it will be the field name) and for complex numbers (when it will be "real" and "imaginary")
	// For maps each map entry will have to items in this slice, even numbered items will represent map keys and odd numbered items will represent their values
	// This field's length is capped at proc.maxArrayValues for slices and arrays and 2*proc.maxArrayValues for maps, in the circumnstances where the cap takes effect len(Children) != Len
	// The other length cap applied to this field is related to maximum recursion depth, when the maximum recursion depth is reached this field is left empty, contrary to the previous one this cap also applies to structs (otherwise structs will always have all thier member fields returned)
	Children []Variable `json:"children"`

	// Unreadable addresses will have this field set
	Unreadable string `json:"unreadable"`
D
Dan Mace 已提交
171 172
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186
// LoadConfig describes how to load values from target's memory
type LoadConfig struct {
	// FollowPointers requests pointers to be automatically dereferenced.
	FollowPointers bool
	// MaxVariableRecurse is how far to recurse when evaluating nested types.
	MaxVariableRecurse int
	// MaxStringLen is the maximum number of bytes read from a string
	MaxStringLen int
	// MaxArrayValues is the maximum number of elements read from an array, a slice or a map.
	MaxArrayValues int
	// MaxStructFields is the maximum number of fields read from a struct, -1 will read all fields.
	MaxStructFields int
}

D
Dan Mace 已提交
187 188 189 190 191
// Goroutine represents the information relevant to Delve from the runtime's
// internal G structure.
type Goroutine struct {
	// ID is a unique identifier for the goroutine.
	ID int `json:"id"`
192
	// Current location of the goroutine
193
	CurrentLoc Location `json:"currentLoc"`
194
	// Current location of the goroutine, excluding calls inside runtime
195
	UserCurrentLoc Location `json:"userCurrentLoc"`
196
	// Location of the go instruction that started this goroutine
197
	GoStatementLoc Location `json:"goStatementLoc"`
D
Dan Mace 已提交
198 199 200 201 202 203 204 205 206
}

// DebuggerCommand is a command which changes the debugger's execution state.
type DebuggerCommand struct {
	// Name is the command to run.
	Name string `json:"name"`
	// ThreadID is used to specify which thread to use with the SwitchThread
	// command.
	ThreadID int `json:"threadID,omitempty"`
207 208 209
	// GoroutineID is used to specify which thread to use with the SwitchGoroutine
	// command.
	GoroutineID int `json:"goroutineID,omitempty"`
D
Dan Mace 已提交
210 211
}

A
aarzilli 已提交
212 213
// Informations about the current breakpoint
type BreakpointInfo struct {
214 215 216 217
	Stacktrace []Stackframe `json:"stacktrace,omitempty"`
	Goroutine  *Goroutine   `json:"goroutine,omitempty"`
	Variables  []Variable   `json:"variables,omitempty"`
	Arguments  []Variable   `json:"arguments,omitempty"`
218
	Locals     []Variable   `json:"locals,omitempty"`
A
aarzilli 已提交
219 220
}

221 222 223 224 225
type EvalScope struct {
	GoroutineID int
	Frame       int
}

D
Dan Mace 已提交
226 227 228
const (
	// Continue resumes process execution.
	Continue = "continue"
229
	// Step continues to next source line, entering function calls.
D
Dan Mace 已提交
230
	Step = "step"
231 232
	// SingleStep continues for exactly 1 cpu instruction.
	StepInstruction = "stepInstruction"
D
Dan Mace 已提交
233 234 235 236
	// Next continues to the next source line, not entering function calls.
	Next = "next"
	// SwitchThread switches the debugger's current thread context.
	SwitchThread = "switchThread"
237 238
	// SwitchGoroutine switches the debugger's current thread context to the thread running the specified goroutine
	SwitchGoroutine = "switchGoroutine"
D
Dan Mace 已提交
239 240 241
	// Halt suspends the process.
	Halt = "halt"
)
A
aarzilli 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

type AssemblyFlavour int

const (
	GNUFlavour   = AssemblyFlavour(proc.GNUFlavour)
	IntelFlavour = AssemblyFlavour(proc.IntelFlavour)
)

// AsmInstruction represents one assembly instruction at some address
type AsmInstruction struct {
	// Loc is the location of this instruction
	Loc Location
	// Destination of CALL instructions
	DestLoc *Location
	// Text is the formatted representation of the instruction
	Text string
	// Bytes is the instruction as read from memory
	Bytes []byte
	// If Breakpoint is true a breakpoint is set at this instruction
	Breakpoint bool
	// In AtPC is true this is the instruction the current thread is stopped at
	AtPC bool
}

type AsmInstructions []AsmInstruction