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