command.go 5.4 KB
Newer Older
D
Derek Parker 已提交
1 2
// Package command implements functions for responding to user
// input and dispatching to appropriate backend commands.
D
Derek Parker 已提交
3 4 5
package command

import (
6
	"bufio"
7
	"debug/gosym"
D
Derek Parker 已提交
8
	"fmt"
9 10
	"io"
	"os"
11 12 13 14
	"path/filepath"
	"strconv"
	"strings"

D
Derek Parker 已提交
15
	"github.com/derekparker/delve/proctl"
D
Derek Parker 已提交
16 17
)

18
type cmdfunc func(proc *proctl.DebuggedProcess, args ...string) error
D
Derek Parker 已提交
19 20 21 22 23

type Commands struct {
	cmds map[string]cmdfunc
}

24
// Returns a Commands struct with default commands defined.
D
Derek Parker 已提交
25 26
func DebugCommands() *Commands {
	cmds := map[string]cmdfunc{
27 28 29 30 31 32 33 34 35 36
		"help":       help,
		"continue":   cont,
		"next":       next,
		"break":      breakpoint,
		"step":       step,
		"clear":      clear,
		"print":      printVar,
		"threads":    threads,
		"goroutines": goroutines,
		"":           nullCommand,
D
Derek Parker 已提交
37 38 39 40 41
	}

	return &Commands{cmds}
}

D
Derek Parker 已提交
42 43
// Register custom commands. Expects cf to be a func of type cmdfunc,
// returning only an error.
D
Derek Parker 已提交
44 45 46 47
func (c *Commands) Register(cmdstr string, cf cmdfunc) {
	c.cmds[cmdstr] = cf
}

48 49 50
// Find will look up the command function for the given command input.
// If it cannot find the command it will defualt to noCmdAvailable().
// If the command is an empty string it will replay the last command.
D
Derek Parker 已提交
51 52 53 54 55 56
func (c *Commands) Find(cmdstr string) cmdfunc {
	cmd, ok := c.cmds[cmdstr]
	if !ok {
		return noCmdAvailable
	}

57 58 59
	// Allow <enter> to replay last command
	c.cmds[""] = cmd

D
Derek Parker 已提交
60 61 62
	return cmd
}

D
Derek Parker 已提交
63
func CommandFunc(fn func() error) cmdfunc {
64
	return func(p *proctl.DebuggedProcess, args ...string) error {
D
Derek Parker 已提交
65 66 67 68
		return fn()
	}
}

69
func noCmdAvailable(p *proctl.DebuggedProcess, ars ...string) error {
D
Derek Parker 已提交
70 71 72
	return fmt.Errorf("command not available")
}

73 74 75 76
func nullCommand(p *proctl.DebuggedProcess, ars ...string) error {
	return nil
}

D
Derek Parker 已提交
77 78 79 80 81 82
func help(p *proctl.DebuggedProcess, ars ...string) error {
	fmt.Println(`The following commands are available:
    break - Set break point at the entry point of a function, or at a specific file/line. Example: break foo.go:13.
    continue - Run until breakpoint or program termination.
    step - Single step through program.
    next - Step over to next source line.
D
Derek Parker 已提交
83
    threads - Print out info for every traced thread.
D
Derek Parker 已提交
84
    goroutines - Print out info for every goroutine.
M
Matt Self 已提交
85 86
    print $var - Evaluate a variable.
    exit - Exit the debugger.`)
D
Derek Parker 已提交
87 88 89

	return nil
}
90 91 92 93 94

func threads(p *proctl.DebuggedProcess, ars ...string) error {
	return p.PrintThreadInfo()
}

95 96 97 98
func goroutines(p *proctl.DebuggedProcess, ars ...string) error {
	return p.PrintGoroutinesInfo()
}

99
func cont(p *proctl.DebuggedProcess, ars ...string) error {
100
	err := p.Continue()
101 102 103 104
	if err != nil {
		return err
	}

105 106 107 108 109
	return printcontext(p)
}

func step(p *proctl.DebuggedProcess, args ...string) error {
	err := p.Step()
110 111 112 113
	if err != nil {
		return err
	}

114
	return printcontext(p)
115 116
}

D
Derek Parker 已提交
117 118 119 120 121 122
func next(p *proctl.DebuggedProcess, args ...string) error {
	err := p.Next()
	if err != nil {
		return err
	}

123
	return printcontext(p)
D
Derek Parker 已提交
124 125
}

