server.go 17.6 KB
Newer Older
1 2 3 4 5
package rpc2

import (
	"errors"
	"fmt"
6
	"time"
7

8 9 10
	"github.com/go-delve/delve/service"
	"github.com/go-delve/delve/service/api"
	"github.com/go-delve/delve/service/debugger"
11 12 13 14 15 16 17 18 19
)

type RPCServer struct {
	// config is all the information necessary to start the debugger and server.
	config *service.Config
	// debugger is a debugger service.
	debugger *debugger.Debugger
}

A
aarzilli 已提交
20 21
func NewServer(config *service.Config, debugger *debugger.Debugger) *RPCServer {
	return &RPCServer{config, debugger}
22 23 24 25 26 27 28 29 30
}

type ProcessPidIn struct {
}

type ProcessPidOut struct {
	Pid int
}

31
// ProcessPid returns the pid of the process we are debugging.
32 33 34 35 36
func (s *RPCServer) ProcessPid(arg ProcessPidIn, out *ProcessPidOut) error {
	out.Pid = s.debugger.ProcessPid()
	return nil
}

37 38 39 40 41 42 43 44 45 46 47 48
type LastModifiedIn struct {
}

type LastModifiedOut struct {
	Time time.Time
}

func (s *RPCServer) LastModified(arg LastModifiedIn, out *LastModifiedOut) error {
	out.Time = s.debugger.LastModified()
	return nil
}

49 50 51 52 53 54 55
type DetachIn struct {
	Kill bool
}

type DetachOut struct {
}

56
// Detach detaches the debugger, optionally killing the process.
57
func (s *RPCServer) Detach(arg DetachIn, out *DetachOut) error {
58 59 60 61 62 63
	err := s.debugger.Detach(arg.Kill)
	if s.config.DisconnectChan != nil {
		close(s.config.DisconnectChan)
		s.config.DisconnectChan = nil
	}
	return err
64 65 66
}

type RestartIn struct {
67 68 69
	// Position to restart from, if it starts with 'c' it's a checkpoint ID,
	// otherwise it's an event number. Only valid for recorded targets.
	Position string
70 71 72 73 74 75

	// ResetArgs tell whether NewArgs should take effect.
	ResetArgs bool
	// NewArgs are arguments to launch a new process.  They replace only the
	// argv[1] and later. Argv[0] cannot be changed.
	NewArgs []string
76 77 78

	// When Rerecord is set the target will be rerecorded
	Rerecord bool
79 80 81
}

type RestartOut struct {
82
	DiscardedBreakpoints []api.DiscardedBreakpoint
83 84
}

85
// Restart restarts program.
86 87 88 89
func (s *RPCServer) Restart(arg RestartIn, out *RestartOut) error {
	if s.config.AttachPid != 0 {
		return errors.New("cannot restart process Delve did not create")
	}
90
	var err error
91
	out.DiscardedBreakpoints, err = s.debugger.Restart(arg.Rerecord, arg.Position, arg.ResetArgs, arg.NewArgs)
92
	return err
93 94 95
}

type StateIn struct {
96 97
	// If NonBlocking is true State will return immediately even if the target process is running.
	NonBlocking bool
98 99 100 101 102 103
}

type StateOut struct {
	State *api.DebuggerState
}

104
// State returns the current debugger state.
105
func (s *RPCServer) State(arg StateIn, out *StateOut) error {
106
	st, err := s.debugger.State(arg.NonBlocking)
107 108 109 110 111 112 113 114 115 116 117
	if err != nil {
		return err
	}
	out.State = st
	return nil
}

type CommandOut struct {
	State api.DebuggerState
}

118
// Command interrupts, continues and steps through the program.
A
aarzilli 已提交
119
func (s *RPCServer) Command(command api.DebuggerCommand, cb service.RPCCallback) {
120 121
	st, err := s.debugger.Command(&command)
	if err != nil {
A
aarzilli 已提交
122 123
		cb.Return(nil, err)
		return
124
	}
A
aarzilli 已提交
125
	var out CommandOut
126
	out.State = *st
A
aarzilli 已提交
127
	cb.Return(out, nil)
128 129 130 131 132 133 134 135 136 137 138
}

