github.com/mprishchepo/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/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  //+build go1.5
    18  
    19  package debug
    20  
    21  import (
    22  	"errors"
    23  	"os"
    24  	"runtime/trace"
    25  
    26  	"github.com/Fantom-foundation/go-ethereum/log"
    27  )
    28  
    29  // StartGoTrace turns on tracing, writing to the given file.
    30  func (h *HandlerT) StartGoTrace(file string) error {
    31  	h.mu.Lock()
    32  	defer h.mu.Unlock()
    33  	if h.traceW != nil {
    34  		return errors.New("trace already in progress")
    35  	}
    36  	f, err := os.Create(expandHome(file))
    37  	if err != nil {
    38  		return err
    39  	}
    40  	if err := trace.Start(f); err != nil {
    41  		f.Close()
    42  		return err
    43  	}
    44  	h.traceW = f
    45  	h.traceFile = file
    46  	log.Info("Go tracing started", "dump", h.traceFile)
    47  	return nil
    48  }
    49  
    50  // StopTrace stops an ongoing trace.
    51  func (h *HandlerT) StopGoTrace() error {
    52  	h.mu.Lock()
    53  	defer h.mu.Unlock()
    54  	trace.Stop()
    55  	if h.traceW == nil {
    56  		return errors.New("trace not in progress")
    57  	}
    58  	log.Info("Done writing Go trace", "dump", h.traceFile)
    59  	h.traceW.Close()
    60  	h.traceW = nil
    61  	h.traceFile = ""
    62  	return nil
    63  }