cfile

Imports

Imports #

"internal/runtime/exithook"
"encoding/json"
"fmt"
"internal/coverage"
"internal/coverage/calloc"
"internal/coverage/cformat"
"internal/coverage/cmerge"
"internal/coverage/decodecounter"
"internal/coverage/decodemeta"
"internal/coverage/pods"
"internal/coverage/rtcov"
"internal/runtime/atomic"
"io"
"os"
"path/filepath"
"strings"
"unsafe"
"fmt"
"internal/coverage"
"internal/coverage/rtcov"
"io"
"sync/atomic"
"unsafe"
"fmt"
"hash/fnv"
"internal/coverage"
"internal/coverage/encodecounter"
"internal/coverage/encodemeta"
"internal/coverage/rtcov"
"io"
"os"
"path/filepath"
"runtime"
"strconv"
"sync/atomic"
"time"
"unsafe"

Constants & Variables

capturedOsArgs var #

Copy of os.Args made at init time, converted into map format.

var capturedOsArgs map[string]string

cgran var #

Counter granularity for this instrumented program run.

var cgran coverage.CounterGranularity

cmode var #

Counter mode for this instrumented program run.

var cmode coverage.CounterMode

counterDataFile const #

const counterDataFile

covProfileAlreadyEmitted var #

Flag used in tests to signal that coverage data already written.

var covProfileAlreadyEmitted bool

finalHash var #

finalHash is computed at init time from the list of meta-data symbols registered during init. It is used both for writing the meta-data file and counter-data files.

var finalHash [16]byte

finalHashComputed var #

Set to true when we've computed finalHash + finalMetaLen.

var finalHashComputed bool

finalMetaLen var #

Total meta-data length.

var finalMetaLen uint64

goCoverDir var #

Cached value of GOCOVERDIR environment variable.

var goCoverDir string

metaDataEmitAttempted var #

Records whether we've already attempted to write meta-data.

var metaDataEmitAttempted bool

metaDataFile const #

const metaDataFile

noFile const #

const noFile = *ast.BinaryExpr

Type Aliases

fileType type #

fileType is used to select between counter-data files and meta-data files.

type fileType int

Structs

emitState struct #

emitState holds useful state information during the emit process. When an instrumented program finishes execution and starts the process of writing out coverage data, it's possible that an existing meta-data file already exists in the output directory. In this case openOutputFiles() below will leave the 'mf' field below as nil. If a new meta-data file is needed, field 'mfname' will be the final desired path of the meta file, 'mftmp' will be a temporary file, and 'mf' will be an open os.File pointer for 'mftmp'. The meta-data file payload will be written to 'mf', the temp file will be then closed and renamed (from 'mftmp' to 'mfname'), so as to insure that the meta-data file is created atomically; we want this so that things work smoothly in cases where there are several instances of a given instrumented program all terminating at the same time and trying to create meta-data files simultaneously. For counter data files there is less chance of a collision, hence the openOutputFiles() stores the counter data file in 'cfname' and then places the *io.File into 'cf'.

type emitState struct {
mfname string
mftmp string
mf *os.File
cfname string
cftmp string
cf *os.File
outdir string
metalist []rtcov.CovMetaBlob
counterlist []rtcov.CovCounterBlob
pkgmap map[int]int
debug bool
}

pkfunc struct #

type pkfunc struct {
pk uint32
fcn uint32
}

tstate struct #

type tstate struct {
calloc.BatchCounterAlloc
cm *cmerge.Merger
cf *cformat.Formatter
cmode coverage.CounterMode
}

Functions

ClearCounters function #

ClearCounters implements [runtime/coverage.ClearCounters].

func ClearCounters() error

InitHook function #

InitHook is invoked from the main package "init" routine in programs built with "-cover". This function is intended to be called only by the compiler (via runtime/coverage.initHook). If 'istest' is false, it indicates we're building a regular program ("go build -cover ..."), in which case we immediately try to write out the meta-data file, and register emitCounterData as an exit hook. If 'istest' is true (indicating that the program in question is a Go test binary), then we tentatively queue up both emitMetaData and emitCounterData as exit hooks. In the normal case (e.g. regular "go test -cover" run) the testmain.go boilerplate will run at the end of the test, write out the coverage percentage, and then invoke MarkProfileEmitted to indicate that no more work needs to be done. If however that call is never made, this is a sign that the test binary is being used as a replacement binary for the tool being tested, hence we do want to run exit hooks when the program terminates.

func InitHook(istest bool)

MarkProfileEmitted function #

MarkProfileEmitted signals the coverage machinery that coverage data output files have already been written out, and there is no need to take any additional action at exit time. This function is called from the coverage-related boilerplate code in _testmain.go emitted for go unit tests.

func MarkProfileEmitted(val bool)

ProcessCoverTestDir function #

ProcessCoverTestDir is called from testmain code when "go test -cover" is in effect. It is not intended to be used other than internally by the Go command's generated code.

func ProcessCoverTestDir(dir string, cfile string, cm string, cpkg string, w io.Writer, selpkgs []string) error

Snapshot function #

Snapshot returns a snapshot of coverage percentage at a moment of time within a running test, so as to support the testing.Coverage() function. This version doesn't examine coverage meta-data, so the result it returns will be less accurate (more "slop") due to the fact that we don't look at the meta data to see how many statements are associated with each counter.