126
func clear(p *proctl.DebuggedProcess, args ...string) error {
127 128 129 130
	if len(args) == 0 {
		return fmt.Errorf("not enough arguments")
	}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	var (
		fn    *gosym.Func
		pc    uint64
		fname = args[0]
	)

	if strings.ContainsRune(fname, ':') {
		fl := strings.Split(fname, ":")

		f, err := filepath.Abs(fl[0])
		if err != nil {
			return err
		}

		l, err := strconv.Atoi(fl[1])
		if err != nil {
			return err
		}

		pc, fn, err = p.GoSymTable.LineToPC(f, l)
		if err != nil {
			return err
		}
	} else {
		fn = p.GoSymTable.LookupFunc(fname)
		if fn == nil {
			return fmt.Errorf("No function named %s", fname)
		}

		pc = fn.Entry
161 162
	}

163
	bp, err := p.Clear(pc)
164 165 166 167 168 169 170 171 172
	if err != nil {
		return err
	}

	fmt.Printf("Breakpoint cleared at %#v for %s %s:%d\n", bp.Addr, bp.FunctionName, bp.File, bp.Line)

	return nil
}

D
Derek Parker 已提交
173
func breakpoint(p *proctl.DebuggedProcess, args ...string) error {
174 175 176 177
	if len(args) == 0 {
		return fmt.Errorf("not enough arguments")
	}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	var (
		fn    *gosym.Func
		pc    uint64
		fname = args[0]
	)

	if strings.ContainsRune(fname, ':') {
		fl := strings.Split(fname, ":")

		f, err := filepath.Abs(fl[0])
		if err != nil {
			return err
		}

		l, err := strconv.Atoi(fl[1])
		if err != nil {
			return err
		}

		pc, fn, err = p.GoSymTable.LineToPC(f, l)
		if err != nil {
			return err
		}
	} else {
		fn = p.GoSymTable.LookupFunc(fname)
203 204 205
		if fn == nil {
			return fmt.Errorf("No function named %s", fname)
		}
206

207
		pc = fn.Entry
208 209 210 211 212 213 214 215 216
	}

	bp, err := p.Break(uintptr(pc))
	if err != nil {
		return err
	}

	fmt.Printf("Breakpoint set at %#v for %s %s:%d\n", bp.Addr, bp.FunctionName, bp.File, bp.Line)

217 218
	return nil
}
219

D
Derek Parker 已提交
220
func printVar(p *proctl.DebuggedProcess, args ...string) error {
221
	if len(args) == 0 {
222
		return fmt.Errorf("not enough arguments")
223 224
	}

D
Derek Parker 已提交
225 226 227 228 229 230 231 232 233
	val, err := p.EvalSymbol(args[0])
	if err != nil {
		return err
	}

	fmt.Println(val.Value)
	return nil
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
func printcontext(p *proctl.DebuggedProcess) error {
	var context []string

	regs, err := p.Registers()
	if err != nil {
		return err
	}

	f, l, _ := p.GoSymTable.PCToLine(regs.PC())

	fmt.Printf("Stopped at: %s:%d\n", f, l)
	file, err := os.Open(f)
	if err != nil {
		return err
	}
	defer file.Close()

	buf := bufio.NewReader(file)
D
Derek Parker 已提交
252 253
	for i := 1; i < l-5; i++ {
		_, err := buf.ReadString('\n')
254 255 256
		if err != nil && err != io.EOF {
			return err
		}
D
Derek Parker 已提交
257
	}
258

D
Derek Parker 已提交
259 260 261 262 263
	for i := l - 5; i <= l+5; i++ {
		line, err := buf.ReadString('\n')
		if err != nil {
			if err != io.EOF {
				return err
264
			}
D
Derek Parker 已提交
265 266 267 268 269 270 271

			if err == io.EOF {
				break
			}
		}

		if i == l {
D
Derek Parker 已提交
272
			line = "\033[34m=>\033[0m" + line
273
		}
D
Derek Parker 已提交
274

D
Derek Parker 已提交
275
		context = append(context, fmt.Sprintf("\033[34m%d\033[0m: %s", i, line))
276 277
	}

D
Derek Parker 已提交
278
	fmt.Println(strings.Join(context, ""))
279 280 281

	return nil
}