trace.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. //+build go1.5
  17. package debug
  18. import (
  19. "errors"
  20. "os"
  21. "runtime/trace"
  22. "github.com/ethereum/go-ethereum/log"
  23. )
  24. // StartGoTrace turns on tracing, writing to the given file.
  25. func (h *HandlerT) StartGoTrace(file string) error {
  26. h.mu.Lock()
  27. defer h.mu.Unlock()
  28. if h.traceW != nil {
  29. return errors.New("trace already in progress")
  30. }
  31. f, err := os.Create(expandHome(file))
  32. if err != nil {
  33. return err
  34. }
  35. if err := trace.Start(f); err != nil {
  36. f.Close()
  37. return err
  38. }
  39. h.traceW = f
  40. h.traceFile = file
  41. log.Info("Go tracing started", "dump", h.traceFile)
  42. return nil
  43. }
  44. // StopTrace stops an ongoing trace.
  45. func (h *HandlerT) StopGoTrace() error {
  46. h.mu.Lock()
  47. defer h.mu.Unlock()
  48. trace.Stop()
  49. if h.traceW == nil {
  50. return errors.New("trace not in progress")
  51. }
  52. log.Info("Done writing Go trace", "dump", h.traceFile)
  53. h.traceW.Close()
  54. h.traceW = nil
  55. h.traceFile = ""
  56. return nil
  57. }