func Snapshot() float64

VisitFuncs method #

func (s *emitState) VisitFuncs(f encodecounter.CounterVisitorFn) error

WriteCounters function #

WriteCounters implements [runtime/coverage.WriteCounters].

func WriteCounters(w io.Writer) error

WriteCountersDir function #

WriteCountersDir implements [runtime/coverage.WriteCountersDir].

func WriteCountersDir(dir string) error

WriteMeta function #

WriteMeta implements [runtime/coverage.WriteMeta].

func WriteMeta(w io.Writer) error

WriteMetaDir function #

WriteMetaDir implements [runtime/coverage.WriteMetaDir].

func WriteMetaDir(dir string) error

captureOsArgs function #

captureOsArgs converts os.Args() into the format we use to store this info in the counter data file (counter data file "args" section is a generic key-value collection). See the 'args' section in internal/coverage/defs.go for more info. The args map is also used to capture GOOS + GOARCH values as well.

func captureOsArgs() map[string]string

emitCounterData function #

emitCounterData emits the counter data output file for this coverage run. This entry point is intended to be invoked by the runtime when an instrumented program is terminating or calling os.Exit().

func emitCounterData()

emitCounterDataFile method #

emitCounterDataFile emits the counter data portion of a coverage output file (to the file 's.cf').

func (s *emitState) emitCounterDataFile(finalHash [16]byte, w io.Writer) error

emitCounterDataToDirectory function #

emitCounterDataToDirectory emits the counter-data output file for this coverage run.

func emitCounterDataToDirectory(outdir string) error

emitCounterDataToWriter method #

emitCounterDataToWriter emits counter data for this coverage run to an io.Writer.

func (s *emitState) emitCounterDataToWriter(w io.Writer) error

emitMetaData function #

emitMetaData emits the meta-data output file for this coverage run. This entry point is intended to be invoked by the compiler from an instrumented program's main package init func.

func emitMetaData()

emitMetaDataFile method #

emitMetaDataFile emits coverage meta-data to a previously opened temporary file (s.mftmp), then renames the generated file to the final path (s.mfname).

func (s *emitState) emitMetaDataFile(finalHash [16]byte, tlen uint64) error

emitMetaDataToDirectory function #

emitMetaDataToDirectory emits the meta-data output file to the specified directory, returning an error if something went wrong.

func emitMetaDataToDirectory(outdir string, ml []rtcov.CovMetaBlob) error

getCovCounterList function #

getCovCounterList returns a list of counter-data blobs registered for the currently executing instrumented program. It is defined in the runtime. go:linkname getCovCounterList

func getCovCounterList() []rtcov.CovCounterBlob

granClash function #

func granClash(g coverage.CounterGranularity) bool

modeClash function #

func modeClash(m coverage.CounterMode) bool

needMetaDataFile method #

needMetaDataFile returns TRUE if we need to emit a meta-data file for this program run. It should be used only after openOutputFiles() has been invoked.

func (s *emitState) needMetaDataFile() bool

openCounterFile method #

openCounterFile opens an output file for the counter data portion of a test coverage run. If updates the 'cfname' and 'cf' fields in 's', returning an error if something went wrong.

func (s *emitState) openCounterFile(metaHash [16]byte) error

openMetaFile method #

openMetaFile determines whether we need to emit a meta-data output file, or whether we can reuse the existing file in the coverage out dir. It updates mfname/mftmp/mf fields in 's', returning an error if something went wrong. See the comment on the emitState type definition above for more on how file opening is managed.

func (s *emitState) openMetaFile(metaHash [16]byte, metaLen uint64) error

openOutputFiles method #

openOutputFiles opens output files in preparation for emitting coverage data. In the case of the meta-data file, openOutputFiles may determine that we can reuse an existing meta-data file in the outdir, in which case it will leave the 'mf' field in the state struct as nil. If a new meta-file is needed, the field 'mfname' will be the final desired path of the meta file, 'mftmp' will be a temporary file, and 'mf' will be an open os.File pointer for 'mftmp'. The idea is that the client/caller will write content into 'mf', close it, and then rename 'mftmp' to 'mfname'. This function also opens the counter data output file, setting 'cf' and 'cfname' in the state struct.

func (s *emitState) openOutputFiles(metaHash [16]byte, metaLen uint64, which fileType) error

prepareForMetaEmit function #

prepareForMetaEmit performs preparatory steps needed prior to emitting a meta-data file, notably computing a final hash of all meta-data blobs and capturing os args.

func prepareForMetaEmit() ([]rtcov.CovMetaBlob, error)

processPod method #

processPod reads coverage counter data for a specific pod.

func (ts *tstate) processPod(p pods.Pod, importpaths map[string]struct{...}) error

readAuxMetaFiles method #

func (ts *tstate) readAuxMetaFiles(metafiles string, importpaths map[string]struct{...}) error

reportErrorInHardcodedList function #

func reportErrorInHardcodedList(slot int32, pkgID int32, fnID uint32, nCtrs uint32)

writeMetaData function #

func writeMetaData(w io.Writer, metalist []rtcov.CovMetaBlob, cmode coverage.CounterMode, gran coverage.CounterGranularity, finalHash [16]byte) error

Generated with Arrow