flags.go 4.4 KB

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