debugger.go 8.5 KB
Newer Older
D
Dan Mace 已提交
1 2 3 4 5 6 7
package debugger

import (
	"fmt"
	"log"
	"regexp"

D
Derek Parker 已提交
8
	"github.com/derekparker/delve/proc"
D
Dan Mace 已提交
9 10 11
	"github.com/derekparker/delve/service/api"
)

D
Derek Parker 已提交
12 13 14
// Debugger service.
//
// Debugger provides a higher level of
D
Derek Parker 已提交
15
// abstraction over proc.Process.
D
Derek Parker 已提交
16 17 18 19
// It handles converting from internal types to
// the types expected by clients. It also handles
// functionality needed by clients, but not needed in
// lower lever packages such as proc.
D
Dan Mace 已提交
20
type Debugger struct {
21
	config  *Config
D
Derek Parker 已提交
22
	process *proc.Process
D
Dan Mace 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
}

// Config provides the configuration to start a Debugger.
//
// Only one of ProcessArgs or AttachPid should be specified. If ProcessArgs is
// provided, a new process will be launched. Otherwise, the debugger will try
// to attach to an existing process with AttachPid.
type Config struct {
	// ProcessArgs are the arguments to launch a new process.
	ProcessArgs []string
	// AttachPid is the PID of an existing process to which the debugger should
	// attach.
	AttachPid int
}

// New creates a new Debugger.
39 40 41
func New(config *Config) (*Debugger, error) {
	d := &Debugger{
		config: config,
D
Dan Mace 已提交
42 43 44 45 46
	}

	// Create the process by either attaching or launching.
	if d.config.AttachPid > 0 {
		log.Printf("attaching to pid %d", d.config.AttachPid)
D
Derek Parker 已提交
47
		p, err := proc.Attach(d.config.AttachPid)
D
Dan Mace 已提交
48
		if err != nil {
49
			return nil, fmt.Errorf("could not attach to pid %d: %s", d.config.AttachPid, err)
D
Dan Mace 已提交
50 51 52 53
		}
		d.process = p
	} else {
		log.Printf("launching process with args: %v", d.config.ProcessArgs)
D
Derek Parker 已提交
54
		p, err := proc.Launch(d.config.ProcessArgs)
D
Dan Mace 已提交
55
		if err != nil {
56
			return nil, fmt.Errorf("could not launch process: %s", err)
D
Dan Mace 已提交
57 58 59
		}
		d.process = p
	}
60
	return d, nil
D
Dan Mace 已提交
61 62 63
}

func (d *Debugger) Detach(kill bool) error {
64
	return d.process.Detach(kill)
D
Dan Mace 已提交
65 66 67
}

func (d *Debugger) State() (*api.DebuggerState, error) {
68 69 70 71 72 73 74 75
	var (
		state  *api.DebuggerState
		thread *api.Thread
	)
	th := d.process.CurrentThread
	if th != nil {
		thread = api.ConvertThread(th)
	}
D
Dan Mace 已提交
76

77 78 79 80 81
	var breakpoint *api.Breakpoint
	bp := d.process.CurrentBreakpoint()
	if bp != nil {
		breakpoint = api.ConvertBreakpoint(bp)
	}
D
Dan Mace 已提交
82

83 84 85 86 87
	state = &api.DebuggerState{
		Breakpoint:    breakpoint,
		CurrentThread: thread,
		Exited:        d.process.Exited(),
	}
D
Dan Mace 已提交
88

89
	return state, nil
D
Dan Mace 已提交
90 91
}

D
Derek Parker 已提交
92 93
func (d *Debugger) CreateBreakpoint(requestedBp *api.Breakpoint) (*api.Breakpoint, error) {
	var createdBp *api.Breakpoint
94 95 96 97 98 99 100 101 102
	var loc string
	switch {
	case len(requestedBp.File) > 0:
		loc = fmt.Sprintf("%s:%d", requestedBp.File, requestedBp.Line)
	case len(requestedBp.FunctionName) > 0:
		loc = requestedBp.FunctionName
	default:
		return nil, fmt.Errorf("no file or function name specified")
	}
D
Dan Mace 已提交
103

104
	bp, err := d.process.SetBreakpointByLocation(loc)
105 106 107 108 109 110
	if err != nil {
		return nil, err
	}
	createdBp = api.ConvertBreakpoint(bp)
	log.Printf("created breakpoint: %#v", createdBp)
	return createdBp, nil
D
Dan Mace 已提交
111 112
}

