flags.go 3.8 KB

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