提交 1ad66660 编写于 作者: D Derek Parker

Prefer errors.New for unformatted errors

上级 6cadeb41
......@@ -4,6 +4,7 @@ package command
import (
"bufio"
"errors"
"fmt"
"io"
"os"
......@@ -103,7 +104,7 @@ func CommandFunc(fn func() error) cmdfunc {
}
func noCmdAvailable(p *proctl.DebuggedProcess, args ...string) error {
return fmt.Errorf("command not available")
return errors.New("command not available")
}
func nullCommand(p *proctl.DebuggedProcess, args ...string) error {
......@@ -140,7 +141,7 @@ func threads(p *proctl.DebuggedProcess, args ...string) error {
func thread(p *proctl.DebuggedProcess, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("you must specify a thread")
return errors.New("you must specify a thread")
}
oldTid := p.CurrentThread.Id
tid, err := strconv.Atoi(args[0])
......@@ -204,7 +205,7 @@ func next(p *proctl.DebuggedProcess, args ...string) error {
func clear(p *proctl.DebuggedProcess, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments")
return errors.New("not enough arguments")
}
bp, err := p.ClearByLocation(args[0])
......@@ -250,7 +251,7 @@ func breakpoints(p *proctl.DebuggedProcess, args ...string) error {
func breakpoint(p *proctl.DebuggedProcess, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments")
return errors.New("not enough arguments")
}
bp, err := p.BreakByLocation(args[0])
......@@ -265,7 +266,7 @@ func breakpoint(p *proctl.DebuggedProcess, args ...string) error {
func printVar(p *proctl.DebuggedProcess, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments")
return errors.New("not enough arguments")
}
val, err := p.EvalSymbol(args[0])
......@@ -292,7 +293,7 @@ func filterVariables(vars []*proctl.Variable, filter *regexp.Regexp) []string {
func info(p *proctl.DebuggedProcess, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments. expected info type [regex].")
return errors.New("not enough arguments. expected info type [regex].")
}
// Allow for optional regex
......@@ -345,7 +346,7 @@ func info(p *proctl.DebuggedProcess, args ...string) error {
data = filterVariables(vars, filter)
default:
return fmt.Errorf("unsupported info type, must be args, funcs, locals, sources, or vars")
return errors.New("unsupported info type, must be args, funcs, locals, sources, or vars")
}
// sort and output data
......
......@@ -2,7 +2,7 @@ package reader
import (
"debug/dwarf"
"fmt"
"errors"
)
type Reader struct {
......@@ -57,7 +57,7 @@ func (reader *Reader) SeekToFunction(pc uint64) (*dwarf.Entry, error) {
}
}
return nil, fmt.Errorf("unable to find function context")
return nil, errors.New("unable to find function context")
}
// SeekToType moves the reader to the type specified by the entry,
......@@ -66,7 +66,7 @@ func (reader *Reader) SeekToFunction(pc uint64) (*dwarf.Entry, error) {
func (reader *Reader) SeekToType(entry *dwarf.Entry, resolveTypedefs bool, resolvePointerTypes bool) (*dwarf.Entry, error) {
offset, ok := entry.Val(dwarf.AttrType).(dwarf.Offset)
if !ok {
return nil, fmt.Errorf("entry does not have a type attribute")
return nil, errors.New("entry does not have a type attribute")
}
// Seek to the first type offset
......@@ -94,7 +94,7 @@ func (reader *Reader) SeekToType(entry *dwarf.Entry, resolveTypedefs bool, resol
reader.Seek(offset)
}
return nil, fmt.Errorf("no type entry found")
return nil, errors.New("no type entry found")
}
// NextScopeVariable moves the reader to the next debug entry that describes a local variable and returns the entry.
......
package proctl
import "fmt"
import "errors"
// TODO(darwin)
func setHardwareBreakpoint(reg, tid int, addr uint64) error {
return fmt.Errorf("not implemented on darwin")
return errors.New("not implemented on darwin")
}
// TODO(darwin)
func clearHardwareBreakpoint(reg, tid int) error {
return fmt.Errorf("not implemented on darwin")
return errors.New("not implemented on darwin")
}
......@@ -15,7 +15,10 @@ int offset(int reg) {
*/
import "C"
import "fmt"
import (
"errors"
"fmt"
)
// Sets a hardware breakpoint by setting the contents of the
// debug register `reg` with the address of the instruction
......@@ -23,7 +26,7 @@ import "fmt"
// DR0-DR3. Debug register 7 is the control register.
func setHardwareBreakpoint(reg, tid int, addr uint64) error {
if reg < 0 || reg > 3 {
return fmt.Errorf("invalid debug register value")
return errors.New("invalid debug register value")
}
var (
......
......@@ -4,6 +4,7 @@ import (
"debug/dwarf"
"debug/gosym"
"encoding/binary"
"errors"
"fmt"
"os"
"path/filepath"
......@@ -582,7 +583,7 @@ func (dbp *DebuggedProcess) handleBreakpointOnThread(id int) (*ThreadContext, er
func (dbp *DebuggedProcess) run(fn func() error) error {
if dbp.exited {
return fmt.Errorf("process has already exited")
return errors.New("process has already exited")
}
dbp.running = true
dbp.halt = false
......
......@@ -6,6 +6,7 @@ import "C"
import (
"debug/gosym"
"debug/macho"
"errors"
"fmt"
"os"
"path/filepath"
......@@ -49,7 +50,7 @@ func Launch(cmd []string) (*DebuggedProcess, error) {
pid := int(C.fork_exec(C.CString(argv0), &argv, &dbp.os.task, &dbp.os.portSet, &dbp.os.exceptionPort, &dbp.os.notificationPort))
if pid <= 0 {
return nil, fmt.Errorf("could not fork/exec")
return nil, errors.New("could not fork/exec")
}
dbp.Pid = pid
......@@ -69,7 +70,7 @@ func (dbp *DebuggedProcess) requestManualStop() (err error) {
)
kret := C.raise_exception(task, thread, exceptionPort, C.EXC_BREAKPOINT)
if kret != C.KERN_SUCCESS {
return fmt.Errorf("could not raise mach exception")
return errors.New("could not raise mach exception")
}
return nil
}
......@@ -81,7 +82,7 @@ func (dbp *DebuggedProcess) updateThreadList() error {
count = C.thread_count(C.task_t(dbp.os.task))
)
if count == -1 {
return fmt.Errorf("could not get thread count")
return errors.New("could not get thread count")
}
list := make([]uint32, count)
......@@ -89,10 +90,10 @@ func (dbp *DebuggedProcess) updateThreadList() error {
// instead of getting count above and passing in a slice
kret = C.get_threads(C.task_t(dbp.os.task), unsafe.Pointer(&list[0]))
if kret != C.KERN_SUCCESS {
return fmt.Errorf("could not get thread list")
return errors.New("could not get thread list")
}
if count < 0 {
return fmt.Errorf("could not get thread list")
return errors.New("could not get thread list")
}
for _, port := range list {
......@@ -232,7 +233,7 @@ func (dbp *DebuggedProcess) trapWait(pid int) (*ThreadContext, error) {
}
return nil, ManualStopError{}
case 0:
return nil, fmt.Errorf("error while waiting for task")
return nil, errors.New("error while waiting for task")
}
// Since we cannot be notified of new threads on OS X
......
......@@ -2,7 +2,7 @@ package proctl
// #include "threads_darwin.h"
import "C"
import "fmt"
import "errors"
type Regs struct {
pc, sp uint64
......@@ -19,7 +19,7 @@ func (r *Regs) SP() uint64 {
func (r *Regs) SetPC(thread *ThreadContext, pc uint64) error {
kret := C.set_pc(thread.os.thread_act, C.uint64_t(pc))
if kret != C.KERN_SUCCESS {
return fmt.Errorf("could not set pc")
return errors.New("could not set pc")
}
return nil
}
......@@ -28,7 +28,7 @@ func registers(thread *ThreadContext) (Registers, error) {
var state C.x86_thread_state64_t
kret := C.get_registers(C.mach_port_name_t(thread.os.thread_act), &state)
if kret != C.KERN_SUCCESS {
return nil, fmt.Errorf("could not get registers")
return nil, errors.New("could not get registers")
}
regs := &Regs{pc: uint64(state.__rip), sp: uint64(state.__rsp)}
return regs, nil
......
......@@ -3,6 +3,7 @@ package proctl
// #include "threads_darwin.h"
import "C"
import (
"errors"
"fmt"
"unsafe"
)
......@@ -24,12 +25,12 @@ func (t *ThreadContext) Halt() error {
func (t *ThreadContext) singleStep() error {
kret := C.single_step(t.os.thread_act)
if kret != C.KERN_SUCCESS {
return fmt.Errorf("could not single step")
return errors.New("could not single step")
}
t.Process.trapWait(0)
kret = C.clear_trap_flag(t.os.thread_act)
if kret != C.KERN_SUCCESS {
return fmt.Errorf("could not clear CPU trap flag")
return errors.New("could not clear CPU trap flag")
}
return nil
}
......@@ -41,7 +42,7 @@ func (t *ThreadContext) resume() error {
}
kret := C.resume_thread(t.os.thread_act)
if kret != C.KERN_SUCCESS {
return fmt.Errorf("could not continue thread")
return errors.New("could not continue thread")
}
return nil
}
......@@ -64,7 +65,7 @@ func writeMemory(thread *ThreadContext, addr uintptr, data []byte) (int, error)
)
if ret := C.write_memory(thread.Process.os.task, vm_addr, vm_data, length); ret < 0 {
return 0, fmt.Errorf("could not write memory")
return 0, errors.New("could not write memory")
}
return len(data), nil
}
......@@ -78,7 +79,7 @@ func readMemory(thread *ThreadContext, addr uintptr, data []byte) (int, error) {
ret := C.read_memory(thread.Process.os.task, vm_addr, vm_data, length)
if ret < 0 {
return 0, fmt.Errorf("could not read memory")
return 0, errors.New("could not read memory")
}
return len(data), nil
}
......@@ -86,7 +87,7 @@ func readMemory(thread *ThreadContext, addr uintptr, data []byte) (int, error) {
func (thread *ThreadContext) saveRegisters() (Registers, error) {
kret := C.get_registers(C.mach_port_name_t(thread.os.thread_act), &thread.os.registers)
if kret != C.KERN_SUCCESS {
return nil, fmt.Errorf("could not save register contents")
return nil, errors.New("could not save register contents")
}
return &Regs{pc: uint64(thread.os.registers.__rip), sp: uint64(thread.os.registers.__rsp)}, nil
}
......@@ -94,7 +95,7 @@ func (thread *ThreadContext) saveRegisters() (Registers, error) {
func (thread *ThreadContext) restoreRegisters() error {
kret := C.set_registers(C.mach_port_name_t(thread.os.thread_act), &thread.os.registers)
if kret != C.KERN_SUCCESS {
return fmt.Errorf("could not save register contents")
return errors.New("could not save register contents")
}
return nil
}
package proctl
import (
"errors"
"fmt"
sys "golang.org/x/sys/unix"
......@@ -52,7 +53,7 @@ func (t *ThreadContext) blocked() bool {
func (thread *ThreadContext) saveRegisters() (Registers, error) {
if err := sys.PtraceGetRegs(thread.Id, &thread.os.registers); err != nil {
return nil, fmt.Errorf("could not save register contents")
return nil, errors.New("could not save register contents")
}
return &Regs{&thread.os.registers}, nil
}
......
......@@ -5,6 +5,7 @@ import (
"debug/dwarf"
"debug/gosym"
"encoding/binary"
"errors"
"fmt"
"strconv"
"strings"
......@@ -83,7 +84,7 @@ func (thread *ThreadContext) AllM() ([]*M, error) {
}
m := binary.LittleEndian.Uint64(mptr)
if m == 0 {
return nil, fmt.Errorf("allm contains no M pointers")
return nil, errors.New("allm contains no M pointers")
}
procidInstructions, err := instructionsFor("procid", thread.Process, reader, true)
......@@ -190,7 +191,7 @@ func instructionsForEntry(entry *dwarf.Entry) ([]byte, error) {
if entry.Tag == dwarf.TagMember {
instructions, ok := entry.Val(dwarf.AttrDataMemberLoc).([]byte)
if !ok {
return nil, fmt.Errorf("member data has no data member location attribute")
return nil, errors.New("member data has no data member location attribute")
}
// clone slice to prevent stomping on the dwarf data
return append([]byte{}, instructions...), nil
......@@ -199,7 +200,7 @@ func instructionsForEntry(entry *dwarf.Entry) ([]byte, error) {
// non-member
instructions, ok := entry.Val(dwarf.AttrLocation).([]byte)
if !ok {
return nil, fmt.Errorf("entry has no location attribute")
return nil, errors.New("entry has no location attribute")
}
// clone slice to prevent stomping on the dwarf data
......@@ -224,7 +225,7 @@ func parseAllMPtr(dbp *DebuggedProcess, reader *dwarf.Reader) (uint64, error) {
instructions, ok := entry.Val(dwarf.AttrLocation).([]byte)
if !ok {
return 0, fmt.Errorf("type assertion failed")
return 0, errors.New("type assertion failed")
}
addr, err := op.ExecuteStackProgram(0, instructions)
if err != nil {
......@@ -320,7 +321,7 @@ func allglenval(dbp *DebuggedProcess, reader *dwarf.Reader) (uint64, error) {
instructions, ok := entry.Val(dwarf.AttrLocation).([]byte)
if !ok {
return 0, fmt.Errorf("type assertion failed")
return 0, errors.New("type assertion failed")
}
addr, err := op.ExecuteStackProgram(0, instructions)
if err != nil {
......@@ -341,7 +342,7 @@ func addressFor(dbp *DebuggedProcess, name string, reader *dwarf.Reader) (uint64
instructions, ok := entry.Val(dwarf.AttrLocation).([]byte)
if !ok {
return 0, fmt.Errorf("type assertion failed")
return 0, errors.New("type assertion failed")
}
addr, err := op.ExecuteStackProgram(0, instructions)
if err != nil {
......@@ -358,7 +359,7 @@ func offsetFor(name string, reader *dwarf.Reader, parentinstr []byte) (uint64, e
}
instructions, ok := entry.Val(dwarf.AttrDataMemberLoc).([]byte)
if !ok {
return 0, fmt.Errorf("type assertion failed")
return 0, errors.New("type assertion failed")
}
offset, err := op.ExecuteStackProgram(0, append(parentinstr, instructions...))
if err != nil {
......@@ -489,7 +490,7 @@ func (thread *ThreadContext) evaluateStructMember(parentEntry *dwarf.Entry, read
// Get parent variable name
parentName, ok := parentEntry.Val(dwarf.AttrName).(string)
if !ok {
return nil, fmt.Errorf("unable to retrive variable name")
return nil, errors.New("unable to retrive variable name")
}
// Seek reader to the type information so members can be iterated
......@@ -522,7 +523,7 @@ func (thread *ThreadContext) evaluateStructMember(parentEntry *dwarf.Entry, read
offset, ok := memberEntry.Val(dwarf.AttrType).(dwarf.Offset)
if !ok {
return nil, fmt.Errorf("type assertion failed")
return nil, errors.New("type assertion failed")
}
data := thread.Process.dwarf
......@@ -549,7 +550,7 @@ func (thread *ThreadContext) evaluateStructMember(parentEntry *dwarf.Entry, read
// Extracts the name, type, and value of a variable from a dwarf entry
func (thread *ThreadContext) extractVariableFromEntry(entry *dwarf.Entry) (*Variable, error) {
if entry == nil {
return nil, fmt.Errorf("invalid entry")
return nil, errors.New("invalid entry")
}
if entry.Tag != dwarf.TagFormalParameter && entry.Tag != dwarf.TagVariable {
......@@ -558,12 +559,12 @@ func (thread *ThreadContext) extractVariableFromEntry(entry *dwarf.Entry) (*Vari
n, ok := entry.Val(dwarf.AttrName).(string)
if !ok {
return nil, fmt.Errorf("type assertion failed")
return nil, errors.New("type assertion failed")
}
offset, ok := entry.Val(dwarf.AttrType).(dwarf.Offset)
if !ok {
return nil, fmt.Errorf("type assertion failed")
return nil, errors.New("type assertion failed")
}
data := thread.Process.dwarf
......@@ -574,7 +575,7 @@ func (thread *ThreadContext) extractVariableFromEntry(entry *dwarf.Entry) (*Vari
instructions, ok := entry.Val(dwarf.AttrLocation).([]byte)
if !ok {
return nil, fmt.Errorf("type assertion failed")
return nil, errors.New("type assertion failed")
}
val, err := thread.extractValue(instructions, 0, t, true)
......@@ -910,7 +911,7 @@ func (thread *ThreadContext) readFloat(addr uintptr, size int64) (string, error)
return strconv.FormatFloat(n, 'f', -1, int(size)*8), nil
}
return "", fmt.Errorf("could not read float")
return "", errors.New("could not read float")
}
func (thread *ThreadContext) readBool(addr uintptr) (string, error) {
......@@ -953,7 +954,7 @@ func (thread *ThreadContext) readFunctionPtr(addr uintptr) (string, error) {
n, ok := entry.Val(dwarf.AttrName).(string)
if !ok {
return "", fmt.Errorf("Unable to retrieve function name")
return "", errors.New("Unable to retrieve function name")
}
return n, nil
......
package debugger
import (
"errors"
"fmt"
"log"
"regexp"
......@@ -65,7 +66,7 @@ func New(config *Config) *Debugger {
// operation.
func (d *Debugger) withProcess(f func(*proctl.DebuggedProcess) error) error {
if !d.running {
return fmt.Errorf("debugger isn't running")
return errors.New("debugger isn't running")
}
result := make(chan error)
......@@ -161,7 +162,7 @@ func (d *Debugger) Run() error {
// Detach stops the debugger.
func (d *Debugger) Detach(kill bool) error {
if !d.running {
return fmt.Errorf("debugger isn't running")
return errors.New("debugger isn't running")
}
d.stop <- stopSignal{KillProcess: kill}
......@@ -205,7 +206,7 @@ func (d *Debugger) CreateBreakPoint(requestedBp *api.BreakPoint) (*api.BreakPoin
case len(requestedBp.FunctionName) > 0:
loc = requestedBp.FunctionName
default:
return fmt.Errorf("no file or function name specified")
return errors.New("no file or function name specified")
}
bp, breakError := p.BreakByLocation(loc)
......
......@@ -4,6 +4,7 @@ package terminal
import (
"bufio"
"errors"
"fmt"
"io"
"os"
......@@ -106,7 +107,7 @@ func CommandFunc(fn func() error) cmdfunc {
}
func noCmdAvailable(client service.Client, args ...string) error {
return fmt.Errorf("command not available")
return errors.New("command not available")
}
func nullCommand(client service.Client, args ...string) error {
......@@ -220,7 +221,7 @@ func next(client service.Client, args ...string) error {
func clear(client service.Client, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments")
return errors.New("not enough arguments")
}
id, err := strconv.Atoi(args[0])
......@@ -272,7 +273,7 @@ func breakpoints(client service.Client, args ...string) error {
func breakpoint(client service.Client, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("argument must be either a function name or <file:line>")
return errors.New("argument must be either a function name or <file:line>")
}
requestedBp := &api.BreakPoint{}
tokens := strings.Split(args[0], ":")
......@@ -288,7 +289,7 @@ func breakpoint(client service.Client, args ...string) error {
requestedBp.File = file
requestedBp.Line = line
default:
return fmt.Errorf("invalid line reference")
return errors.New("invalid line reference")
}
bp, err := client.CreateBreakPoint(requestedBp)
......@@ -302,7 +303,7 @@ func breakpoint(client service.Client, args ...string) error {
func printVar(client service.Client, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments")
return errors.New("not enough arguments")
}
val, err := client.EvalSymbol(args[0])
......@@ -326,7 +327,7 @@ func filterVariables(vars []api.Variable, filter *regexp.Regexp) []string {
func info(client service.Client, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments. expected info type [regex].")
return errors.New("not enough arguments. expected info type [regex].")
}
// Allow for optional regex
......@@ -397,7 +398,7 @@ func info(client service.Client, args ...string) error {
}
default:
return fmt.Errorf("unsupported info type, must be args, funcs, locals, sources, or vars")
return errors.New("unsupported info type, must be args, funcs, locals, sources, or vars")
}
// sort and output data
......
package terminal
import (
"fmt"
"errors"
"testing"
"github.com/derekparker/delve/service"
......@@ -25,7 +25,7 @@ func TestCommandDefault(t *testing.T) {
func TestCommandReplay(t *testing.T) {
cmds := DebugCommands(nil)
cmds.Register("foo", func(client service.Client, args ...string) error { return fmt.Errorf("registered command") }, "foo command")
cmds.Register("foo", func(client service.Client, args ...string) error { return errors.New("registered command") }, "foo command")
cmd := cmds.Find("foo")
err := cmd(nil)
......
package terminal
import (
"errors"
"fmt"
"io"
"os"
......@@ -62,7 +63,7 @@ func (t *Term) Run() (error, int) {
if err == io.EOF {
err, status = handleExit(t.client, t)
}
err, status = fmt.Errorf("Prompt for input failed.\n"), 1
err, status = errors.New("Prompt for input failed.\n"), 1
break
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册