github.com/juliankolbe/go-ethereum@v1.9.992/internal/debug/flags.go (about) 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 17 package debug 18 19 import ( 20 "fmt" 21 "io" 22 "net/http" 23 _ "net/http/pprof" 24 "os" 25 "runtime" 26 27 "github.com/juliankolbe/go-ethereum/log" 28 "github.com/juliankolbe/go-ethereum/metrics" 29 "github.com/juliankolbe/go-ethereum/metrics/exp" 30 "github.com/fjl/memsize/memsizeui" 31 "github.com/mattn/go-colorable" 32 "github.com/mattn/go-isatty" 33 "gopkg.in/urfave/cli.v1" 34 ) 35 36 var Memsize memsizeui.Handler 37 38 var ( 39 verbosityFlag = cli.IntFlag{ 40 Name: "verbosity", 41 Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail", 42 Value: 3, 43 } 44 logjsonFlag = cli.BoolFlag{ 45 Name: "log.json", 46 Usage: "Format logs with JSON", 47 } 48 vmoduleFlag = cli.StringFlag{ 49 Name: "vmodule", 50 Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)", 51 Value: "", 52 } 53 backtraceAtFlag = cli.StringFlag{ 54 Name: "backtrace", 55 Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")", 56 Value: "", 57 } 58 debugFlag = cli.BoolFlag{ 59 Name: "debug", 60 Usage: "Prepends log messages with call-site location (file and line number)", 61 } 62 pprofFlag = cli.BoolFlag{ 63 Name: "pprof", 64 Usage: "Enable the pprof HTTP server", 65 } 66 pprofPortFlag = cli.IntFlag{ 67 Name: "pprof.port", 68 Usage: "pprof HTTP server listening port", 69 Value: 6060, 70 } 71 pprofAddrFlag = cli.StringFlag{ 72 Name: "pprof.addr", 73 Usage: "pprof HTTP server listening interface", 74 Value: "127.0.0.1", 75 } 76 memprofilerateFlag = cli.IntFlag{ 77 Name: "pprof.memprofilerate", 78 Usage: "Turn on memory profiling with the given rate", 79 Value: runtime.MemProfileRate, 80 } 81 blockprofilerateFlag = cli.IntFlag{ 82 Name: "pprof.blockprofilerate", 83 Usage: "Turn on block profiling with the given rate", 84 } 85 cpuprofileFlag = cli.StringFlag{ 86 Name: "pprof.cpuprofile", 87 Usage: "Write CPU profile to the given file", 88 } 89 traceFlag = cli.StringFlag{ 90 Name: "trace", 91 Usage: "Write execution trace to the given file", 92 } 93 ) 94 95 // Flags holds all command-line flags required for debugging. 96 var Flags = []cli.Flag{ 97 verbosityFlag, logjsonFlag, vmoduleFlag, backtraceAtFlag, debugFlag, 98 pprofFlag, pprofAddrFlag, pprofPortFlag, memprofilerateFlag, 99 blockprofilerateFlag, cpuprofileFlag, traceFlag, 100 } 101 102 var ( 103 glogger *log.GlogHandler 104 ) 105 106 func init() { 107 glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) 108 glogger.Verbosity(log.LvlInfo) 109 log.Root().SetHandler(glogger) 110 } 111 112 // Setup initializes profiling and logging based on the CLI flags. 113 // It should be called as early as possible in the program. 114 func Setup(ctx *cli.Context) error { 115 var ostream log.Handler 116 output := io.Writer(os.Stderr) 117 if ctx.GlobalBool(logjsonFlag.Name) { 118 ostream = log.StreamHandler(output, log.JSONFormat()) 119 } else { 120 usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb" 121 if usecolor { 122 output = colorable.NewColorableStderr() 123 } 124 ostream = log.StreamHandler(output, log.TerminalFormat(usecolor)) 125 } 126 glogger.SetHandler(ostream) 127 // logging 128 log.PrintOrigins(ctx.GlobalBool(debugFlag.Name)) 129 glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name))) 130 glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name)) 131 glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name)) 132 log.Root().SetHandler(glogger) 133 134 // profiling, tracing 135 runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name) 136 137 Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name)) 138 139 if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" { 140 if err := Handler.StartGoTrace(traceFile); err != nil { 141 return err 142 } 143 } 144 145 if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" { 146 if err := Handler.StartCPUProfile(cpuFile); err != nil { 147 return err 148 } 149 } 150 151 // pprof server 152 if ctx.GlobalBool(pprofFlag.Name) { 153 listenHost := ctx.GlobalString(pprofAddrFlag.Name) 154 155 port := ctx.GlobalInt(pprofPortFlag.Name) 156 157 address := fmt.Sprintf("%s:%d", listenHost, port) 158 // This context value ("metrics.addr") represents the utils.MetricsHTTPFlag.Name. 159 // It cannot be imported because it will cause a cyclical dependency. 160 StartPProf(address, !ctx.GlobalIsSet("metrics.addr")) 161 } 162 return nil 163 } 164 165 func StartPProf(address string, withMetrics bool) { 166 // Hook go-metrics into expvar on any /debug/metrics request, load all vars 167 // from the registry into expvar, and execute regular expvar handler. 168 if withMetrics { 169 exp.Exp(metrics.DefaultRegistry) 170 } 171 http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize)) 172 log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address)) 173 go func() { 174 if err := http.ListenAndServe(address, nil); err != nil { 175 log.Error("Failure in running pprof server", "err", err) 176 } 177 }() 178 } 179 180 // Exit stops all running profiles, flushing their output to the 181 // respective file. 182 func Exit() { 183 Handler.StopCPUProfile() 184 Handler.StopGoTrace() 185 }