type GetBreakpointIn struct {
	Id   int
	Name string
}

type GetBreakpointOut struct {
	Breakpoint api.Breakpoint
}

139
// GetBreakpoint gets a breakpoint by Name (if Name is not an empty string) or by ID.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
func (s *RPCServer) GetBreakpoint(arg GetBreakpointIn, out *GetBreakpointOut) error {
	var bp *api.Breakpoint
	if arg.Name != "" {
		bp = s.debugger.FindBreakpointByName(arg.Name)
		if bp == nil {
			return fmt.Errorf("no breakpoint with name %s", arg.Name)
		}
	} else {
		bp = s.debugger.FindBreakpoint(arg.Id)
		if bp == nil {
			return fmt.Errorf("no breakpoint with id %d", arg.Id)
		}
	}
	out.Breakpoint = *bp
	return nil
}

type StacktraceIn struct {
158 159 160
	Id     int
	Depth  int
	Full   bool
161 162
	Defers bool // read deferred functions (equivalent to passing StacktraceReadDefers in Opts)
	Opts   api.StacktraceOptions
163
	Cfg    *api.LoadConfig
164 165 166 167 168 169
}

type StacktraceOut struct {
	Locations []api.Stackframe
}

170 171 172 173
// Stacktrace returns stacktrace of goroutine Id up to the specified Depth.
//
// If Full is set it will also the variable of all local variables
// and function arguments of all stack frames.
174
func (s *RPCServer) Stacktrace(arg StacktraceIn, out *StacktraceOut) error {
175 176
	cfg := arg.Cfg
	if cfg == nil && arg.Full {
A
aarzilli 已提交
177
		cfg = &api.LoadConfig{true, 1, 64, 64, -1}
178
	}
179 180 181
	if arg.Defers {
		arg.Opts |= api.StacktraceReadDefers
	}
182
	var err error
183
	out.Locations, err = s.debugger.Stacktrace(arg.Id, arg.Depth, arg.Opts, api.LoadConfigToProc(cfg))
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	return err
}

type AncestorsIn struct {
	GoroutineID  int
	NumAncestors int
	Depth        int
}

type AncestorsOut struct {
	Ancestors []api.Ancestor
}

// Ancestors returns the stacktraces for the ancestors of a goroutine.
func (s *RPCServer) Ancestors(arg AncestorsIn, out *AncestorsOut) error {
	var err error
	out.Ancestors, err = s.debugger.Ancestors(arg.GoroutineID, arg.NumAncestors, arg.Depth)
	return err
202 203 204 205 206 207 208 209 210
}

type ListBreakpointsIn struct {
}

type ListBreakpointsOut struct {
	Breakpoints []*api.Breakpoint
}

211
// ListBreakpoints gets all breakpoints.
212 213 214 215 216 217 218 219 220 221 222 223 224
func (s *RPCServer) ListBreakpoints(arg ListBreakpointsIn, out *ListBreakpointsOut) error {
	out.Breakpoints = s.debugger.Breakpoints()
	return nil
}

type CreateBreakpointIn struct {
	Breakpoint api.Breakpoint
}

type CreateBreakpointOut struct {
	Breakpoint api.Breakpoint
}

225 226 227 228 229 230 231
// CreateBreakpoint creates a new breakpoint.
//
// - If arg.Breakpoint.File is not an empty string the breakpoint
// will be created on the specified file:line location
//
// - If arg.Breakpoint.FunctionName is not an empty string
// the breakpoint will be created on the specified function:line
232
// location.
233 234
//
// - Otherwise the value specified by arg.Breakpoint.Addr will be used.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
func (s *RPCServer) CreateBreakpoint(arg CreateBreakpointIn, out *CreateBreakpointOut) error {
	createdbp, err := s.debugger.CreateBreakpoint(&arg.Breakpoint)
	if err != nil {
		return err
	}
	out.Breakpoint = *createdbp
	return nil
}

