github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/internal/debug/trace.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  //+build go1.5
    19  
    20  package debug
    21  
    22  import (
    23  	"errors"
    24  	"os"
    25  	"runtime/trace"
    26  
    27  	"github.com/AigarNetwork/aigar/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  }