server.go 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
package rpc2

import (
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	grpc "net/rpc"
	"net/rpc/jsonrpc"

	"github.com/derekparker/delve/service"
	"github.com/derekparker/delve/service/api"
	"github.com/derekparker/delve/service/debugger"
)

type ServerImpl struct {
	s *RPCServer
}

type RPCServer struct {
	// config is all the information necessary to start the debugger and server.
	config *service.Config
	// listener is used to serve HTTP.
	listener net.Listener
	// stopChan is used to stop the listener goroutine
	stopChan chan struct{}
	// debugger is a debugger service.
	debugger *debugger.Debugger
}

// NewServer creates a new RPCServer.
func NewServer(config *service.Config, logEnabled bool) *ServerImpl {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
	if !logEnabled {
		log.SetOutput(ioutil.Discard)
	}

	return &ServerImpl{
		&RPCServer{
			config:   config,
			listener: config.Listener,
			stopChan: make(chan struct{}),
		},
	}
}

// Stop detaches from the debugger and waits for it to stop.
func (s *ServerImpl) Stop(kill bool) error {
	if s.s.config.AcceptMulti {
		close(s.s.stopChan)
		s.s.listener.Close()
	}
	err := s.s.debugger.Detach(kill)
	if err != nil {
		return err
	}
	return nil
}

// Run starts a debugger and exposes it with an HTTP server. The debugger
// itself can be stopped with the `detach` API. Run blocks until the HTTP
// server stops.
func (s *ServerImpl) Run() error {
	var err error
	// Create and start the debugger
	if s.s.debugger, err = debugger.New(&debugger.Config{
		ProcessArgs: s.s.config.ProcessArgs,
		AttachPid:   s.s.config.AttachPid,
	}); err != nil {
		return err
	}

	rpcs := grpc.NewServer()
	rpcs.Register(s.s)

	go func() {
		defer s.s.listener.Close()
		for {
			c, err := s.s.listener.Accept()
			if err != nil {
				select {
				case <-s.s.stopChan:
					// We were supposed to exit, do nothing and return
					return
				default:
					panic(err)
				}
			}
			go rpcs.ServeCodec(jsonrpc.NewServerCodec(c))
			if !s.s.config.AcceptMulti {
				break
			}
		}
	}()
	return nil
}

func (s *ServerImpl) Restart() error {
	return s.s.Restart(RestartIn{}, nil)
}

type ProcessPidIn struct {
}

type ProcessPidOut struct {
	Pid int
}

110
// ProcessPid returns the pid of the process we are debugging.
111 112 113 114 115 116 117 118 119 120 121 122
func (s *RPCServer) ProcessPid(arg ProcessPidIn, out *ProcessPidOut) error {
	out.Pid = s.debugger.ProcessPid()
	return nil
}

type DetachIn struct {
	Kill bool
}

type DetachOut struct {
}

123
// Detach detaches the debugger, optionally killing the process.
124 125 126 127 128 129 130 131 132 133
func (s *RPCServer) Detach(arg DetachIn, out *DetachOut) error {
	return s.debugger.Detach(arg.Kill)
}

type RestartIn struct {
}

type RestartOut struct {
}

134
// Restart restarts program.
135 136 137 138 139 140 141 142 143 144 145 146 147 148
func (s *RPCServer) Restart(arg RestartIn, out *RestartOut) error {
	if s.config.AttachPid != 0 {
		return errors.New("cannot restart process Delve did not create")
	}
	return s.debugger.Restart()
}

type StateIn struct {
}

type StateOut struct {
	State *api.DebuggerState
}

149
// State returns the current debugger state.
150 151 152 153 154 155 156 157 158 159 160 161 162
func (s *RPCServer) State(arg StateIn, out *StateOut) error {
	st, err := s.debugger.State()
	if err != nil {
		return err
	}
	out.State = st
	return nil
}

type CommandOut struct {
	State api.DebuggerState
}

163
// Command interrupts, continues and steps through the program.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
func (s *RPCServer) Command(command api.DebuggerCommand, out *CommandOut) error {
	st, err := s.debugger.Command(&command)
	if err != nil {
		return err
	}
	out.State = *st
	return nil
}

type GetBreakpointIn struct {
	Id   int
	Name string
}

type GetBreakpointOut struct {
	Breakpoint api.Breakpoint
}

182
// GetBreakpoint gets a breakpoint by Name (if Name is not an empty string) or by ID.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
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 {
	Id    int
	Depth int
	Full  bool
204
	Cfg *api.LoadConfig
205 206 207 208 209 210
}

type StacktraceOut struct {
	Locations []api.Stackframe
}

211 212 213 214
// 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.
215
func (s *RPCServer) Stacktrace(arg StacktraceIn, out *StacktraceOut) error {
216 217 218 219 220
	cfg := arg.Cfg
	if cfg == nil && arg.Full {
		cfg = &api.LoadConfig{ true, 1, 64, 64, -1 }
	}
	locs, err := s.debugger.Stacktrace(arg.Id, arg.Depth, api.LoadConfigToProc(cfg))
221 222 223 224 225 226 227 228 229 230 231 232 233 234
	if err != nil {
		return err
	}
	out.Locations = locs
	return nil
}

type ListBreakpointsIn struct {
}

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

235
// ListBreakpoints gets all breakpoints.
236 237 238 239 240 241 242 243 244 245 246 247 248
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
}

249 250 251 252 253 254 255 256 257 258 259 260
// 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
// location. Note that setting a breakpoint on a function's entry point
// (line == 0) can have surprising consequences, it is advisable to
// use line = -1 instead which will skip the prologue.
//
// - Otherwise the value specified by arg.Breakpoint.Addr will be used.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
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
}

