flags.go 4.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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"
21
	"io"
22 23
	"net/http"
	_ "net/http/pprof"
24
	"os"
25 26
	"runtime"

27
	"github.com/ethereum/go-ethereum/log"
28 29
	"github.com/ethereum/go-ethereum/log/term"
	colorable "github.com/mattn/go-colorable"
30
	"gopkg.in/urfave/cli.v1"
31 32 33
)

var (
34
	verbosityFlag = cli.IntFlag{
35
		Name:  "verbosity",
36
		Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
37
		Value: 3,
38
	}
39
	vmoduleFlag = cli.StringFlag{
40
		Name:  "vmodule",
41
		Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
42
		Value: "",
43
	}
44
	backtraceAtFlag = cli.StringFlag{
45 46
		Name:  "backtrace",
		Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
47
		Value: "",
48
	}
49 50 51 52
	debugFlag = cli.BoolFlag{
		Name:  "debug",
		Usage: "Prepends log messages with call-site location (file and line number)",
	}
53 54 55 56 57 58 59 60 61
	pprofFlag = cli.BoolFlag{
		Name:  "pprof",
		Usage: "Enable the pprof HTTP server",
	}
	pprofPortFlag = cli.IntFlag{
		Name:  "pprofport",
		Usage: "pprof HTTP server listening port",
		Value: 6060,
	}
62
	pprofAddrFlag = cli.StringFlag{
63
		Name:  "pprofaddr",
64 65 66
		Usage: "pprof HTTP server listening interface",
		Value: "127.0.0.1",
	}
67 68 69
	memprofilerateFlag = cli.IntFlag{
		Name:  "memprofilerate",
		Usage: "Turn on memory profiling with the given rate",
70
		Value: runtime.MemProfileRate,
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	}
	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{
88
	verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
89
	pprofFlag, pprofAddrFlag, pprofPortFlag,
90 91 92
	memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
}

93 94 95 96 97 98 99 100 101 102
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)))
}
103

104 105 106 107
// 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
108
	log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
109 110 111 112
	glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
	glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
	glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))
	log.Root().SetHandler(glogger)
113 114 115 116 117

	// profiling, tracing
	runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
	Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
	if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
118
		if err := Handler.StartGoTrace(traceFile); err != nil {
119 120 121 122 123 124 125 126 127 128 129
			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) {
130
		address := fmt.Sprintf("%s:%d", ctx.GlobalString(pprofAddrFlag.Name), ctx.GlobalInt(pprofPortFlag.Name))
131
		go func() {
132 133
			log.Info(fmt.Sprintf("starting pprof server at http://%s/debug/pprof", address))
			log.Error(fmt.Sprint(http.ListenAndServe(address, nil)))
134 135 136 137 138 139 140 141 142
		}()
	}
	return nil
}

// Exit stops all running profiles, flushing their output to the
// respective file.
func Exit() {
	Handler.StopCPUProfile()
143
	Handler.StopGoTrace()
144
}