flags.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. package debug
  17. import (
  18. "fmt"
  19. "io"
  20. "net/http"
  21. _ "net/http/pprof"
  22. "os"
  23. "runtime"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/log/term"
  26. "github.com/ethereum/go-ethereum/metrics"
  27. "github.com/ethereum/go-ethereum/metrics/exp"
  28. colorable "github.com/mattn/go-colorable"
  29. "gopkg.in/urfave/cli.v1"
  30. )
  31. var (
  32. verbosityFlag = cli.IntFlag{
  33. Name: "verbosity",
  34. Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
  35. Value: 3,
  36. }
  37. vmoduleFlag = cli.StringFlag{
  38. Name: "vmodule",
  39. Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
  40. Value: "",
  41. }
  42. backtraceAtFlag = cli.StringFlag{
  43. Name: "backtrace",
  44. Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
  45. Value: "",
  46. }
  47. debugFlag = cli.BoolFlag{
  48. Name: "debug",
  49. Usage: "Prepends log messages with call-site location (file and line number)",
  50. }
  51. pprofFlag = cli.BoolFlag{
  52. Name: "pprof",
  53. Usage: "Enable the pprof HTTP server",
  54. }
  55. pprofPortFlag = cli.IntFlag{
  56. Name: "pprofport",
  57. Usage: "pprof HTTP server listening port",
  58. Value: 6060,
  59. }
  60. pprofAddrFlag = cli.StringFlag{
  61. Name: "pprofaddr",
  62. Usage: "pprof HTTP server listening interface",
  63. Value: "127.0.0.1",
  64. }
  65. memprofilerateFlag = cli.IntFlag{
  66. Name: "memprofilerate",
  67. Usage: "Turn on memory profiling with the given rate",
  68. Value: runtime.MemProfileRate,
  69. }
  70. blockprofilerateFlag = cli.IntFlag{
  71. Name: "blockprofilerate",
  72. Usage: "Turn on block profiling with the given rate",
  73. }
  74. cpuprofileFlag = cli.StringFlag{
  75. Name: "cpuprofile",
  76. Usage: "Write CPU profile to the given file",
  77. }
  78. traceFlag = cli.StringFlag{
  79. Name: "trace",
  80. Usage: "Write execution trace to the given file",
  81. }
  82. )
  83. // Flags holds all command-line flags required for debugging.
  84. var Flags = []cli.Flag{
  85. verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
  86. pprofFlag, pprofAddrFlag, pprofPortFlag,
  87. memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
  88. }
  89. var glogger *log.GlogHandler
  90. func init() {
  91. usecolor := term.IsTty(os.Stderr.Fd()) && os.Getenv("TERM") != "dumb"
  92. output := io.Writer(os.Stderr)
  93. if usecolor {
  94. output = colorable.NewColorableStderr()
  95. }
  96. glogger = log.NewGlogHandler(log.StreamHandler(output, log.TerminalFormat(usecolor)))
  97. }
  98. // Setup initializes profiling and logging based on the CLI flags.
  99. // It should be called as early as possible in the program.
  100. func Setup(ctx *cli.Context) error {
  101. // logging
  102. log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
  103. glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
  104. glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
  105. glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))
  106. log.Root().SetHandler(glogger)
  107. // profiling, tracing
  108. runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
  109. Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
  110. if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
  111. if err := Handler.StartGoTrace(traceFile); err != nil {
  112. return err
  113. }
  114. }
  115. if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
  116. if err := Handler.StartCPUProfile(cpuFile); err != nil {
  117. return err
  118. }
  119. }
  120. // pprof server
  121. if ctx.GlobalBool(pprofFlag.Name) {
  122. // Hook go-metrics into expvar on any /debug/metrics request, load all vars
  123. // from the registry into expvar, and execute regular expvar handler.
  124. exp.Exp(metrics.DefaultRegistry)
  125. address := fmt.Sprintf("%s:%d", ctx.GlobalString(pprofAddrFlag.Name), ctx.GlobalInt(pprofPortFlag.Name))
  126. go func() {
  127. log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
  128. if err := http.ListenAndServe(address, nil); err != nil {
  129. log.Error("Failure in running pprof server", "err", err)
  130. }
  131. }()
  132. }
  133. return nil
  134. }
  135. // Exit stops all running profiles, flushing their output to the
  136. // respective file.
  137. func Exit() {
  138. Handler.StopCPUProfile()
  139. Handler.StopGoTrace()
  140. }