2016-01-26 14:39:21 +01: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 19:31:13 +01:00
|
|
|
"io"
|
2024-03-22 13:17:59 +01:00
|
|
|
"log/slog"
|
2023-07-02 19:21:16 +08:00
|
|
|
"net"
|
2016-01-26 14:39:21 +01:00
|
|
|
"net/http"
|
2023-04-03 14:35:36 +05:30
|
|
|
_ "net/http/pprof"
|
2017-02-22 14:10:07 +02:00
|
|
|
"os"
|
2023-04-03 14:35:36 +05:30
|
|
|
"path/filepath"
|
2016-01-26 14:39:21 +01:00
|
|
|
"runtime"
|
|
|
|
|
2022-06-27 18:22:36 +02:00
|
|
|
"github.com/ethereum/go-ethereum/internal/flags"
|
2017-02-22 14:10:07 +02:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-02-23 10:56:08 +01:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
|
|
"github.com/ethereum/go-ethereum/metrics/exp"
|
2018-04-23 15:20:39 +02:00
|
|
|
"github.com/fjl/memsize/memsizeui"
|
2020-11-25 21:00:23 +01:00
|
|
|
"github.com/mattn/go-colorable"
|
2018-09-29 23:15:39 +09:00
|
|
|
"github.com/mattn/go-isatty"
|
2022-06-27 18:22:36 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2023-04-03 14:35:36 +05:30
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2016-01-26 14:39:21 +01:00
|
|
|
)
|
|
|
|
|
2018-04-23 15:20:39 +02:00
|
|
|
var Memsize memsizeui.Handler
|
|
|
|
|
2016-01-26 14:39:21 +01:00
|
|
|
var (
|
2022-06-27 18:22:36 +02: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,
|
|
|
|
}
|
2023-04-19 21:18:02 +10:00
|
|
|
logVmoduleFlag = &cli.StringFlag{
|
|
|
|
Name: "log.vmodule",
|
|
|
|
Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
|
|
|
|
Value: "",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
2022-06-27 18:22:36 +02:00
|
|
|
vmoduleFlag = &cli.StringFlag{
|
|
|
|
Name: "vmodule",
|
|
|
|
Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
|
|
|
|
Value: "",
|
2023-04-19 21:18:02 +10:00
|
|
|
Hidden: true,
|
2022-06-27 18:22:36 +02:00
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
logjsonFlag = &cli.BoolFlag{
|
|
|
|
Name: "log.json",
|
|
|
|
Usage: "Format logs with JSON",
|
2023-03-30 23:24:48 +10:00
|
|
|
Hidden: true,
|
2022-06-27 18:22:36 +02:00
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
2023-03-30 23:24:48 +10:00
|
|
|
logFormatFlag = &cli.StringFlag{
|
|
|
|
Name: "log.format",
|
|
|
|
Usage: "Log format to use (json|logfmt|terminal)",
|
2023-03-28 22:37:40 +10:00
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
2022-11-11 11:33:18 +01:00
|
|
|
logFileFlag = &cli.StringFlag{
|
|
|
|
Name: "log.file",
|
|
|
|
Usage: "Write logs to a file",
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
2023-04-03 14:35:36 +05:30
|
|
|
logRotateFlag = &cli.BoolFlag{
|
2023-09-25 16:02:44 +02:00
|
|
|
Name: "log.rotate",
|
|
|
|
Usage: "Enables log file rotation",
|
|
|
|
Category: flags.LoggingCategory,
|
2023-04-03 14:35:36 +05:30
|
|
|
}
|
|
|
|
logMaxSizeMBsFlag = &cli.IntFlag{
|
|
|
|
Name: "log.maxsize",
|
|
|
|
Usage: "Maximum size in MBs of a single log file",
|
|
|
|
Value: 100,
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
logMaxBackupsFlag = &cli.IntFlag{
|
|
|
|
Name: "log.maxbackups",
|
|
|
|
Usage: "Maximum number of log files to retain",
|
|
|
|
Value: 10,
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
logMaxAgeFlag = &cli.IntFlag{
|
|
|
|
Name: "log.maxage",
|
|
|
|
Usage: "Maximum number of days to retain a log file",
|
|
|
|
Value: 30,
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
|
|
|
logCompressFlag = &cli.BoolFlag{
|
|
|
|
Name: "log.compress",
|
|
|
|
Usage: "Compress the log files",
|
|
|
|
Value: false,
|
|
|
|
Category: flags.LoggingCategory,
|
|
|
|
}
|
2022-06-27 18:22:36 +02:00
|
|
|
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 14:39:21 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Flags holds all command-line flags required for debugging.
|
|
|
|
var Flags = []cli.Flag{
|
2021-04-06 18:39:51 +04:30
|
|
|
verbosityFlag,
|
2023-04-19 21:18:02 +10:00
|
|
|
logVmoduleFlag,
|
2021-04-06 18:39:51 +04:30
|
|
|
vmoduleFlag,
|
|
|
|
logjsonFlag,
|
2023-03-30 23:24:48 +10:00
|
|
|
logFormatFlag,
|
2022-11-11 11:33:18 +01:00
|
|
|
logFileFlag,
|
2023-04-03 14:35:36 +05:30
|
|
|
logRotateFlag,
|
|
|
|
logMaxSizeMBsFlag,
|
|
|
|
logMaxBackupsFlag,
|
|
|
|
logMaxAgeFlag,
|
|
|
|
logCompressFlag,
|
2021-04-06 18:39:51 +04:30
|
|
|
pprofFlag,
|
|
|
|
pprofAddrFlag,
|
|
|
|
pprofPortFlag,
|
|
|
|
memprofilerateFlag,
|
|
|
|
blockprofilerateFlag,
|
|
|
|
cpuprofileFlag,
|
|
|
|
traceFlag,
|
2020-05-05 10:19:17 +02:00
|
|
|
}
|
|
|
|
|
2022-11-11 11:33:18 +01:00
|
|
|
var (
|
2024-01-12 15:59:03 +02:00
|
|
|
glogger *log.GlogHandler
|
|
|
|
logOutputFile io.WriteCloser
|
2022-11-11 11:33:18 +01:00
|
|
|
)
|
2017-02-23 19:31:13 +01:00
|
|
|
|
|
|
|
func init() {
|
2024-01-12 15:59:03 +02:00
|
|
|
glogger = log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, false))
|
2017-02-23 19:31:13 +01:00
|
|
|
}
|
2017-02-22 14:10:07 +02:00
|
|
|
|
2016-01-26 14:39:21 +01: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 13:57:33 +01:00
|
|
|
func Setup(ctx *cli.Context) error {
|
2023-04-03 14:35:36 +05:30
|
|
|
var (
|
2023-11-29 15:33:50 +08:00
|
|
|
handler slog.Handler
|
|
|
|
terminalOutput = io.Writer(os.Stderr)
|
|
|
|
output io.Writer
|
|
|
|
logFmtFlag = ctx.String(logFormatFlag.Name)
|
2023-04-03 14:35:36 +05:30
|
|
|
)
|
|
|
|
var (
|
2023-10-25 17:57:12 +02:00
|
|
|
logFile = ctx.String(logFileFlag.Name)
|
|
|
|
rotation = ctx.Bool(logRotateFlag.Name)
|
2023-04-03 14:35:36 +05:30
|
|
|
)
|
|
|
|
if len(logFile) > 0 {
|
|
|
|
if err := validateLogLocation(filepath.Dir(logFile)); err != nil {
|
|
|
|
return fmt.Errorf("failed to initiatilize file logger: %v", err)
|
2022-11-11 11:33:18 +01:00
|
|
|
}
|
2023-04-03 14:35:36 +05:30
|
|
|
}
|
|
|
|
context := []interface{}{"rotate", rotation}
|
|
|
|
if len(logFmtFlag) > 0 {
|
|
|
|
context = append(context, "format", logFmtFlag)
|
2022-11-11 11:33:18 +01:00
|
|
|
} else {
|
2023-04-03 14:35:36 +05:30
|
|
|
context = append(context, "format", "terminal")
|
|
|
|
}
|
|
|
|
if rotation {
|
|
|
|
// Lumberjack uses <processname>-lumberjack.log in is.TempDir() if empty.
|
|
|
|
// so typically /tmp/geth-lumberjack.log on linux
|
|
|
|
if len(logFile) > 0 {
|
|
|
|
context = append(context, "location", logFile)
|
|
|
|
} else {
|
|
|
|
context = append(context, "location", filepath.Join(os.TempDir(), "geth-lumberjack.log"))
|
|
|
|
}
|
2023-11-29 15:33:50 +08:00
|
|
|
logOutputFile = &lumberjack.Logger{
|
2023-04-03 14:35:36 +05:30
|
|
|
Filename: logFile,
|
|
|
|
MaxSize: ctx.Int(logMaxSizeMBsFlag.Name),
|
|
|
|
MaxBackups: ctx.Int(logMaxBackupsFlag.Name),
|
|
|
|
MaxAge: ctx.Int(logMaxAgeFlag.Name),
|
|
|
|
Compress: ctx.Bool(logCompressFlag.Name),
|
2023-10-25 17:57:12 +02:00
|
|
|
}
|
2023-11-29 15:33:50 +08:00
|
|
|
output = io.MultiWriter(terminalOutput, logOutputFile)
|
2023-04-03 14:35:36 +05:30
|
|
|
} else if logFile != "" {
|
2023-11-29 15:33:50 +08:00
|
|
|
var err error
|
|
|
|
if logOutputFile, err = os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err != nil {
|
2023-04-03 14:35:36 +05:30
|
|
|
return err
|
2021-02-09 10:42:55 +01:00
|
|
|
}
|
2023-11-29 15:33:50 +08:00
|
|
|
output = io.MultiWriter(logOutputFile, terminalOutput)
|
2023-10-25 17:57:12 +02:00
|
|
|
context = append(context, "location", logFile)
|
2023-11-29 15:33:50 +08:00
|
|
|
} else {
|
|
|
|
output = terminalOutput
|
2021-02-09 10:42:55 +01:00
|
|
|
}
|
2023-11-29 15:33:50 +08:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.Bool(logjsonFlag.Name):
|
|
|
|
// Retain backwards compatibility with `--log.json` flag if `--log.format` not set
|
|
|
|
defer log.Warn("The flag '--log.json' is deprecated, please use '--log.format=json' instead")
|
2024-04-06 03:17:41 -07:00
|
|
|
handler = log.JSONHandlerWithLevel(output, log.LevelInfo)
|
2023-11-29 15:33:50 +08:00
|
|
|
case logFmtFlag == "json":
|
2024-04-06 03:17:41 -07:00
|
|
|
handler = log.JSONHandlerWithLevel(output, log.LevelInfo)
|
2023-11-29 15:33:50 +08:00
|
|
|
case logFmtFlag == "logfmt":
|
|
|
|
handler = log.LogfmtHandler(output)
|
|
|
|
case logFmtFlag == "", logFmtFlag == "terminal":
|
|
|
|
useColor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
|
|
|
if useColor {
|
|
|
|
terminalOutput = colorable.NewColorableStderr()
|
|
|
|
if logOutputFile != nil {
|
|
|
|
output = io.MultiWriter(logOutputFile, terminalOutput)
|
|
|
|
} else {
|
|
|
|
output = terminalOutput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler = log.NewTerminalHandler(output, useColor)
|
|
|
|
default:
|
|
|
|
// Unknown log format specified
|
|
|
|
return fmt.Errorf("unknown log format: %v", ctx.String(logFormatFlag.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
glogger = log.NewGlogHandler(handler)
|
2021-04-06 18:39:51 +04:30
|
|
|
|
2016-01-26 14:39:21 +01:00
|
|
|
// logging
|
2023-11-29 15:33:50 +08:00
|
|
|
verbosity := log.FromLegacyLevel(ctx.Int(verbosityFlag.Name))
|
|
|
|
glogger.Verbosity(verbosity)
|
2023-04-19 21:18:02 +10:00
|
|
|
vmodule := ctx.String(logVmoduleFlag.Name)
|
|
|
|
if vmodule == "" {
|
|
|
|
// Retain backwards compatibility with `--vmodule` flag if `--log.vmodule` not set
|
|
|
|
vmodule = ctx.String(vmoduleFlag.Name)
|
|
|
|
if vmodule != "" {
|
|
|
|
defer log.Warn("The flag '--vmodule' is deprecated, please use '--log.vmodule' instead")
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 18:39:51 +04:30
|
|
|
glogger.Vmodule(vmodule)
|
|
|
|
|
2023-11-29 15:33:50 +08:00
|
|
|
log.SetDefault(log.NewLogger(glogger))
|
2016-01-26 14:39:21 +01:00
|
|
|
|
|
|
|
// profiling, tracing
|
2021-04-06 18:39:51 +04:30
|
|
|
runtime.MemProfileRate = memprofilerateFlag.Value
|
2022-06-27 18:22:36 +02:00
|
|
|
if ctx.IsSet(memprofilerateFlag.Name) {
|
|
|
|
runtime.MemProfileRate = ctx.Int(memprofilerateFlag.Name)
|
2021-04-06 18:39:51 +04:30
|
|
|
}
|
2020-05-05 10:19:17 +02:00
|
|
|
|
2022-06-27 18:22:36 +02:00
|
|
|
blockProfileRate := ctx.Int(blockprofilerateFlag.Name)
|
2021-04-06 18:39:51 +04:30
|
|
|
Handler.SetBlockProfileRate(blockProfileRate)
|
2020-05-05 10:19:17 +02:00
|
|
|
|
2022-06-27 18:22:36 +02:00
|
|
|
if traceFile := ctx.String(traceFlag.Name); traceFile != "" {
|
2016-05-06 11:15:05 +02:00
|
|
|
if err := Handler.StartGoTrace(traceFile); err != nil {
|
2016-01-26 14:39:21 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 10:19:17 +02:00
|
|
|
|
2022-06-27 18:22:36 +02:00
|
|
|
if cpuFile := ctx.String(cpuprofileFlag.Name); cpuFile != "" {
|
2016-01-26 14:39:21 +01:00
|
|
|
if err := Handler.StartCPUProfile(cpuFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pprof server
|
2022-06-27 18:22:36 +02:00
|
|
|
if ctx.Bool(pprofFlag.Name) {
|
|
|
|
listenHost := ctx.String(pprofAddrFlag.Name)
|
2020-05-05 10:19:17 +02:00
|
|
|
|
2022-06-27 18:22:36 +02:00
|
|
|
port := ctx.Int(pprofPortFlag.Name)
|
2020-05-05 10:19:17 +02:00
|
|
|
|
2023-07-02 19:21:16 +08:00
|
|
|
address := net.JoinHostPort(listenHost, fmt.Sprintf("%d", port))
|
2020-07-03 12:12:22 -05: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 18:22:36 +02:00
|
|
|
StartPProf(address, !ctx.IsSet("metrics.addr"))
|
2016-01-26 14:39:21 +01:00
|
|
|
}
|
2023-04-03 14:35:36 +05:30
|
|
|
if len(logFile) > 0 || rotation {
|
|
|
|
log.Info("Logging configured", context...)
|
|
|
|
}
|
2016-01-26 14:39:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 12:12:22 -05:00
|
|
|
func StartPProf(address string, withMetrics bool) {
|
2018-04-23 15:20:39 +02: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 12:12:22 -05:00
|
|
|
if withMetrics {
|
|
|
|
exp.Exp(metrics.DefaultRegistry)
|
|
|
|
}
|
2018-04-23 15:20:39 +02: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 14:39:21 +01:00
|
|
|
// Exit stops all running profiles, flushing their output to the
|
|
|
|
// respective file.
|
|
|
|
func Exit() {
|
|
|
|
Handler.StopCPUProfile()
|
2016-05-06 11:15:05 +02:00
|
|
|
Handler.StopGoTrace()
|
2023-11-29 15:33:50 +08:00
|
|
|
if logOutputFile != nil {
|
|
|
|
logOutputFile.Close()
|
2022-11-11 11:33:18 +01:00
|
|
|
}
|
2016-01-26 14:39:21 +01:00
|
|
|
}
|
2023-04-03 14:35:36 +05:30
|
|
|
|
|
|
|
func validateLogLocation(path string) error {
|
|
|
|
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("error creating the directory: %w", err)
|
|
|
|
}
|
|
|
|
// Check if the path is writable by trying to create a temporary file
|
|
|
|
tmp := filepath.Join(path, "tmp")
|
|
|
|
if f, err := os.Create(tmp); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
return os.Remove(tmp)
|
|
|
|
}
|