flags.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "runtime"
  22. "github.com/codegangsta/cli"
  23. "github.com/ethereum/go-ethereum/logger"
  24. "github.com/ethereum/go-ethereum/logger/glog"
  25. )
  26. var (
  27. verbosityFlag = cli.GenericFlag{
  28. Name: "verbosity",
  29. Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=detail",
  30. Value: glog.GetVerbosity(),
  31. }
  32. vmoduleFlag = cli.GenericFlag{
  33. Name: "vmodule",
  34. Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=6,p2p=5)",
  35. Value: glog.GetVModule(),
  36. }
  37. backtraceAtFlag = cli.GenericFlag{
  38. Name: "backtrace",
  39. Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
  40. Value: glog.GetTraceLocation(),
  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. memprofilerateFlag = cli.IntFlag{
  52. Name: "memprofilerate",
  53. Usage: "Turn on memory profiling with the given rate",
  54. }
  55. blockprofilerateFlag = cli.IntFlag{
  56. Name: "blockprofilerate",
  57. Usage: "Turn on block profiling with the given rate",
  58. }
  59. cpuprofileFlag = cli.StringFlag{
  60. Name: "cpuprofile",
  61. Usage: "Write CPU profile to the given file",
  62. }
  63. traceFlag = cli.StringFlag{
  64. Name: "trace",
  65. Usage: "Write execution trace to the given file",
  66. }
  67. )
  68. // Flags holds all command-line flags required for debugging.
  69. var Flags = []cli.Flag{
  70. verbosityFlag, vmoduleFlag, backtraceAtFlag,
  71. pprofFlag, pprofPortFlag,
  72. memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
  73. }
  74. // Setup initializes profiling and logging based on the CLI flags.
  75. // It should be called as early as possible in the program.
  76. func Setup(ctx *cli.Context) error {
  77. // logging
  78. glog.CopyStandardLogTo("INFO")
  79. glog.SetToStderr(true)
  80. // profiling, tracing
  81. runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
  82. Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
  83. if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
  84. if err := Handler.StartTrace(traceFile); err != nil {
  85. return err
  86. }
  87. }
  88. if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
  89. if err := Handler.StartCPUProfile(cpuFile); err != nil {
  90. return err
  91. }
  92. }
  93. // pprof server
  94. if ctx.GlobalBool(pprofFlag.Name) {
  95. address := fmt.Sprintf("127.0.0.1:%d", ctx.GlobalInt(pprofPortFlag.Name))
  96. go func() {
  97. glog.V(logger.Info).Infof("starting pprof server at http://%s/debug/pprof", address)
  98. glog.Errorln(http.ListenAndServe(address, nil))
  99. }()
  100. }
  101. return nil
  102. }
  103. // Exit stops all running profiles, flushing their output to the
  104. // respective file.
  105. func Exit() {
  106. Handler.StopCPUProfile()
  107. Handler.StopTrace()
  108. }