github.com/ava-labs/subnet-evm@v0.6.4/internal/debug/trace.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2016 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package debug
    28  
    29  import (
    30  	"errors"
    31  	"os"
    32  	"runtime/trace"
    33  
    34  	"github.com/ethereum/go-ethereum/log"
    35  )
    36  
    37  // StartGoTrace turns on tracing, writing to the given file.
    38  func (h *HandlerT) StartGoTrace(file string) error {
    39  	h.mu.Lock()
    40  	defer h.mu.Unlock()
    41  	if h.traceW != nil {
    42  		return errors.New("trace already in progress")
    43  	}
    44  	f, err := os.Create(expandHome(file))
    45  	if err != nil {
    46  		return err
    47  	}
    48  	if err := trace.Start(f); err != nil {
    49  		f.Close()
    50  		return err
    51  	}
    52  	h.traceW = f
    53  	h.traceFile = file
    54  	log.Info("Go tracing started", "dump", h.traceFile)
    55  	return nil
    56  }
    57  
    58  // StopGoTrace stops an ongoing trace.
    59  func (h *HandlerT) StopGoTrace() error {
    60  	h.mu.Lock()
    61  	defer h.mu.Unlock()
    62  	trace.Stop()
    63  	if h.traceW == nil {
    64  		return errors.New("trace not in progress")
    65  	}
    66  	log.Info("Done writing Go trace", "dump", h.traceFile)
    67  	h.traceW.Close()
    68  	h.traceW = nil
    69  	h.traceFile = ""
    70  	return nil
    71  }