2021-07-20 12:50:17 +00:00
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2021-09-04 20:33:06 +00:00
|
|
|
"fmt"
|
2021-07-20 12:50:17 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2022-10-18 10:06:34 +00:00
|
|
|
"runtime" // #nosec G702
|
2021-07-20 12:50:17 +00:00
|
|
|
"runtime/debug"
|
|
|
|
"runtime/pprof"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-10-25 15:01:04 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2021-09-15 08:46:01 +00:00
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
|
2021-09-04 20:33:06 +00:00
|
|
|
|
2021-11-25 15:12:57 +00:00
|
|
|
stderrors "github.com/pkg/errors"
|
2021-09-04 20:33:06 +00:00
|
|
|
|
2021-07-20 12:50:17 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
2021-11-25 15:12:57 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
"github.com/cerc-io/laconicd/rpc/backend"
|
|
|
|
rpctypes "github.com/cerc-io/laconicd/rpc/types"
|
2021-09-04 20:33:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-10-25 15:01:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2021-07-20 12:50:17 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HandlerT keeps track of the cpu profiler and trace execution
|
|
|
|
type HandlerT struct {
|
|
|
|
cpuFilename string
|
|
|
|
cpuFile io.WriteCloser
|
|
|
|
mu sync.Mutex
|
|
|
|
traceFilename string
|
|
|
|
traceFile io.WriteCloser
|
|
|
|
}
|
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
// API is the collection of tracing APIs exposed over the private debugging endpoint.
|
|
|
|
type API struct {
|
2022-10-10 10:38:33 +00:00
|
|
|
ctx *server.Context
|
|
|
|
logger log.Logger
|
|
|
|
backend backend.EVMBackend
|
|
|
|
handler *HandlerT
|
2021-07-20 12:50:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
// NewAPI creates a new API definition for the tracing methods of the Ethereum service.
|
|
|
|
func NewAPI(
|
2021-07-20 12:50:17 +00:00
|
|
|
ctx *server.Context,
|
2022-10-10 10:38:33 +00:00
|
|
|
backend backend.EVMBackend,
|
2021-09-04 20:33:06 +00:00
|
|
|
) *API {
|
|
|
|
return &API{
|
2022-10-10 10:38:33 +00:00
|
|
|
ctx: ctx,
|
|
|
|
logger: ctx.Logger.With("module", "debug"),
|
|
|
|
backend: backend,
|
|
|
|
handler: new(HandlerT),
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceTransaction returns the structured logs created during the execution of EVM
|
|
|
|
// and returns them as a JSON object.
|
|
|
|
func (a *API) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfig) (interface{}, error) {
|
|
|
|
a.logger.Debug("debug_traceTransaction", "hash", hash)
|
2022-10-10 10:38:33 +00:00
|
|
|
return a.backend.TraceTransaction(hash, config)
|
2021-07-20 12:50:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 08:46:01 +00:00
|
|
|
// TraceBlockByNumber returns the structured logs created during the execution of
|
|
|
|
// EVM and returns them as a JSON object.
|
|
|
|
func (a *API) TraceBlockByNumber(height rpctypes.BlockNumber, config *evmtypes.TraceConfig) ([]*evmtypes.TxTraceResult, error) {
|
|
|
|
a.logger.Debug("debug_traceBlockByNumber", "height", height)
|
|
|
|
if height == 0 {
|
|
|
|
return nil, errors.New("genesis is not traceable")
|
|
|
|
}
|
|
|
|
// Get Tendermint Block
|
2022-10-10 10:38:33 +00:00
|
|
|
resBlock, err := a.backend.TendermintBlockByNumber(height)
|
2021-09-15 08:46:01 +00:00
|
|
|
if err != nil {
|
2021-11-12 08:50:38 +00:00
|
|
|
a.logger.Debug("get block failed", "height", height, "error", err.Error())
|
2021-09-15 08:46:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
return a.backend.TraceBlock(rpctypes.BlockNumber(resBlock.Block.Height), config, resBlock)
|
2021-09-15 08:46:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 08:50:38 +00:00
|
|
|
// TraceBlockByHash returns the structured logs created during the execution of
|
|
|
|
// EVM and returns them as a JSON object.
|
|
|
|
func (a *API) TraceBlockByHash(hash common.Hash, config *evmtypes.TraceConfig) ([]*evmtypes.TxTraceResult, error) {
|
|
|
|
a.logger.Debug("debug_traceBlockByHash", "hash", hash)
|
|
|
|
// Get Tendermint Block
|
2022-10-10 10:38:33 +00:00
|
|
|
resBlock, err := a.backend.TendermintBlockByHash(hash)
|
2021-11-12 08:50:38 +00:00
|
|
|
if err != nil {
|
|
|
|
a.logger.Debug("get block failed", "hash", hash.Hex(), "error", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resBlock == nil || resBlock.Block == nil {
|
|
|
|
a.logger.Debug("block not found", "hash", hash.Hex())
|
|
|
|
return nil, errors.New("block not found")
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
return a.backend.TraceBlock(rpctypes.BlockNumber(resBlock.Block.Height), config, resBlock)
|
2021-09-15 08:46:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 12:50:17 +00:00
|
|
|
// BlockProfile turns on goroutine profiling for nsec seconds and writes profile data to
|
|
|
|
// file. It uses a profile rate of 1 for most accurate information. If a different rate is
|
|
|
|
// desired, set the rate and write the profile manually.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) BlockProfile(file string, nsec uint) error {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_blockProfile", "file", file, "nsec", nsec)
|
|
|
|
runtime.SetBlockProfileRate(1)
|
|
|
|
defer runtime.SetBlockProfileRate(0)
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(nsec) * time.Second)
|
|
|
|
return writeProfile("block", file, a.logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CpuProfile turns on CPU profiling for nsec seconds and writes
|
|
|
|
// profile data to file.
|
2022-10-10 10:38:33 +00:00
|
|
|
func (a *API) CpuProfile(file string, nsec uint) error { //nolint: golint, stylecheck, revive
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_cpuProfile", "file", file, "nsec", nsec)
|
|
|
|
if err := a.StartCPUProfile(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(nsec) * time.Second)
|
|
|
|
return a.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GcStats returns GC statistics.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) GcStats() *debug.GCStats {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_gcStats")
|
|
|
|
s := new(debug.GCStats)
|
|
|
|
debug.ReadGCStats(s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// GoTrace turns on tracing for nsec seconds and writes
|
|
|
|
// trace data to file.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) GoTrace(file string, nsec uint) error {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_goTrace", "file", file, "nsec", nsec)
|
|
|
|
if err := a.StartGoTrace(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(nsec) * time.Second)
|
|
|
|
return a.StopGoTrace()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MemStats returns detailed runtime memory statistics.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) MemStats() *runtime.MemStats {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_memStats")
|
|
|
|
s := new(runtime.MemStats)
|
|
|
|
runtime.ReadMemStats(s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBlockProfileRate sets the rate of goroutine block profile data collection.
|
|
|
|
// rate 0 disables block profiling.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) SetBlockProfileRate(rate int) {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_setBlockProfileRate", "rate", rate)
|
|
|
|
runtime.SetBlockProfileRate(rate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stacks returns a printed representation of the stacks of all goroutines.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) Stacks() string {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_stacks")
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := pprof.Lookup("goroutine").WriteTo(buf, 2)
|
|
|
|
if err != nil {
|
|
|
|
a.logger.Error("Failed to create stacks", "error", err.Error())
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartCPUProfile turns on CPU profiling, writing to the given file.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) StartCPUProfile(file string) error {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_startCPUProfile", "file", file)
|
|
|
|
a.handler.mu.Lock()
|
|
|
|
defer a.handler.mu.Unlock()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isCPUProfileConfigurationActivated(a.ctx):
|
|
|
|
a.logger.Debug("CPU profiling already in progress using the configuration file")
|
|
|
|
return errors.New("CPU profiling already in progress using the configuration file")
|
|
|
|
case a.handler.cpuFile != nil:
|
|
|
|
a.logger.Debug("CPU profiling already in progress")
|
|
|
|
return errors.New("CPU profiling already in progress")
|
|
|
|
default:
|
2021-10-13 10:52:05 +00:00
|
|
|
fp, err := ExpandHome(file)
|
|
|
|
if err != nil {
|
|
|
|
a.logger.Debug("failed to get filepath for the CPU profile file", "error", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Create(fp)
|
2021-07-20 12:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
a.logger.Debug("failed to create CPU profile file", "error", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
|
|
|
a.logger.Debug("cpu profiling already in use", "error", err.Error())
|
2021-11-25 15:12:57 +00:00
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
a.logger.Debug("failed to close cpu profile file")
|
|
|
|
return stderrors.Wrap(err, "failed to close cpu profile file")
|
|
|
|
}
|
2021-07-20 12:50:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Info("CPU profiling started", "profile", file)
|
|
|
|
a.handler.cpuFile = f
|
|
|
|
a.handler.cpuFilename = file
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopCPUProfile stops an ongoing CPU profile.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) StopCPUProfile() error {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_stopCPUProfile")
|
|
|
|
a.handler.mu.Lock()
|
|
|
|
defer a.handler.mu.Unlock()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isCPUProfileConfigurationActivated(a.ctx):
|
|
|
|
a.logger.Debug("CPU profiling already in progress using the configuration file")
|
|
|
|
return errors.New("CPU profiling already in progress using the configuration file")
|
|
|
|
case a.handler.cpuFile != nil:
|
|
|
|
a.logger.Info("Done writing CPU profile", "profile", a.handler.cpuFilename)
|
|
|
|
pprof.StopCPUProfile()
|
2021-11-25 15:12:57 +00:00
|
|
|
if err := a.handler.cpuFile.Close(); err != nil {
|
|
|
|
a.logger.Debug("failed to close cpu file")
|
|
|
|
return stderrors.Wrap(err, "failed to close cpu file")
|
|
|
|
}
|
2021-07-20 12:50:17 +00:00
|
|
|
a.handler.cpuFile = nil
|
|
|
|
a.handler.cpuFilename = ""
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
a.logger.Debug("CPU profiling not in progress")
|
|
|
|
return errors.New("CPU profiling not in progress")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteBlockProfile writes a goroutine blocking profile to the given file.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) WriteBlockProfile(file string) error {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_writeBlockProfile", "file", file)
|
|
|
|
return writeProfile("block", file, a.logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMemProfile writes an allocation profile to the given file.
|
|
|
|
// Note that the profiling rate cannot be set through the API,
|
|
|
|
// it must be set on the command line.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) WriteMemProfile(file string) error {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_writeMemProfile", "file", file)
|
|
|
|
return writeProfile("heap", file, a.logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MutexProfile turns on mutex profiling for nsec seconds and writes profile data to file.
|
|
|
|
// It uses a profile rate of 1 for most accurate information. If a different rate is
|
|
|
|
// desired, set the rate and write the profile manually.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) MutexProfile(file string, nsec uint) error {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_mutexProfile", "file", file, "nsec", nsec)
|
|
|
|
runtime.SetMutexProfileFraction(1)
|
|
|
|
time.Sleep(time.Duration(nsec) * time.Second)
|
|
|
|
defer runtime.SetMutexProfileFraction(0)
|
|
|
|
return writeProfile("mutex", file, a.logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMutexProfileFraction sets the rate of mutex profiling.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) SetMutexProfileFraction(rate int) {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_setMutexProfileFraction", "rate", rate)
|
|
|
|
runtime.SetMutexProfileFraction(rate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMutexProfile writes a goroutine blocking profile to the given file.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) WriteMutexProfile(file string) error {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_writeMutexProfile", "file", file)
|
|
|
|
return writeProfile("mutex", file, a.logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FreeOSMemory forces a garbage collection.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) FreeOSMemory() {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_freeOSMemory")
|
|
|
|
debug.FreeOSMemory()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGCPercent sets the garbage collection target percentage. It returns the previous
|
|
|
|
// setting. A negative value disables GC.
|
2021-09-04 20:33:06 +00:00
|
|
|
func (a *API) SetGCPercent(v int) int {
|
2021-07-20 12:50:17 +00:00
|
|
|
a.logger.Debug("debug_setGCPercent", "percent", v)
|
|
|
|
return debug.SetGCPercent(v)
|
|
|
|
}
|
2021-10-25 15:01:04 +00:00
|
|
|
|
|
|
|
// GetHeaderRlp retrieves the RLP encoded for of a single header.
|
|
|
|
func (a *API) GetHeaderRlp(number uint64) (hexutil.Bytes, error) {
|
|
|
|
header, err := a.backend.HeaderByNumber(rpctypes.BlockNumber(number))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rlp.EncodeToBytes(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockRlp retrieves the RLP encoded for of a single block.
|
|
|
|
func (a *API) GetBlockRlp(number uint64) (hexutil.Bytes, error) {
|
2022-10-10 10:38:33 +00:00
|
|
|
block, err := a.backend.EthBlockByNumber(rpctypes.BlockNumber(number))
|
2021-10-25 15:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rlp.EncodeToBytes(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintBlock retrieves a block and returns its pretty printed form.
|
|
|
|
func (a *API) PrintBlock(number uint64) (string, error) {
|
2022-10-10 10:38:33 +00:00
|
|
|
block, err := a.backend.EthBlockByNumber(rpctypes.BlockNumber(number))
|
2021-10-25 15:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return spew.Sdump(block), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeedHash retrieves the seed hash of a block.
|
|
|
|
func (a *API) SeedHash(number uint64) (string, error) {
|
|
|
|
_, err := a.backend.HeaderByNumber(rpctypes.BlockNumber(number))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("0x%x", ethash.SeedHash(number)), nil
|
|
|
|
}
|
2021-11-16 14:29:51 +00:00
|
|
|
|
|
|
|
// IntermediateRoots executes a block, and returns a list
|
|
|
|
// of intermediate roots: the stateroot after each transaction.
|
|
|
|
func (a *API) IntermediateRoots(hash common.Hash, _ *evmtypes.TraceConfig) ([]common.Hash, error) {
|
|
|
|
a.logger.Debug("debug_intermediateRoots", "hash", hash)
|
|
|
|
return ([]common.Hash)(nil), nil
|
|
|
|
}
|