glog_file_posix.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. //go:build (unix || windows) && !linux
  17. package glog
  18. import (
  19. "os"
  20. "syscall"
  21. "time"
  22. )
  23. // abortProcess attempts to kill the current process in a way that will dump the
  24. // currently-running goroutines someplace useful (like stderr).
  25. //
  26. // It does this by sending SIGABRT to the current process. Unfortunately, the
  27. // signal may or may not be delivered to the current thread; in order to do that
  28. // portably, we would need to add a cgo dependency and call pthread_kill.
  29. //
  30. // If successful, abortProcess does not return.
  31. func abortProcess() error {
  32. p, err := os.FindProcess(os.Getpid())
  33. if err != nil {
  34. return err
  35. }
  36. if err := p.Signal(syscall.SIGABRT); err != nil {
  37. return err
  38. }
  39. // Sent the signal. Now we wait for it to arrive and any SIGABRT handlers to
  40. // run (and eventually terminate the process themselves).
  41. //
  42. // We could just "select{}" here, but there's an outside chance that would
  43. // trigger the runtime's deadlock detector if there happen not to be any
  44. // background goroutines running. So we'll sleep a while first to give
  45. // the signal some time.
  46. time.Sleep(10 * time.Second)
  47. select {}
  48. }