type ClearBreakpointIn struct {
	Id   int
	Name string
}

type ClearBreakpointOut struct {
	Breakpoint *api.Breakpoint
}

253 254
// ClearBreakpoint deletes a breakpoint by Name (if Name is not an
// empty string) or by ID.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
func (s *RPCServer) ClearBreakpoint(arg ClearBreakpointIn, out *ClearBreakpointOut) error {
	var bp *api.Breakpoint
	if arg.Name != "" {
		bp = s.debugger.FindBreakpointByName(arg.Name)
		if bp == nil {
			return fmt.Errorf("no breakpoint with name %s", arg.Name)
		}
	} else {
		bp = s.debugger.FindBreakpoint(arg.Id)
		if bp == nil {
			return fmt.Errorf("no breakpoint with id %d", arg.Id)
		}
	}
	deleted, err := s.debugger.ClearBreakpoint(bp)
	if err != nil {
		return err
	}
	out.Breakpoint = deleted
	return nil
}

type AmendBreakpointIn struct {
	Breakpoint api.Breakpoint
}

type AmendBreakpointOut struct {
}

283 284 285 286 287
// AmendBreakpoint allows user to update an existing breakpoint
// for example to change the information retrieved when the
// breakpoint is hit or to change, add or remove the break condition.
//
// arg.Breakpoint.ID must be a valid breakpoint ID
288 289 290 291
func (s *RPCServer) AmendBreakpoint(arg AmendBreakpointIn, out *AmendBreakpointOut) error {
	return s.debugger.AmendBreakpoint(&arg.Breakpoint)
}

292 293 294 295 296 297 298 299 300 301
type CancelNextIn struct {
}

type CancelNextOut struct {
}

func (s *RPCServer) CancelNext(arg CancelNextIn, out *CancelNextOut) error {
	return s.debugger.CancelNext()
}

302 303 304 305 306 307 308
type ListThreadsIn struct {
}

type ListThreadsOut struct {
	Threads []*api.Thread
}

309
// ListThreads lists all threads.
310 311 312 313 314 315 316 317 318 319 320 321 322
func (s *RPCServer) ListThreads(arg ListThreadsIn, out *ListThreadsOut) (err error) {
	out.Threads, err = s.debugger.Threads()
	return err
}

type GetThreadIn struct {
	Id int
}

type GetThreadOut struct {
	Thread *api.Thread
}

323
// GetThread gets a thread by its ID.
324 325 326 327 328 329 330 331 332 333 334 335 336 337
func (s *RPCServer) GetThread(arg GetThreadIn, out *GetThreadOut) error {
	t, err := s.debugger.FindThread(arg.Id)
	if err != nil {
		return err
	}
	if t == nil {
		return fmt.Errorf("no thread with id %d", arg.Id)
	}
	out.Thread = t
	return nil
}

type ListPackageVarsIn struct {
	Filter string
A
aarzilli 已提交
338
	Cfg    api.LoadConfig
339 340 341 342 343 344
}

type ListPackageVarsOut struct {
	Variables []api.Variable
}

345
// ListPackageVars lists all package variables in the context of the current thread.
346
func (s *RPCServer) ListPackageVars(arg ListPackageVarsIn, out *ListPackageVarsOut) error {
347
	state, err := s.debugger.State(false)
348 349 350 351 352 353 354 355 356
	if err != nil {
		return err
	}

	current := state.CurrentThread
	if current == nil {
		return fmt.Errorf("no current thread")
	}

357
	vars, err := s.debugger.PackageVariables(current.ID, arg.Filter, *api.LoadConfigToProc(&arg.Cfg))
358 359 360 361 362 363 364 365
	if err != nil {
		return err
	}
	out.Variables = vars
	return nil
}

type ListRegistersIn struct {
A
aarzilli 已提交
366 367
	ThreadID  int
	IncludeFp bool
368 369 370 371
}