D
Derek Parker 已提交
113 114
func (d *Debugger) ClearBreakpoint(requestedBp *api.Breakpoint) (*api.Breakpoint, error) {
	var clearedBp *api.Breakpoint
115
	bp, err := d.process.ClearBreakpoint(requestedBp.Addr)
116 117 118 119 120
	if err != nil {
		return nil, fmt.Errorf("Can't clear breakpoint @%x: %s", requestedBp.Addr, err)
	}
	clearedBp = api.ConvertBreakpoint(bp)
	log.Printf("cleared breakpoint: %#v", clearedBp)
D
Dan Mace 已提交
121 122 123
	return clearedBp, err
}

D
Derek Parker 已提交
124 125
func (d *Debugger) Breakpoints() []*api.Breakpoint {
	bps := []*api.Breakpoint{}
126 127 128
	for _, bp := range d.process.Breakpoints {
		if bp.Temp {
			continue
D
Dan Mace 已提交
129
		}
130 131
		bps = append(bps, api.ConvertBreakpoint(bp))
	}
D
Dan Mace 已提交
132 133 134
	return bps
}

D
Derek Parker 已提交
135 136
func (d *Debugger) FindBreakpoint(id int) *api.Breakpoint {
	for _, bp := range d.Breakpoints() {
D
Dan Mace 已提交
137 138 139 140 141 142 143 144 145
		if bp.ID == id {
			return bp
		}
	}
	return nil
}

func (d *Debugger) Threads() []*api.Thread {
	threads := []*api.Thread{}
146 147 148
	for _, th := range d.process.Threads {
		threads = append(threads, api.ConvertThread(th))
	}
D
Dan Mace 已提交
149 150 151 152 153 154 155 156 157 158 159 160
	return threads
}

func (d *Debugger) FindThread(id int) *api.Thread {
	for _, thread := range d.Threads() {
		if thread.ID == id {
			return thread
		}
	}
	return nil
}

D
Derek Parker 已提交
161
// Command handles commands which control the debugger lifecycle
D
Dan Mace 已提交
162 163 164 165
func (d *Debugger) Command(command *api.DebuggerCommand) (*api.DebuggerState, error) {
	var err error
	switch command.Name {
	case api.Continue:
166 167
		log.Print("continuing")
		err = d.process.Continue()
D
Dan Mace 已提交
168
	case api.Next:
169 170
		log.Print("nexting")
		err = d.process.Next()
D
Dan Mace 已提交
171
	case api.Step:
172 173
		log.Print("stepping")
		err = d.process.Step()
D
Dan Mace 已提交
174
	case api.SwitchThread:
175 176
		log.Printf("switching to thread %d", command.ThreadID)
		err = d.process.SwitchThread(command.ThreadID)
D
Dan Mace 已提交
177 178 179 180 181 182 183
	case api.Halt:
		// RequestManualStop does not invoke any ptrace syscalls, so it's safe to
		// access the process directly.
		log.Print("halting")
		err = d.process.RequestManualStop()
	}
	if err != nil {
D
Derek Parker 已提交
184
		return nil, err
D
Dan Mace 已提交
185 186 187 188 189 190 191 192 193 194 195
	}
	return d.State()
}

func (d *Debugger) Sources(filter string) ([]string, error) {
	regex, err := regexp.Compile(filter)
	if err != nil {
		return nil, fmt.Errorf("invalid filter argument: %s", err.Error())
	}

	files := []string{}
196 197 198
	for f := range d.process.Sources() {
		if regex.Match([]byte(f)) {
			files = append(files, f)
D
Dan Mace 已提交
199
		}
200
	}
D
Dan Mace 已提交
201 202 203 204 205 206 207 208 209 210
	return files, nil
}

func (d *Debugger) Functions(filter string) ([]string, error) {
	regex, err := regexp.Compile(filter)
	if err != nil {
		return nil, fmt.Errorf("invalid filter argument: %s", err.Error())
	}

	funcs := []string{}
211 212 213
	for _, f := range d.process.Funcs() {
		if f.Sym != nil && regex.Match([]byte(f.Name)) {
			funcs = append(funcs, f.Name)
D
Dan Mace 已提交
214
		}
215
	}
D
Dan Mace 已提交
216 217 218 219 220 221 222 223 224 225
	return funcs, nil
}