279 280
// ClearBreakpoint deletes a breakpoint by Name (if Name is not an
// empty string) or by ID.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
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 {
}

309 310 311 312 313
// 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
314 315 316 317 318 319 320 321 322 323 324
func (s *RPCServer) AmendBreakpoint(arg AmendBreakpointIn, out *AmendBreakpointOut) error {
	return s.debugger.AmendBreakpoint(&arg.Breakpoint)
}

type ListThreadsIn struct {
}

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

325
// ListThreads lists all threads.
326 327 328 329 330 331 332 333 334 335 336 337 338
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
}

339
// GetThread gets a thread by its ID.
340 341 342 343 344 345 346 347 348 349 350 351 352 353
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
354
	Cfg api.LoadConfig
355 356 357 358 359 360
}

type ListPackageVarsOut struct {
	Variables []api.Variable
}

361
// ListPackageVars lists all package variables in the context of the current thread.
362 363 364 365 366 367 368 369 370 371 372
func (s *RPCServer) ListPackageVars(arg ListPackageVarsIn, out *ListPackageVarsOut) error {
	state, err := s.debugger.State()
	if err != nil {
		return err
	}

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

373
	vars, err := s.debugger.PackageVariables(current.ID, arg.Filter, *api.LoadConfigToProc(&arg.Cfg))
374 375 376 377 378 379 380 381 382 383 384 385 386 387
	if err != nil {
		return err
	}
	out.Variables = vars
	return nil
}

type ListRegistersIn struct {
}

type ListRegistersOut struct {
	Registers string
}

388
// ListRegisters lists registers and their values.
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
func (s *RPCServer) ListRegisters(arg ListRegistersIn, out *ListRegistersOut) error {
	state, err := s.debugger.State()
	if err != nil {
		return err
	}

	regs, err := s.debugger.Registers(state.CurrentThread.ID)
	if err != nil {
		return err
	}
	out.Registers = regs
	return nil
}

type ListLocalVarsIn struct {
	Scope api.EvalScope
405
	Cfg api.LoadConfig
406 407 408 409 410 411
}

type ListLocalVarsOut struct {
	Variables []api.Variable
}

412
// ListLocalVars lists all local variables in scope.
413
func (s *RPCServer) ListLocalVars(arg ListLocalVarsIn, out *ListLocalVarsOut) error {
414
	vars, err := s.debugger.LocalVariables(arg.Scope, *api.LoadConfigToProc(&arg.Cfg))
415 416 417 418 419 420 421 422 423
	if err != nil {
		return err
	}
	out.Variables = vars
	return nil
}

type ListFunctionArgsIn struct {
	Scope api.EvalScope
424
	Cfg api.LoadConfig
425 426 427 428 429 430
}

type ListFunctionArgsOut struct {
	Args []api.Variable
}

431
// ListFunctionArgs lists all arguments to the current function
432
func (s *RPCServer) ListFunctionArgs(arg ListFunctionArgsIn, out *ListFunctionArgsOut) error {
433
	vars, err := s.debugger.FunctionArguments(arg.Scope, *api.LoadConfigToProc(&arg.Cfg))
434 435 436 437 438 439 440 441 442 443
	if err != nil {
		return err
	}
	out.Args = vars
	return nil
}

type EvalIn struct {
	Scope api.EvalScope
	Expr  string
444
	Cfg *api.LoadConfig
445 446 447 448 449 450
}

type EvalOut struct {
	Variable *api.Variable
}

451 452 453 454
// EvalVariable returns a variable in the specified context.
//
// See https://github.com/derekparker/delve/wiki/Expressions for
// a description of acceptable values of arg.Expr.
455
func (s *RPCServer) Eval(arg EvalIn, out *EvalOut) error {
456 457 458 459 460
	cfg := arg.Cfg
	if cfg == nil {
		cfg = &api.LoadConfig{ true, 1, 64, 64, -1 }
	}
	v, err := s.debugger.EvalVariableInScope(arg.Scope, arg.Expr, *api.LoadConfigToProc(cfg))
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
	if err != nil {
		return err
	}
	out.Variable = v
	return nil
}

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

type SetOut struct {
}

477 478
// Set sets the value of a variable. Only numerical types and
// pointers are currently supported.
479 480 481 482 483 484 485 486 487 488 489 490
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
}

491
// ListSources lists all source files in the process matching filter.
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
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
}

509
// ListFunctions lists all functions in the process matching filter.
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
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
}

527
// ListTypes lists all types in the process matching filter.
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
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 {
}

type ListGoroutinesOut struct {
	Goroutines []*api.Goroutine
}

544
// ListGoroutines lists all goroutines.
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
func (s *RPCServer) ListGoroutines(arg ListGoroutinesIn, out *ListGoroutinesOut) error {
	gs, err := s.debugger.Goroutines()
	if err != nil {
		return err
	}
	out.Goroutines = gs
	return nil
}

type AttachedToExistingProcessIn struct {
}

type AttachedToExistingProcessOut struct {
	Answer bool
}

561
// AttachedToExistingProcess returns whether we attached to a running process or not
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
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
}

578 579 580 581 582 583 584 585 586 587 588 589 590
// 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.
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
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
}

607 608 609 610 611 612 613
// Disassemble code.
//
// If both StartPC and EndPC are non-zero the specified range will be disassembled, otherwise the function containing StartPC will be disassembled.
//
// Scope is used to mark the instruction the specified gorutine is stopped at.
//
// 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.
614 615 616 617 618
func (c *RPCServer) Disassemble(arg DisassembleIn, out *DisassembleOut) error {
	var err error
	out.Disassemble, err = c.debugger.Disassemble(arg.Scope, arg.StartPC, arg.EndPC, arg.Flavour)
	return err
}