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 coverage 6 7 import _ "unsafe" 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. 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 runOnNonZeroExit := true 33 runtime_addExitHook(emitCounterData, runOnNonZeroExit) 34 if istest { 35 runtime_addExitHook(emitMetaData, runOnNonZeroExit) 36 } else { 37 emitMetaData() 38 } 39 } 40 41 //go:linkname runtime_addExitHook runtime.addExitHook 42 func runtime_addExitHook(f func(), runOnNonZeroExit bool) 43