type ListRegistersOut struct {
	Registers string
A
aarzilli 已提交
372
	Regs      api.Registers
373 374
}

375
// ListRegisters lists registers and their values.
376
func (s *RPCServer) ListRegisters(arg ListRegistersIn, out *ListRegistersOut) error {
A
aarzilli 已提交
377
	if arg.ThreadID == 0 {
378
		state, err := s.debugger.State(false)
A
aarzilli 已提交
379 380 381 382
		if err != nil {
			return err
		}
		arg.ThreadID = state.CurrentThread.ID
383 384
	}

A
aarzilli 已提交
385
	regs, err := s.debugger.Registers(arg.ThreadID, arg.IncludeFp)
386 387 388
	if err != nil {
		return err
	}
A
aarzilli 已提交
389 390 391
	out.Regs = regs
	out.Registers = out.Regs.String()

392 393 394 395 396
	return nil
}

type ListLocalVarsIn struct {
	Scope api.EvalScope
A
aarzilli 已提交
397
	Cfg   api.LoadConfig
398 399 400 401 402 403
}

type ListLocalVarsOut struct {
	Variables []api.Variable
}

404
// ListLocalVars lists all local variables in scope.
405
func (s *RPCServer) ListLocalVars(arg ListLocalVarsIn, out *ListLocalVarsOut) error {
406
	vars, err := s.debugger.LocalVariables(arg.Scope, *api.LoadConfigToProc(&arg.Cfg))
407 408 409 410 411 412 413 414 415
	if err != nil {
		return err
	}
	out.Variables = vars
	return nil
}

type ListFunctionArgsIn struct {
	Scope api.EvalScope
A
aarzilli 已提交
416
	Cfg   api.LoadConfig
417 418 419 420 421 422
}

type ListFunctionArgsOut struct {
	Args []api.Variable
}

423
// ListFunctionArgs lists all arguments to the current function
424
func (s *RPCServer) ListFunctionArgs(arg ListFunctionArgsIn, out *ListFunctionArgsOut) error {
425
	vars, err := s.debugger.FunctionArguments(arg.Scope, *api.LoadConfigToProc(&arg.Cfg))
426 427 428 429 430 431 432 433 434 435
	if err != nil {
		return err
	}
	out.Args = vars
	return nil
}

type EvalIn struct {
	Scope api.EvalScope
	Expr  string
A
aarzilli 已提交
436
	Cfg   *api.LoadConfig
437 438 439 440 441 442
}

type EvalOut struct {
	Variable *api.Variable
}

443 444
// EvalVariable returns a variable in the specified context.
//
445
// See https://github.com/go-delve/delve/wiki/Expressions for
446
// a description of acceptable values of arg.Expr.
447
func (s *RPCServer) Eval(arg EvalIn, out *EvalOut) error {
448 449
	cfg := arg.Cfg
	if cfg == nil {
A
aarzilli 已提交
450
		cfg = &api.LoadConfig{true, 1, 64, 64, -1}
451 452
	}
	v, err := s.debugger.EvalVariableInScope(arg.Scope, arg.Expr, *api.LoadConfigToProc(cfg))
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
	if err != nil {
		return err
	}
	out.Variable = v
	return nil
}

type SetIn struct {
	Scope  api.EvalScope
	Symbol string
	Value  string
}

type SetOut struct {
}

469 470
// Set sets the value of a variable. Only numerical types and
// pointers are currently supported.
471 472 473 474 475 476 477 478 479 480 481 482
func (s *RPCServer) Set(arg SetIn, out *SetOut) error {
	return s.debugger.SetVariableInScope(arg.Scope, arg.Symbol, arg.Value)
}

type ListSourcesIn struct {
	Filter string
}

type ListSourcesOut struct {
	Sources []string
}

483
// ListSources lists all source files in the process matching filter.
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
func (s *RPCServer) ListSources(arg ListSourcesIn, out *ListSourcesOut) error {
	ss, err := s.debugger.Sources(arg.Filter)
	if err != nil {
		return err
	}
	out.Sources = ss
	return nil
}

