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