func (d *Debugger) PackageVariables(threadID int, filter string) ([]api.Variable, error) {
	regex, err := regexp.Compile(filter)
	if err != nil {
		return nil, fmt.Errorf("invalid filter argument: %s", err.Error())
	}

	vars := []api.Variable{}
226 227 228 229 230 231 232 233 234 235 236
	thread, found := d.process.Threads[threadID]
	if !found {
		return nil, fmt.Errorf("couldn't find thread %d", threadID)
	}
	pv, err := thread.PackageVariables()
	if err != nil {
		return nil, err
	}
	for _, v := range pv {
		if regex.Match([]byte(v.Name)) {
			vars = append(vars, api.ConvertVar(v))
D
Dan Mace 已提交
237
		}
238
	}
D
Dan Mace 已提交
239 240 241
	return vars, err
}

242 243 244 245 246 247 248 249 250 251 252 253
func (d *Debugger) Registers(threadID int) (string, error) {
	thread, found := d.process.Threads[threadID]
	if !found {
		return "", fmt.Errorf("couldn't find thread %d", threadID)
	}
	regs, err := thread.Registers()
	if err != nil {
		return "", err
	}
	return regs.String(), err
}

254 255
func (d *Debugger) LocalVariables(threadID int) ([]api.Variable, error) {
	vars := []api.Variable{}
256 257 258 259 260 261 262 263 264 265 266
	thread, found := d.process.Threads[threadID]
	if !found {
		return nil, fmt.Errorf("couldn't find thread %d", threadID)
	}
	pv, err := thread.LocalVariables()
	if err != nil {
		return nil, err
	}
	for _, v := range pv {
		vars = append(vars, api.ConvertVar(v))
	}
267 268 269 270 271
	return vars, err
}

func (d *Debugger) FunctionArguments(threadID int) ([]api.Variable, error) {
	vars := []api.Variable{}
272 273 274
	thread, found := d.process.Threads[threadID]
	if !found {
		return nil, fmt.Errorf("couldn't find thread %d", threadID)
D
Dan Mace 已提交
275
	}
276 277 278
	pv, err := thread.FunctionArguments()
	if err != nil {
		return nil, err
D
Dan Mace 已提交
279
	}
280 281
	for _, v := range pv {
		vars = append(vars, api.ConvertVar(v))
D
Dan Mace 已提交
282
	}
283
	return vars, err
D
Dan Mace 已提交
284 285
}

286 287 288 289 290 291 292 293
func (d *Debugger) EvalVariableInThread(threadID int, symbol string) (*api.Variable, error) {
	thread, found := d.process.Threads[threadID]
	if !found {
		return nil, fmt.Errorf("couldn't find thread %d", threadID)
	}
	v, err := thread.EvalVariable(symbol)
	if err != nil {
		return nil, err
D
Dan Mace 已提交
294
	}
295 296
	converted := api.ConvertVar(v)
	return &converted, err
D
Dan Mace 已提交
297 298
}

299 300 301 302 303
func (d *Debugger) Goroutines() ([]*api.Goroutine, error) {
	goroutines := []*api.Goroutine{}
	gs, err := d.process.GoroutinesInfo()
	if err != nil {
		return nil, err
D
Dan Mace 已提交
304
	}
305 306
	for _, g := range gs {
		goroutines = append(goroutines, api.ConvertGoroutine(g))
D
Dan Mace 已提交
307
	}
308
	return goroutines, err
D
Dan Mace 已提交
309
}
A
aarzilli 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

func (d *Debugger) Stacktrace(goroutineId, depth int) ([]api.Location, error) {
	var rawlocs []proc.Location
	var rawloc *proc.Location
	var err error

	if goroutineId < 0 {
		rawlocs, err = d.process.CurrentThread.Stacktrace(depth)
		if err != nil {
			return nil, err
		}
		rawloc, err = d.process.CurrentThread.Location()
		if err != nil {
			return nil, err
		}
	} else {
		gs, err := d.process.GoroutinesInfo()
		if err != nil {
			return nil, err
		}
		for _, g := range gs {
			if g.Id == goroutineId {
				rawlocs, err = d.process.GoroutineStacktrace(g, depth)
				if err != nil {
					return nil, err
				}
				rawloc = d.process.GoroutineLocation(g)
				break
			}
		}

		if rawlocs == nil {
			return nil, fmt.Errorf("Unknown goroutine id %d\n", goroutineId)
		}
	}

	locations := make([]api.Location, 0, len(rawlocs)+1)

	locations = append(locations, api.ConvertLocation(*rawloc))
	for i := range rawlocs {
		rawlocs[i].Line--
		locations = append(locations, api.ConvertLocation(rawlocs[i]))
	}

	return locations, nil
}