github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/stacktrace/capture.go (about)

     1  package stacktrace
     2  
     3  import "runtime"
     4  
     5  // Capture captures a stacktrace for the current calling go program
     6  //
     7  // skip is the number of frames to skip
     8  func Capture(userSkip int) Stacktrace {
     9  	var (
    10  		skip   = userSkip + 1 // add one for our own function
    11  		frames []Frame
    12  		prevPc uintptr
    13  	)
    14  	for i := skip; ; i++ {
    15  		pc, file, line, ok := runtime.Caller(i)
    16  		//detect if caller is repeated to avoid loop, gccgo
    17  		//currently runs  into a loop without this check
    18  		if !ok || pc == prevPc {
    19  			break
    20  		}
    21  		frames = append(frames, NewFrame(pc, file, line))
    22  		prevPc = pc
    23  	}
    24  	return Stacktrace{
    25  		Frames: frames,
    26  	}
    27  }