1 // Copyright 2022 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 package cfile
6 7 import "internal/runtime/exithook"
8 9 // InitHook is invoked from the main package "init" routine in
10 // programs built with "-cover". This function is intended to be
11 // called only by the compiler (via runtime/coverage.initHook).
12 //
13 // If 'istest' is false, it indicates we're building a regular program
14 // ("go build -cover ..."), in which case we immediately try to write
15 // out the meta-data file, and register emitCounterData as an exit
16 // hook.
17 //
18 // If 'istest' is true (indicating that the program in question is a
19 // Go test binary), then we tentatively queue up both emitMetaData and
20 // emitCounterData as exit hooks. In the normal case (e.g. regular "go
21 // test -cover" run) the testmain.go boilerplate will run at the end
22 // of the test, write out the coverage percentage, and then invoke
23 // MarkProfileEmitted to indicate that no more work needs to be
24 // done. If however that call is never made, this is a sign that the
25 // test binary is being used as a replacement binary for the tool
26 // being tested, hence we do want to run exit hooks when the program
27 // terminates.
28 func InitHook(istest bool) {
29 // Note: hooks are run in reverse registration order, so
30 // register the counter data hook before the meta-data hook
31 // (in the case where two hooks are needed).
32 exithook.Add(exithook.Hook{F: emitCounterData, RunOnFailure: true})
33 if istest {
34 exithook.Add(exithook.Hook{F: emitMetaData, RunOnFailure: true})
35 } else {
36 emitMetaData()
37 }
38 }
39