2016-01-26 13:39:21 +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-02-23 18:31:13 +00:00
|
|
|
"io"
|
2016-01-26 13:39:21 +00:00
|
|
|
"net/http"
|
2022-06-13 14:24:45 +00:00
|
|
|
_ "net/http/pprof" // nolint: gosec
|
2017-02-22 12:10:07 +00:00
|
|
|
"os"
|
2016-01-26 13:39:21 +00:00
|
|
|
"runtime"
|
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/internal/flags"
|
2017-02-22 12:10:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-02-23 09:56:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
|
|
"github.com/ethereum/go-ethereum/metrics/exp"
|
2018-04-23 13:20:39 +00:00
|
|
|
"github.com/fjl/memsize/memsizeui"
|
2020-11-25 20:00:23 +00:00
|
|
|
"github.com/mattn/go-colorable"
|
2018-09-29 14:15:39 +00:00
|
|
|
"github.com/mattn/go-isatty"
|
2022-06-27 16:22:36 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2016-01-26 13:39:21 +00:00
|
|
|
)
|
|
|
|
|
2018-04-23 13:20:39 +00:00
|
|
|
var Memsize memsizeui.Handler
|
|
|
|
|
2016-01-26 13:39:21 +00:00
|
|
|
var (
|
2022-06-27 16:22:36 +00:00
|
|
|
verbosityFlag = &cli.IntFlag{
|
|
|
|
Name: "verbosity",
|
|
|
|
Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
|
|
|
|
Value: 3,
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
vmoduleFlag = &cli.StringFlag{
|
|
|
|
Name: "vmodule",
|
|
|
|
Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
|
|
|
|
Value: "",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
logjsonFlag = &cli.BoolFlag{
|
|
|
|
Name: "log.json",
|
|
|
|
Usage: "Format logs with JSON",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
2023-03-28 12:37:40 +00:00
|
|
|
logfmtFlag = &cli.BoolFlag{
|
|
|
|
Name: "log.logfmt",
|
|
|
|
Usage: "Format logs with logfmt",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
2022-11-11 10:33:18 +00:00
|
|
|
logFileFlag = &cli.StringFlag{
|
|
|
|
Name: "log.file",
|
|
|
|
Usage: "Write logs to a file",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
2022-06-27 16:22:36 +00:00
|
|
|
backtraceAtFlag = &cli.StringFlag{
|
|
|
|
Name: "log.backtrace",
|
|
|
|
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
|
|
|
|
Value: "",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
debugFlag = &cli.BoolFlag{
|
|
|
|
Name: "log.debug",
|
|
|
|
Usage: "Prepends log messages with call-site location (file and line number)",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
pprofFlag = &cli.BoolFlag{
|
|
|
|
Name: "pprof",
|
|
|
|
Usage: "Enable the pprof HTTP server",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
pprofPortFlag = &cli.IntFlag{
|
|
|
|
Name: "pprof.port",
|
|
|
|
Usage: "pprof HTTP server listening port",
|
|
|
|
Value: 6060,
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
pprofAddrFlag = &cli.StringFlag{
|
|
|
|
Name: "pprof.addr",
|
|
|
|
Usage: "pprof HTTP server listening interface",
|
|
|
|
Value: "127.0.0.1",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
memprofilerateFlag = &cli.IntFlag{
|
|
|
|
Name: "pprof.memprofilerate",
|
|
|
|
Usage: "Turn on memory profiling with the given rate",
|
|
|
|
Value: runtime.MemProfileRate,
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
blockprofilerateFlag = &cli.IntFlag{
|
|
|
|
Name: "pprof.blockprofilerate",
|
|
|
|
Usage: "Turn on block profiling with the given rate",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
cpuprofileFlag = &cli.StringFlag{
|
|
|
|
Name: "pprof.cpuprofile",
|
|
|
|
Usage: "Write CPU profile to the given file",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
traceFlag = &cli.StringFlag{
|
|
|
|
Name: "trace",
|
|
|
|
Usage: "Write execution trace to the given file",
|
|
|
|
Category: flags.LoggingCategory,
|
2016-01-26 13:39:21 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Flags holds all command-line flags required for debugging.
|
|
|
|
var Flags = []cli.Flag{
|
2021-04-06 14:09:51 +00:00
|
|
|
verbosityFlag,
|
|
|
|
vmoduleFlag,
|
|
|
|
logjsonFlag,
|
2023-03-28 12:37:40 +00:00
|
|
|
logfmtFlag,
|
2022-11-11 10:33:18 +00:00
|
|
|
logFileFlag,
|
2021-04-06 14:09:51 +00:00
|
|
|
backtraceAtFlag,
|
|
|
|
debugFlag,
|
|
|
|
pprofFlag,
|
|
|
|
pprofAddrFlag,
|
|
|
|
pprofPortFlag,
|
|
|
|
memprofilerateFlag,
|
|
|
|
blockprofilerateFlag,
|
|
|
|
cpuprofileFlag,
|
|
|
|
traceFlag,
|
2020-05-05 08:19:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 10:33:18 +00:00
|
|
|
var (
|
|
|
|
glogger *log.GlogHandler
|
|
|
|
logOutputStream log.Handler
|
|
|
|
)
|
2017-02-23 18:31:13 +00:00
|
|
|
|
|
|
|
func init() {
|
2021-02-09 09:42:55 +00:00
|
|
|
glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
|
|
|
|
glogger.Verbosity(log.LvlInfo)
|
|
|
|
log.Root().SetHandler(glogger)
|
2017-02-23 18:31:13 +00:00
|
|
|
}
|
2017-02-22 12:10:07 +00:00
|
|
|
|
2016-01-26 13:39:21 +00:00
|
|
|
// Setup initializes profiling and logging based on the CLI flags.
|
|
|
|
// It should be called as early as possible in the program.
|
2020-01-21 12:57:33 +00:00
|
|
|
func Setup(ctx *cli.Context) error {
|
2022-11-11 10:33:18 +00:00
|
|
|
logFile := ctx.String(logFileFlag.Name)
|
|
|
|
useColor := logFile == "" && os.Getenv("TERM") != "dumb" && (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd()))
|
|
|
|
|
|
|
|
var logfmt log.Format
|
2022-06-27 16:22:36 +00:00
|
|
|
if ctx.Bool(logjsonFlag.Name) {
|
2022-11-11 10:33:18 +00:00
|
|
|
logfmt = log.JSONFormat()
|
2023-03-28 12:37:40 +00:00
|
|
|
} else if ctx.Bool(logfmtFlag.Name) {
|
|
|
|
logfmt = log.LogfmtFormat()
|
2021-02-09 09:42:55 +00:00
|
|
|
} else {
|
2022-11-11 10:33:18 +00:00
|
|
|
logfmt = log.TerminalFormat(useColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
if logFile != "" {
|
|
|
|
var err error
|
|
|
|
logOutputStream, err = log.FileHandler(logFile, logfmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output := io.Writer(os.Stderr)
|
|
|
|
if useColor {
|
2021-02-09 09:42:55 +00:00
|
|
|
output = colorable.NewColorableStderr()
|
|
|
|
}
|
2022-11-11 10:33:18 +00:00
|
|
|
logOutputStream = log.StreamHandler(output, logfmt)
|
2021-02-09 09:42:55 +00:00
|
|
|
}
|
2022-11-11 10:33:18 +00:00
|
|
|
glogger.SetHandler(logOutputStream)
|
2021-04-06 14:09:51 +00:00
|
|
|
|
2016-01-26 13:39:21 +00:00
|
|
|
// logging
|
2022-06-27 16:22:36 +00:00
|
|
|
verbosity := ctx.Int(verbosityFlag.Name)
|
2021-04-06 14:09:51 +00:00
|
|
|
glogger.Verbosity(log.Lvl(verbosity))
|
2022-06-27 16:22:36 +00:00
|
|
|
vmodule := ctx.String(vmoduleFlag.Name)
|
2021-04-06 14:09:51 +00:00
|
|
|
glogger.Vmodule(vmodule)
|
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
debug := ctx.Bool(debugFlag.Name)
|
|
|
|
if ctx.IsSet(debugFlag.Name) {
|
|
|
|
debug = ctx.Bool(debugFlag.Name)
|
2021-04-06 14:09:51 +00:00
|
|
|
}
|
|
|
|
log.PrintOrigins(debug)
|
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
backtrace := ctx.String(backtraceAtFlag.Name)
|
2021-04-06 14:09:51 +00:00
|
|
|
glogger.BacktraceAt(backtrace)
|
|
|
|
|
2017-02-22 12:10:07 +00:00
|
|
|
log.Root().SetHandler(glogger)
|
2016-01-26 13:39:21 +00:00
|
|
|
|
|
|
|
// profiling, tracing
|
2021-04-06 14:09:51 +00:00
|
|
|
runtime.MemProfileRate = memprofilerateFlag.Value
|
2022-06-27 16:22:36 +00:00
|
|
|
if ctx.IsSet(memprofilerateFlag.Name) {
|
|
|
|
runtime.MemProfileRate = ctx.Int(memprofilerateFlag.Name)
|
2021-04-06 14:09:51 +00:00
|
|
|
}
|
2020-05-05 08:19:17 +00:00
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
blockProfileRate := ctx.Int(blockprofilerateFlag.Name)
|
2021-04-06 14:09:51 +00:00
|
|
|
Handler.SetBlockProfileRate(blockProfileRate)
|
2020-05-05 08:19:17 +00:00
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
if traceFile := ctx.String(traceFlag.Name); traceFile != "" {
|
2016-05-06 09:15:05 +00:00
|
|
|
if err := Handler.StartGoTrace(traceFile); err != nil {
|
2016-01-26 13:39:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 08:19:17 +00:00
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
if cpuFile := ctx.String(cpuprofileFlag.Name); cpuFile != "" {
|
2016-01-26 13:39:21 +00:00
|
|
|
if err := Handler.StartCPUProfile(cpuFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pprof server
|
2022-06-27 16:22:36 +00:00
|
|
|
if ctx.Bool(pprofFlag.Name) {
|
|
|
|
listenHost := ctx.String(pprofAddrFlag.Name)
|
2020-05-05 08:19:17 +00:00
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
port := ctx.Int(pprofPortFlag.Name)
|
2020-05-05 08:19:17 +00:00
|
|
|
|
|
|
|
address := fmt.Sprintf("%s:%d", listenHost, port)
|
2020-07-03 17:12:22 +00:00
|
|
|
// This context value ("metrics.addr") represents the utils.MetricsHTTPFlag.Name.
|
|
|
|
// It cannot be imported because it will cause a cyclical dependency.
|
2022-06-27 16:22:36 +00:00
|
|
|
StartPProf(address, !ctx.IsSet("metrics.addr"))
|
2016-01-26 13:39:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 17:12:22 +00:00
|
|
|
func StartPProf(address string, withMetrics bool) {
|
2018-04-23 13:20:39 +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.
|
2020-07-03 17:12:22 +00:00
|
|
|
if withMetrics {
|
|
|
|
exp.Exp(metrics.DefaultRegistry)
|
|
|
|
}
|
2018-04-23 13:20:39 +00:00
|
|
|
http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize))
|
|
|
|
log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
|
|
|
|
go func() {
|
|
|
|
if err := http.ListenAndServe(address, nil); err != nil {
|
|
|
|
log.Error("Failure in running pprof server", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:39:21 +00:00
|
|
|
// Exit stops all running profiles, flushing their output to the
|
|
|
|
// respective file.
|
|
|
|
func Exit() {
|
|
|
|
Handler.StopCPUProfile()
|
2016-05-06 09:15:05 +00:00
|
|
|
Handler.StopGoTrace()
|
2022-11-11 10:33:18 +00:00
|
|
|
if closer, ok := logOutputStream.(io.Closer); ok {
|
|
|
|
closer.Close()
|
|
|
|
}
|
2016-01-26 13:39:21 +00:00
|
|
|
}
|