github.com/klaytn/klaytn@v1.12.1/api/debug/trace.go (about)

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