trace.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. //go:build go1.5
  17. // +build go1.5
  18. package debug
  19. import (
  20. "errors"
  21. "os"
  22. "runtime/trace"
  23. "github.com/ethereum/go-ethereum/log"
  24. )
  25. // StartGoTrace turns on tracing, writing to the given file.
  26. func (h *HandlerT) StartGoTrace(file string) error {
  27. h.mu.Lock()
  28. defer h.mu.Unlock()
  29. if h.traceW != nil {
  30. return errors.New("trace already in progress")
  31. }
  32. f, err := os.Create(expandHome(file))
  33. if err != nil {
  34. return err
  35. }
  36. if err := trace.Start(f); err != nil {
  37. f.Close()
  38. return err
  39. }
  40. h.traceW = f
  41. h.traceFile = file
  42. log.Info("Go tracing started", "dump", h.traceFile)
  43. return nil
  44. }
  45. // StopTrace stops an ongoing trace.
  46. func (h *HandlerT) StopGoTrace() error {
  47. h.mu.Lock()
  48. defer h.mu.Unlock()
  49. trace.Stop()
  50. if h.traceW == nil {
  51. return errors.New("trace not in progress")
  52. }
  53. log.Info("Done writing Go trace", "dump", h.traceFile)
  54. h.traceW.Close()
  55. h.traceW = nil
  56. h.traceFile = ""
  57. return nil
  58. }