Skip to content

Commit

Permalink
tracer: make bytes truncation configurable (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso authored Jul 26, 2023
1 parent 7cfbd27 commit 8d274b9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmd/wasirun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var (
wasiHttpAddr string
wasiHttpPath string
trace bool
tracerStringSize int
nonBlockingStdio bool
version bool
maxOpenFiles int
Expand All @@ -125,6 +126,7 @@ func main() {
flagSet.StringVar(&wasiHttpAddr, "http-server-addr", "", "")
flagSet.StringVar(&wasiHttpPath, "http-server-path", "/", "")
flagSet.BoolVar(&trace, "trace", false, "")
flagSet.IntVar(&tracerStringSize, "tracer-string-size", 32, "")
flagSet.BoolVar(&nonBlockingStdio, "non-blocking-stdio", false, "")
flagSet.BoolVar(&version, "version", false, "")
flagSet.BoolVar(&version, "v", false, "")
Expand Down Expand Up @@ -212,7 +214,7 @@ func run(wasmFile string, args []string) error {
WithDials(dials...).
WithNonBlockingStdio(nonBlockingStdio).
WithSocketsExtension(socketExt, wasmModule).
WithTracer(trace, os.Stderr).
WithTracer(trace, os.Stderr, wasi.WithTracerStringSize(tracerStringSize)).
WithMaxOpenFiles(maxOpenFiles).
WithMaxOpenDirs(maxOpenDirs)

Expand Down
4 changes: 3 additions & 1 deletion imports/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Builder struct {
pathOpenSockets bool
nonBlockingStdio bool
tracer io.Writer
tracerOptions []wasi.TracerOption
decorators []wasi_snapshot_preview1.Decorator
wrappers []func(wasi.System) wasi.System
errors []error
Expand Down Expand Up @@ -198,11 +199,12 @@ func (b *Builder) WithNonBlockingStdio(enable bool) *Builder {

// WithTracer enables the Tracer, and instructs it to write to the
// specified io.Writer.
func (b *Builder) WithTracer(enable bool, w io.Writer) *Builder {
func (b *Builder) WithTracer(enable bool, w io.Writer, options ...wasi.TracerOption) *Builder {
if !enable {
w = nil
}
b.tracer = w
b.tracerOptions = options
return b
}

Expand Down
2 changes: 1 addition & 1 deletion imports/builder_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (b *Builder) Instantiate(ctx context.Context, runtime wazero.Runtime) (ctxr
system = &unix.PathOpenSockets{System: unixSystem}
}
if b.tracer != nil {
system = wasi.Trace(b.tracer, system)
system = wasi.Trace(b.tracer, system, b.tracerOptions...)
}
for _, wrap := range b.wrappers {
system = wrap(system)
Expand Down
38 changes: 29 additions & 9 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,35 @@ import (

// Trace wraps a System to log all calls to its methods in a human-readable
// format to the given io.Writer.
func Trace(w io.Writer, s System) System {
return &tracer{writer: w, system: s}
func Trace(w io.Writer, s System, options ...TracerOption) System {
t := &tracer{
writer: w,
system: s,
stringSize: 32,
}
for _, option := range options {
option(t)
}
return t
}

// TracerOption configures a tracer.
type TracerOption func(*tracer)

// WithTracerStringSize sets the number of bytes to print when
// printing strings.
//
// To disable truncation of strings, use stringSize < 0.
//
// The default string size is 32.
func WithTracerStringSize(stringSize int) TracerOption {
return func(t *tracer) { t.stringSize = stringSize }
}

type tracer struct {
writer io.Writer
system System
writer io.Writer
system System
stringSize int
}

func (t *tracer) ArgsSizesGet(ctx context.Context) (int, int, Errno) {
Expand Down Expand Up @@ -876,15 +898,13 @@ func (t *tracer) printAddressInfo(a AddressInfo) {
t.printf("}")
}

const maxBytes = 32

func (t *tracer) printBytes(b []byte) {
t.printf("[%d]byte(\"", len(b))

if len(b) > 0 {
trunc := b
if len(b) > maxBytes {
trunc = trunc[:maxBytes]
if t.stringSize >= 0 && len(b) > t.stringSize {
trunc = trunc[:t.stringSize]
}
for _, c := range trunc {
if c < 32 || c >= 127 || c == '"' {
Expand All @@ -911,7 +931,7 @@ func (t *tracer) printBytes(b []byte) {
}
}
t.printf("\"")
if len(b) > maxBytes {
if t.stringSize >= 0 && len(b) > t.stringSize {
t.printf("...")
}
t.printf(")")
Expand Down

0 comments on commit 8d274b9

Please sign in to comment.