2016-06-15 13:54:07 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-01 11:09:48 +00:00
|
|
|
"io"
|
2016-06-15 13:54:07 +00:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2017-05-01 11:09:48 +00:00
|
|
|
"os"
|
2016-06-15 13:54:07 +00:00
|
|
|
"runtime"
|
|
|
|
|
2017-05-01 11:09:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/log/term"
|
2018-04-24 15:50:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
|
|
"github.com/ethereum/go-ethereum/metrics/exp"
|
2017-05-01 11:09:48 +00:00
|
|
|
colorable "github.com/mattn/go-colorable"
|
2016-07-03 19:44:31 +00:00
|
|
|
"gopkg.in/urfave/cli.v1"
|
2016-06-15 13:54:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-05-01 11:09:48 +00:00
|
|
|
verbosityFlag = cli.IntFlag{
|
2016-06-15 13:54:07 +00:00
|
|
|
Name: "verbosity",
|
2017-05-01 11:09:48 +00:00
|
|
|
Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
|
|
|
|
Value: 3,
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
2017-05-01 11:09:48 +00:00
|
|
|
vmoduleFlag = cli.StringFlag{
|
2016-06-15 13:54:07 +00:00
|
|
|
Name: "vmodule",
|
2017-05-01 11:09:48 +00:00
|
|
|
Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
|
|
|
|
Value: "",
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
2017-05-01 11:09:48 +00:00
|
|
|
backtraceAtFlag = cli.StringFlag{
|
2016-06-15 13:54:07 +00:00
|
|
|
Name: "backtrace",
|
|
|
|
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
|
2017-05-01 11:09:48 +00:00
|
|
|
Value: "",
|
|
|
|
}
|
|
|
|
debugFlag = cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Usage: "Prepends log messages with call-site location (file and line number)",
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
pprofFlag = cli.BoolFlag{
|
|
|
|
Name: "pprof",
|
|
|
|
Usage: "Enable the pprof HTTP server",
|
|
|
|
}
|
|
|
|
pprofPortFlag = cli.IntFlag{
|
|
|
|
Name: "pprofport",
|
|
|
|
Usage: "pprof HTTP server listening port",
|
|
|
|
Value: 6060,
|
|
|
|
}
|
2016-10-20 11:07:42 +00:00
|
|
|
pprofAddrFlag = cli.StringFlag{
|
2016-11-25 05:50:30 +00:00
|
|
|
Name: "pprofaddr",
|
2016-10-20 11:07:42 +00:00
|
|
|
Usage: "pprof HTTP server listening interface",
|
|
|
|
Value: "127.0.0.1",
|
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
memprofilerateFlag = cli.IntFlag{
|
|
|
|
Name: "memprofilerate",
|
|
|
|
Usage: "Turn on memory profiling with the given rate",
|
|
|
|
Value: runtime.MemProfileRate,
|
|
|
|
}
|
|
|
|
blockprofilerateFlag = cli.IntFlag{
|
|
|
|
Name: "blockprofilerate",
|
|
|
|
Usage: "Turn on block profiling with the given rate",
|
|
|
|
}
|
|
|
|
cpuprofileFlag = cli.StringFlag{
|
|
|
|
Name: "cpuprofile",
|
|
|
|
Usage: "Write CPU profile to the given file",
|
|
|
|
}
|
|
|
|
traceFlag = cli.StringFlag{
|
|
|
|
Name: "trace",
|
|
|
|
Usage: "Write execution trace to the given file",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Flags holds all command-line flags required for debugging.
|
|
|
|
var Flags = []cli.Flag{
|
2017-05-01 11:09:48 +00:00
|
|
|
verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
|
2016-10-20 11:07:42 +00:00
|
|
|
pprofFlag, pprofAddrFlag, pprofPortFlag,
|
2016-06-15 13:54:07 +00:00
|
|
|
memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
|
|
|
|
}
|
|
|
|
|
2017-05-01 11:09:48 +00:00
|
|
|
var glogger *log.GlogHandler
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
usecolor := term.IsTty(os.Stderr.Fd()) && os.Getenv("TERM") != "dumb"
|
|
|
|
output := io.Writer(os.Stderr)
|
|
|
|
if usecolor {
|
|
|
|
output = colorable.NewColorableStderr()
|
|
|
|
}
|
|
|
|
glogger = log.NewGlogHandler(log.StreamHandler(output, log.TerminalFormat(usecolor)))
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:54:07 +00:00
|
|
|
// Setup initializes profiling and logging based on the CLI flags.
|
|
|
|
// It should be called as early as possible in the program.
|
|
|
|
func Setup(ctx *cli.Context) error {
|
|
|
|
// logging
|
2017-05-01 11:09:48 +00:00
|
|
|
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
|
|
|
|
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
|
|
|
|
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
|
|
|
|
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))
|
|
|
|
log.Root().SetHandler(glogger)
|
2016-06-15 13:54:07 +00:00
|
|
|
|
|
|
|
// profiling, tracing
|
|
|
|
runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
|
|
|
|
Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
|
|
|
|
if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
|
|
|
|
if err := Handler.StartGoTrace(traceFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
|
|
|
|
if err := Handler.StartCPUProfile(cpuFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pprof server
|
|
|
|
if ctx.GlobalBool(pprofFlag.Name) {
|
2018-04-24 15:50:26 +00:00
|
|
|
// Hook go-metrics into expvar on any /debug/metrics request, load all vars
|
|
|
|
// from the registry into expvar, and execute regular expvar handler.
|
|
|
|
exp.Exp(metrics.DefaultRegistry)
|
|
|
|
|
2016-10-20 11:07:42 +00:00
|
|
|
address := fmt.Sprintf("%s:%d", ctx.GlobalString(pprofAddrFlag.Name), ctx.GlobalInt(pprofPortFlag.Name))
|
2016-06-15 13:54:07 +00:00
|
|
|
go func() {
|
2017-05-01 11:09:48 +00:00
|
|
|
log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
|
|
|
|
if err := http.ListenAndServe(address, nil); err != nil {
|
|
|
|
log.Error("Failure in running pprof server", "err", err)
|
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit stops all running profiles, flushing their output to the
|
|
|
|
// respective file.
|
|
|
|
func Exit() {
|
|
|
|
Handler.StopCPUProfile()
|
|
|
|
Handler.StopGoTrace()
|
|
|
|
}
|