// stack returns a nicely formatted stack frame, skipping skip frames
181398
func stack(skip int) []byte {
	buf := new(bytes.Buffer) // the returned data
	// As we loop, we open files and read them. These variables record the currently
	// loaded file.
	var lines [][]byte
	var lastFile string
	for i := skip; ; i++ { // Skip the expected number of frames
		pc, file, line, ok := runtime.Caller(i)
		if !ok {
			break
		}
		// Print this much at least.  If we can't find the source, it won't show.
		fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
		if file != lastFile {
			data, err := ioutil.ReadFile(file)
			if err != nil {
				continue
			}
			lines = bytes.Split(data, []byte{'\n'})
			lastFile = file
		}
		fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
	}
	return buf.Bytes()
}
1166227
func GetCallStack(skip int, frames int, filter string) string {
	buf := new(bytes.Buffer)
	for i, count := skip, 0; count < frames; i++ {
		_, file, line, ok := runtime.Caller(i)
		if !ok {
			break
		}
		if filter == "" || strings.Contains(file, filter) {
			fmt.Fprintf(buf, "\n%s:%d", file, line)
			count++
		}
	}
	return buf.String()
}
41436
func (g *G) Stacktrace(depth int, readDefers bool) ([]Stackframe, error) {
	it, err := g.stackIterator()
	if err != nil {
		return nil, err
	}
	frames, err := it.stacktrace(depth)
	if err != nil {
		return nil, err
	}
	if readDefers {
		g.readDefers(frames)
	}
	return frames, nil
}
914335
func (self *Stack) Pop() Token {
	if len(self.Values) == 0 {
		return Token{}
	}
	token := self.Values[len(self.Values)-1]
	self.Values = self.Values[:len(self.Values)-1]
	return token
}
907927
func (s *Stack) Push(v interface{}) error {
	spin := s.spin
	for {
		head := atomic.LoadUint32(s.head)
		unlockedHead := head & unlockMask
		lockedHead := head | lockMask

		// Always work with unlocked head as head might be locked
		if unlockedHead == unlockMask {
			return LimitError{"Stack is full"}
		}

		if atomic.CompareAndSwapUint32(s.head, unlockedHead, lockedHead) {
			if unlockedHead == uint32(len(s.data)) {
				// Grow stack
				old := s.data
				s.data = make([]interface{}, len(s.data)+s.growBy)
				copy(s.data, old)
			}

			s.data[unlockedHead] = v                   // write to new head
			atomic.StoreUint32(s.head, unlockedHead+1) // unlock
			return nil                                 // ### return ###
		}

		spin.Yield()
	}
}
1262393
func (m *Mux) SetStack(stack []plugin.Plugin) {
	m.Middleware.SetStack(stack)
}
73474
func (it *stackIterator) Next() bool {
	if it.err != nil || it.atend {
		return false
	}
	callFrameRegs, ret, retaddr := it.advanceRegs()
	it.frame = it.newStackframe(ret, retaddr)

	if it.stkbar != nil && it.frame.Ret == it.stackBarrierPC && it.frame.addrret == it.stkbar[0].ptr {
		// Skip stack barrier frames
		it.frame.Ret = it.stkbar[0].val
		it.stkbar = it.stkbar[1:]
	}

	if it.switchStack() {
		return true
	}

	if it.frame.Ret <= 0 {
		it.atend = true
		return true
	}

	it.top = false
	it.pc = it.frame.Ret
	it.regs = callFrameRegs
	return true
}
808735
func (pc popChannel) stack(cmd ...*URLContext) {
	toStack := cmd
	for {
		select {
		case pc <- toStack:
			return
		case old := <-pc:
			// Content of the channel got emptied and is now in old, so append whatever
			// is in toStack to it, so that it can either be inserted in the channel,
			// or appended to some other content that got through in the meantime.
			toStack = append(old, toStack...)
		}
	}
}
1185644
func buildStack(frames []runtime.Frame) stack {
	stack := make(stack, len(frames))

	for i, fr := range frames {
		file := shortenFilePath(fr.File)
		stack[i] = frame{file, functionName(fr.Function), fr.Line}
	}

	return stack
}
760864
func (stack *CoordStack) Push(data []float64, idx int) []float64 {
	c := data[idx : idx+stack.stride]
	stack.Data = append(stack.Data, c...)
	return c
}
1074440
func stacksplit(ctxt *liblink.Link, p *liblink.Prog, framesize int64, noctxt int, jmpok **liblink.Prog) *liblink.Prog {
	var q *liblink.Prog
	var q1 *liblink.Prog
	var arg int
	if ctxt.Debugstack != 0 {
		// 8l -K means check not only for stack
		// overflow but stack underflow.
		// On underflow, INT 3 (breakpoint).
		// Underflow itself is rare but this also
		// catches out-of-sync stack guard info.
		p = liblink.Appendp(ctxt, p)
		p.As = ACMPL
		p.From.Typ = D_INDIR + D_CX
		p.From.Offset = 4
		p.To.Typ = D_SP
		p = liblink.Appendp(ctxt, p)
		p.As = AJCC
		p.To.Typ = D_BRANCH
		p.To.Offset = 4
		q1 = p
		p = liblink.Appendp(ctxt, p)
		p.As = AINT
		p.From.Typ = D_CONST
		p.From.Offset = 3
		p = liblink.Appendp(ctxt, p)
		p.As = ANOP
		q1.Pcond = p
	}
	q1 = nil
	if framesize <= liblink.StackSmall {
		// small stack: SP <= stackguard
		//	CMPL SP, stackguard
		p = liblink.Appendp(ctxt, p)
		p.As = ACMPL
		p.From.Typ = D_SP
		p.To.Typ = D_INDIR + D_CX
	} else if framesize <= liblink.StackBig {
		// large stack: SP-framesize <= stackguard-StackSmall
		//	LEAL -(framesize-StackSmall)(SP), AX
		//	CMPL AX, stackguard
		p = liblink.Appendp(ctxt, p)
		p.As = ALEAL
		p.From.Typ = D_INDIR + D_SP
		p.From.Offset = -(framesize - liblink.StackSmall)
		p.To.Typ = D_AX
		p = liblink.Appendp(ctxt, p)
		p.As = ACMPL
		p.From.Typ = D_AX
		p.To.Typ = D_INDIR + D_CX
	} else {
		// Such a large stack we need to protect against wraparound
		// if SP is close to zero.
		//	SP-stackguard+StackGuard <= framesize + (StackGuard-StackSmall)
		// The +StackGuard on both sides is required to keep the left side positive:
		// SP is allowed to be slightly below stackguard. See stack.h.
		//
		// Preemption sets stackguard to StackPreempt, a very large value.
		// That breaks the math above, so we have to check for that explicitly.
		//	MOVL	stackguard, CX
		//	CMPL	CX, $StackPreempt
		//	JEQ	label-of-call-to-morestack
		//	LEAL	StackGuard(SP), AX
		//	SUBL	stackguard, AX
		//	CMPL	AX, $(framesize+(StackGuard-StackSmall))
		p = liblink.Appendp(ctxt, p)
		p.As = AMOVL
		p.From.Typ = D_INDIR + D_CX
		p.From.Offset = 0
		p.To.Typ = D_SI
		p = liblink.Appendp(ctxt, p)
		p.As = ACMPL
		p.From.Typ = D_SI
		p.To.Typ = D_CONST
		p.To.Offset = int64(uint32(liblink.StackPreempt & 0xFFFFFFFF))
		p = liblink.Appendp(ctxt, p)
		p.As = AJEQ
		p.To.Typ = D_BRANCH
		q1 = p
		p = liblink.Appendp(ctxt, p)
		p.As = ALEAL
		p.From.Typ = D_INDIR + D_SP
		p.From.Offset = liblink.StackGuard
		p.To.Typ = D_AX
		p = liblink.Appendp(ctxt, p)
		p.As = ASUBL
		p.From.Typ = D_SI
		p.From.Offset = 0
		p.To.Typ = D_AX
		p = liblink.Appendp(ctxt, p)
		p.As = ACMPL
		p.From.Typ = D_AX
		p.To.Typ = D_CONST
		p.To.Offset = framesize + (liblink.StackGuard - liblink.StackSmall)
	}
	// common
	p = liblink.Appendp(ctxt, p)
	p.As = AJHI
	p.To.Typ = D_BRANCH
	p.To.Offset = 4
	q = p
	p = liblink.Appendp(ctxt, p) // save frame size in DI
	p.As = AMOVL
	p.To.Typ = D_DI
	p.From.Typ = D_CONST
	// If we ask for more stack, we'll get a minimum of StackMin bytes.
	// We need a stack frame large enough to hold the top-of-stack data,
	// the function arguments+results, our caller's PC, our frame,
	// a word for the return PC of the next call, and then the StackLimit bytes
	// that must be available on entry to any function called from a function
	// that did a stack check.  If StackMin is enough, don't ask for a specific
	// amount: then we can use the custom functions and save a few
	// instructions.
	if int64(liblink.StackTop+ctxt.Cursym.Text.To.Offset2)+ctxt.Arch.Ptrsize+framesize+ctxt.Arch.Ptrsize+liblink.StackLimit >= liblink.StackMin {
		p.From.Offset = (framesize + 7) &^ 7
	}
	arg = ctxt.Cursym.Text.To.Offset2
	if arg == 1 { // special marker for known 0
		arg = 0
	}
	if arg&3 != 0 {
		ctxt.Diag("misaligned argument size in stack split")
	}
	p = liblink.Appendp(ctxt, p) // save arg size in AX
	p.As = AMOVL
	p.To.Typ = D_AX
	p.From.Typ = D_CONST
	p.From.Offset = int64(arg)
	p = liblink.Appendp(ctxt, p)
	p.As = ACALL
	p.To.Typ = D_BRANCH
	p.To.Sym = ctxt.Symmorestack[noctxt]
	p = liblink.Appendp(ctxt, p)
	p.As = AJMP
	p.To.Typ = D_BRANCH
	p.Pcond = ctxt.Cursym.Text.Link
	if q != nil {
		q.Pcond = p.Link
	}
	if q1 != nil {
		q1.Pcond = q.Link
	}
	*jmpok = q
	return p
}
288184
func GetOrNewStacktrace(err error, skip int, context int, appPackagePrefixes []string) *Stacktrace {
	type stackTracer interface {
		StackTrace() []runtime.Frame
	}
	stacktrace, ok := err.(stackTracer)
	if !ok {
		return NewStacktrace(skip+1, context, appPackagePrefixes)
	}
	var frames []*StacktraceFrame
	for f := range stacktrace.StackTrace() {
		pc := uintptr(f) - 1
		fn := runtime.FuncForPC(pc)
		var fName string
		var file string
		var line int
		if fn != nil {
			file, line = fn.FileLine(pc)
			fName = fn.Name()
		} else {
			file = "unknown"
			fName = "unknown"
		}
		frame := NewStacktraceFrame(pc, fName, file, line, context, appPackagePrefixes)
		if frame != nil {
			frames = append([]*StacktraceFrame{frame}, frames...)
		}
	}
	return &Stacktrace{Frames: frames}
}
1312163
func (s *Stack) Push(value interface{}) {
	s.top = &stackElement{value, s.top}
	s.size++
}
76117
func genStacktrace(err error, skip int) errors.StackTrace {
	var stack errors.StackTrace
	errWithStack := errors.WithStack(err)
	stack = errWithStack.(stackTracer).StackTrace()
	skip++

	// if it is recovering from a panic() call,
	// reset the stack trace at that point
	for index, frame := range stack {
		file := fmt.Sprintf("%s", frame)
		if file == "panic.go" {
			skip = index + 1
			break
		}
	}

	return stack[skip:]
}
1225774
func setStack(stack *stack, data [][]byte) {
	// This can not error. Only errors are for invalid arguments.
	_ = stack.DropN(stack.Depth())

	for i := range data {
		stack.PushByteArray(data[i])
	}
}
601697
func (s *stack) Push(vs ...interface{}) {
	s.values = append(s.values, vs...)
}
564887
func AttachStack(err error) {
	if err == nil {
		return
	}
	cast, ok := err.(*Error)
	if !ok {
		return
	}
	if len(cast.stacks) < 1 {
		// only record stacks if this error was supposed to
		return
	}
	cast.stacks = append(cast.stacks, getStack(2))
}