type ListFunctionsIn struct {
	Filter string
}

type ListFunctionsOut struct {
	Funcs []string
}

501
// ListFunctions lists all functions in the process matching filter.
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
func (s *RPCServer) ListFunctions(arg ListFunctionsIn, out *ListFunctionsOut) error {
	fns, err := s.debugger.Functions(arg.Filter)
	if err != nil {
		return err
	}
	out.Funcs = fns
	return nil
}

type ListTypesIn struct {
	Filter string
}

type ListTypesOut struct {
	Types []string
}

519
// ListTypes lists all types in the process matching filter.
520 521 522 523 524 525 526 527 528 529
func (s *RPCServer) ListTypes(arg ListTypesIn, out *ListTypesOut) error {
	tps, err := s.debugger.Types(arg.Filter)
	if err != nil {
		return err
	}
	out.Types = tps
	return nil
}

type ListGoroutinesIn struct {
530 531
	Start int
	Count int
532 533 534 535
}

type ListGoroutinesOut struct {
	Goroutines []*api.Goroutine
536
	Nextg      int
537 538
}

539
// ListGoroutines lists all goroutines.
540 541 542 543 544
// If Count is specified ListGoroutines will return at the first Count
// goroutines and an index in Nextg, that can be passed as the Start
// parameter, to get more goroutines from ListGoroutines.
// Passing a value of Start that wasn't returned by ListGoroutines will skip
// an undefined number of goroutines.
545
func (s *RPCServer) ListGoroutines(arg ListGoroutinesIn, out *ListGoroutinesOut) error {
546
	gs, nextg, err := s.debugger.Goroutines(arg.Start, arg.Count)
547 548 549 550
	if err != nil {
		return err
	}
	out.Goroutines = gs
551
	out.Nextg = nextg
552 553 554 555 556 557 558 559 560 561
	return nil
}

type AttachedToExistingProcessIn struct {
}

type AttachedToExistingProcessOut struct {
	Answer bool
}

562
// AttachedToExistingProcess returns whether we attached to a running process or not
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
func (c *RPCServer) AttachedToExistingProcess(arg AttachedToExistingProcessIn, out *AttachedToExistingProcessOut) error {
	if c.config.AttachPid != 0 {
		out.Answer = true
	}
	return nil
}

type FindLocationIn struct {
	Scope api.EvalScope
	Loc   string
}

type FindLocationOut struct {
	Locations []api.Location
}

579 580 581 582 583 584 585 586 587 588 589 590 591
// FindLocation returns concrete location information described by a location expression
//
//  loc ::= <filename>:<line> | <function>[:<line>] | /<regex>/ | (+|-)<offset> | <line> | *<address>
//  * <filename> can be the full path of a file or just a suffix
//  * <function> ::= <package>.<receiver type>.<name> | <package>.(*<receiver type>).<name> | <receiver type>.<name> | <package>.<name> | (*<receiver type>).<name> | <name>
//  * <function> must be unambiguous
//  * /<regex>/ will return a location for each function matched by regex
//  * +<offset> returns a location for the line that is <offset> lines after the current line
//  * -<offset> returns a location for the line that is <offset> lines before the current line
//  * <line> returns a location for a line in the current file
//  * *<address> returns the location corresponding to the specified address
//
// NOTE: this function does not actually set breakpoints.
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
func (c *RPCServer) FindLocation(arg FindLocationIn, out *FindLocationOut) error {
	var err error
	out.Locations, err = c.debugger.FindLocation(arg.Scope, arg.Loc)
	return err
}

type DisassembleIn struct {
	Scope          api.EvalScope
	StartPC, EndPC uint64
	Flavour        api.AssemblyFlavour
}

type DisassembleOut struct {
	Disassemble api.AsmInstructions
}

