github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/runtime/trace/trace.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Go execution tracer. 6 // The tracer captures a wide range of execution events like goroutine 7 // creation/blocking/unblocking, syscall enter/exit/block, GC-related events, 8 // changes of heap size, processor start/stop, etc and writes them to an io.Writer 9 // in a compact form. A precise nanosecond-precision timestamp and a stack 10 // trace is captured for most events. A trace can be analyzed later with 11 // 'go tool trace' command. 12 package trace 13 14 import ( 15 "io" 16 "runtime" 17 ) 18 19 // Start enables tracing for the current program. 20 // While tracing, the trace will be buffered and written to w. 21 // Start returns an error if tracing is already enabled. 22 func Start(w io.Writer) error { 23 if err := runtime.StartTrace(); err != nil { 24 return err 25 } 26 go func() { 27 for { 28 data := runtime.ReadTrace() 29 if data == nil { 30 break 31 } 32 w.Write(data) 33 } 34 }() 35 return nil 36 } 37 38 // Stop stops the current tracing, if any. 39 // Stop only returns after all the writes for the trace have completed. 40 func Stop() { 41 runtime.StopTrace() 42 }