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