Skip to content

Commit

Permalink
add ecov trace format
Browse files Browse the repository at this point in the history
  • Loading branch information
lunixbochs committed Jun 5, 2018
1 parent 0404aba commit 2580ea2
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
88 changes: 88 additions & 0 deletions go/cmd/trace/ecov.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package trace

import (
"fmt"
"github.com/pkg/errors"
"io"
"os"
"sort"

"github.com/lunixbochs/usercorn/go/arch"
"github.com/lunixbochs/usercorn/go/models"
"github.com/lunixbochs/usercorn/go/models/cpu"
"github.com/lunixbochs/usercorn/go/models/debug"
"github.com/lunixbochs/usercorn/go/models/trace"
)

// TODO: this is duplicated from drcov.go
func WriteEcov(tf *trace.TraceReader, out *os.File) error {
arch, OS, err := arch.GetArch(tf.Header.Arch, tf.Header.OS)
if err != nil {
return errors.Wrap(err, "arch.GetArch() failed")
}
config := &models.Config{}
config.Init()
replay := trace.NewReplay(arch, OS, tf.Header.CodeOrder, debug.NewDebug(tf.Header.Arch, config))

modserial := 0
modules := make(map[string]*cpu.Page)
modlookup := make(cpu.Pages, 0)

addmod := func(o *trace.OpMemMap) {
if o.Prot&cpu.PROT_EXEC == 0 {
return
}
key := o.Desc + "|" + o.File
if _, ok := modules[key]; !ok {
desc := o.File
if desc == "" {
desc = "[" + o.Desc + "]"
}
mod := &cpu.Page{Addr: o.Addr, Size: o.Size, Prot: modserial, Desc: desc}
modules[key] = mod
modlookup = append(modlookup, mod)
modserial++
}
sort.Sort(modlookup)
}

for {
op, err := tf.Next()
if err == io.EOF {
break
} else if err != nil {
fmt.Println(errors.Wrap(err, "error reading next trace operation"))
break
}
switch frame := op.(type) {
case *trace.OpKeyframe:
for _, op := range frame.Ops {
switch o := op.(type) {
case *trace.OpMemMap:
addmod(o)
}
}
case *trace.OpFrame:
for _, op := range frame.Ops {
switch o := op.(type) {
case *trace.OpJmp:
mod := modlookup.Find(o.Addr)
if mod != nil && mod.Prot == 0 {
fmt.Fprintf(out, "%x\n", o.Addr-mod.Addr)
}
case *trace.OpMemMap:
addmod(o)
case *trace.OpSyscall:
for _, op := range o.Ops {
switch o := op.(type) {
case *trace.OpMemMap:
addmod(o)
}
}
}
}
}
}
replay.Flush()
return nil
}
14 changes: 13 additions & 1 deletion go/cmd/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ func Main(args []string) {
jsonFlag := fs.Bool("json", false, "output trace as line-delimited JSON objects")
prettyFlag := fs.Bool("pretty", false, "output trace as human-readable console text")
drcovFlag := fs.String("drcov", "", "output trace to drcov file")
ecovFlag := fs.String("ecov", "", "output trace to ecov file")
fs.Usage = func() {
fmt.Printf("Usage: %s [options] <tracefile>\n", args[0])
fs.PrintDefaults()
}

fs.Parse(args[1:])
if fs.NArg() == 0 || !(*jsonFlag || *prettyFlag || *drcovFlag != "") {
if fs.NArg() == 0 || !(*jsonFlag || *prettyFlag || *drcovFlag != "" || *ecovFlag != "") {
fs.Usage()
os.Exit(1)
}
Expand Down Expand Up @@ -107,6 +108,17 @@ func Main(args []string) {
fmt.Fprintf(os.Stderr, "error generating drcov file: %v\n", err)
os.Exit(1)
}
} else if *ecovFlag != "" {
f, err := os.Create(*ecovFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening ecov output file: %v\n", err)
os.Exit(1)
}
defer f.Close()
if err := WriteEcov(tf, f); err != nil {
fmt.Fprintf(os.Stderr, "error generating ecov file: %v\n", err)
os.Exit(1)
}
}
}

Expand Down

0 comments on commit 2580ea2

Please sign in to comment.