github.com/ethereum/go-ethereum@v1.14.3/internal/debug/trace.go (about)

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