1 // Go support for leveled logs, analogous to https://github.com/google/glog. 2 // 3 // Copyright 2023 Google Inc. All Rights Reserved. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 //go:build (unix || windows) && !linux 18 19 package glog 20 21 import ( 22 "os" 23 "syscall" 24 "time" 25 ) 26 27 // abortProcess attempts to kill the current process in a way that will dump the 28 // currently-running goroutines someplace useful (like stderr). 29 // 30 // It does this by sending SIGABRT to the current process. Unfortunately, the 31 // signal may or may not be delivered to the current thread; in order to do that 32 // portably, we would need to add a cgo dependency and call pthread_kill. 33 // 34 // If successful, abortProcess does not return. 35 func abortProcess() error { 36 p, err := os.FindProcess(os.Getpid()) 37 if err != nil { 38 return err 39 } 40 if err := p.Signal(syscall.SIGABRT); err != nil { 41 return err 42 } 43 44 // Sent the signal. Now we wait for it to arrive and any SIGABRT handlers to 45 // run (and eventually terminate the process themselves). 46 // 47 // We could just "select{}" here, but there's an outside chance that would 48 // trigger the runtime's deadlock detector if there happen not to be any 49 // background goroutines running. So we'll sleep a while first to give 50 // the signal some time. 51 time.Sleep(10 * time.Second) 52 select {} 53 } 54