Skip to content

Commit

Permalink
refactor: Tidy up signals, last syscall completion and type handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
liamg committed Oct 30, 2022
1 parent f0ca668 commit ccfea47
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 27 deletions.
6 changes: 5 additions & 1 deletion printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Printer struct {
argProgress int
extraNewLine bool
multiline bool
inSyscall bool
}

func New(w io.Writer) *Printer {
Expand Down Expand Up @@ -82,8 +83,11 @@ func (p *Printer) PrintProcessExit(i int) {
if i != 0 {
colour = ColourRed
}
if p.inSyscall {
p.PrintDim(" = ?\n")
}
if p.multiline {
p.Print("\n")
}
p.PrintColour(colour, "\nProcess exited with status %d\n", i)
p.PrintColour(colour, "Process exited with status %d\n", i)
}
43 changes: 37 additions & 6 deletions printer/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,47 @@ import (
"syscall"
)

var signals = map[syscall.Signal]string{
syscall.SIGABRT: "SIGABRT",
syscall.SIGALRM: "SIGALRM",
syscall.SIGBUS: "SIGBUS",
syscall.SIGCHLD: "SIGCHLD",
syscall.SIGCONT: "SIGCONT",
syscall.SIGFPE: "SIGFPE",
syscall.SIGHUP: "SIGHUP",
syscall.SIGILL: "SIGILL",
syscall.SIGINT: "SIGINT",
syscall.SIGIO: "SIGIO",
syscall.SIGKILL: "SIGKILL",
syscall.SIGPIPE: "SIGPIPE",
syscall.SIGPROF: "SIGPROF",
syscall.SIGPWR: "SIGPWR",
syscall.SIGQUIT: "SIGQUIT",
syscall.SIGSEGV: "SIGSEGV",
syscall.SIGSTKFLT: "SIGSTKFLT",
syscall.SIGSTOP: "SIGSTOP",
syscall.SIGSYS: "SIGSYS",
syscall.SIGTERM: "SIGTERM",
syscall.SIGTRAP: "SIGTRAP",
syscall.SIGTSTP: "SIGTSTP",
syscall.SIGTTIN: "SIGTTIN",
syscall.SIGTTOU: "SIGTTOU",
syscall.SIGURG: "SIGURG",
syscall.SIGUSR1: "SIGUSR1",
syscall.SIGUSR2: "SIGUSR2",
syscall.SIGVTALRM: "SIGVTALRM",
syscall.SIGWINCH: "SIGWINCH",
syscall.SIGXCPU: "SIGXCPU",
syscall.SIGXFSZ: "SIGXFSZ",
}

func (p *Printer) PrintSignal(signal syscall.Signal) {
p.PrintColour(ColourYellow, "--> SIGNAL: %s <--\n", signalToString(signal))
}

func signalToString(signal syscall.Signal) string {
switch signal {
// TODO: more signals
case syscall.SIGURG:
return "SIGURG"
default:
return fmt.Sprintf("signal %d", signal)
if str, ok := signals[signal]; ok {
return str
}
return fmt.Sprintf("%d", signal)
}
2 changes: 2 additions & 0 deletions printer/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func (p *Printer) PrintSyscallEnter(syscall *tracer.Syscall) {
p.PrintColour(ColourDefault, syscall.Name())
}
p.printRemainingArgs(syscall, false)
p.inSyscall = true
}

func (p *Printer) PrintSyscallExit(syscall *tracer.Syscall) {
Expand All @@ -22,6 +23,7 @@ func (p *Printer) PrintSyscallExit(syscall *tracer.Syscall) {
if p.extraNewLine {
p.Print("\n")
}
p.inSyscall = false
}

func (p *Printer) printRemainingArgs(syscall *tracer.Syscall, exit bool) {
Expand Down
19 changes: 0 additions & 19 deletions tracer/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,6 @@ const (

type ReturnMetadata ArgMetadata

type ArgType int

const (
ArgTypeUnknown ArgType = iota
ArgTypeData
ArgTypeInt
ArgTypeStat
ArgTypeLong
ArgTypeAddress
ArgTypeUnsignedInt
ArgTypeUnsignedLong
ArgTypePollFdArray
ArgTypeObject
ArgTypeErrorCode
ArgTypeSigAction
ArgTypeIovecArray
ArgTypeIntArray
)

type Arg struct {
name string
t ArgType
Expand Down
12 changes: 12 additions & 0 deletions tracer/sys_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ var (
},
unix.SYS_OPENAT: {
Name: "openat",
ReturnValue: ReturnMetadata{
Type: ArgTypeInt,
},
Args: []ArgMetadata{
{
Name: "dfd",
Expand All @@ -592,6 +595,9 @@ var (
},
unix.SYS_NEWFSTATAT: {
Name: "newfstatat",
ReturnValue: ReturnMetadata{
Type: ArgTypeErrorCode,
},
Args: []ArgMetadata{
{
Name: "dfd",
Expand All @@ -616,6 +622,9 @@ var (
},
unix.SYS_EXIT: {
Name: "exit",
ReturnValue: ReturnMetadata{
Type: ArgTypeErrorCode,
},
Args: []ArgMetadata{
{
Name: "status",
Expand All @@ -625,6 +634,9 @@ var (
},
unix.SYS_EXIT_GROUP: {
Name: "exit_group",
ReturnValue: ReturnMetadata{
Type: ArgTypeErrorCode,
},
Args: []ArgMetadata{
{
Name: "status",
Expand Down
21 changes: 20 additions & 1 deletion tracer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ import (
"sync"
)

type ArgType int

const (
ArgTypeUnknown ArgType = iota
ArgTypeData
ArgTypeInt
ArgTypeStat
ArgTypeLong
ArgTypeAddress
ArgTypeUnsignedInt
ArgTypeUnsignedLong
ArgTypePollFdArray
ArgTypeObject
ArgTypeErrorCode
ArgTypeSigAction
ArgTypeIovecArray
ArgTypeIntArray
)

type typeHandler func(arg *Arg, metadata ArgMetadata, raw uintptr, next uintptr, ret uintptr, pid int) error

var typesRegistry = map[ArgType]typeHandler{}
Expand All @@ -25,5 +44,5 @@ func handleType(arg *Arg, metadata ArgMetadata, raw uintptr, next uintptr, ret u
if h, ok := typesRegistry[metadata.Type]; ok {
return h(arg, metadata, raw, next, ret, pid)
}
return fmt.Errorf("no handler registered for type %d", metadata.Type)
return nil
}

0 comments on commit ccfea47

Please sign in to comment.