608 609 610 611
// Disassemble code.
//
// If both StartPC and EndPC are non-zero the specified range will be disassembled, otherwise the function containing StartPC will be disassembled.
//
J
Josh Soref 已提交
612
// Scope is used to mark the instruction the specified goroutine is stopped at.
613 614
//
// Disassemble will also try to calculate the destination address of an absolute indirect CALL if it happens to be the instruction the selected goroutine is stopped at.
615 616
func (c *RPCServer) Disassemble(arg DisassembleIn, out *DisassembleOut) error {
	var err error
D
Derek Parker 已提交
617
	out.Disassemble, err = c.debugger.Disassemble(arg.Scope.GoroutineID, arg.StartPC, arg.EndPC, arg.Flavour)
618 619
	return err
}
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

type RecordedIn struct {
}

type RecordedOut struct {
	Recorded       bool
	TraceDirectory string
}

func (s *RPCServer) Recorded(arg RecordedIn, out *RecordedOut) error {
	out.Recorded, out.TraceDirectory = s.debugger.Recorded()
	return nil
}

type CheckpointIn struct {
	Where string
}

type CheckpointOut struct {
	ID int
}

func (s *RPCServer) Checkpoint(arg CheckpointIn, out *CheckpointOut) error {
	var err error
	out.ID, err = s.debugger.Checkpoint(arg.Where)
	return err
}

type ListCheckpointsIn struct {
}

type ListCheckpointsOut struct {
	Checkpoints []api.Checkpoint
}

func (s *RPCServer) ListCheckpoints(arg ListCheckpointsIn, out *ListCheckpointsOut) error {
	var err error
	out.Checkpoints, err = s.debugger.Checkpoints()
	return err
}

type ClearCheckpointIn struct {
	ID int
}

type ClearCheckpointOut struct {
}

func (s *RPCServer) ClearCheckpoint(arg ClearCheckpointIn, out *ClearCheckpointOut) error {
	return s.debugger.ClearCheckpoint(arg.ID)
}
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

type IsMulticlientIn struct {
}

type IsMulticlientOut struct {
	// IsMulticlient returns true if the headless instance was started with --accept-multiclient
	IsMulticlient bool
}

func (s *RPCServer) IsMulticlient(arg IsMulticlientIn, out *IsMulticlientOut) error {
	*out = IsMulticlientOut{
		IsMulticlient: s.config.AcceptMulti,
	}
	return nil
}
D
Derek Parker 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

// FunctionReturnLocationsIn holds arguments for the
// FunctionReturnLocationsRPC call. It holds the name of
// the function for which all return locations should be
// given.
type FunctionReturnLocationsIn struct {
	// FnName is the name of the function for which all
	// return locations should be given.
	FnName string
}

// FunctionReturnLocationsOut holds the result of the FunctionReturnLocations
// RPC call. It provides the list of addresses that the given function returns,
// for example with a `RET` instruction or `CALL runtime.deferreturn`.
type FunctionReturnLocationsOut struct {
	// Addrs is the list of all locations where the given function returns.
	Addrs []uint64
}

// FunctionReturnLocations is the implements the client call of the same name. Look at client documentation for more information.
func (s *RPCServer) FunctionReturnLocations(in FunctionReturnLocationsIn, out *FunctionReturnLocationsOut) error {
	addrs, err := s.debugger.FunctionReturnLocations(in.FnName)
	if err != nil {
		return err
	}
	*out = FunctionReturnLocationsOut{
		Addrs: addrs,
	}
	return nil
}
716 717 718 719 720 721 722 723 724 725 726 727 728 729

// ListDynamicLibrariesIn holds the arguments of ListDynamicLibraries
type ListDynamicLibrariesIn struct {
}

// ListDynamicLibrariesOut holds the return values of ListDynamicLibraries
type ListDynamicLibrariesOut struct {
	List []api.Image
}

func (s *RPCServer) ListDynamicLibraries(in ListDynamicLibrariesIn, out *ListDynamicLibrariesOut) error {
	out.List = s.debugger.ListDynamicLibraries()
	return nil
}