Source file src/runtime/tracestack.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Trace stack table and acquisition.
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/abi"
    11  	"internal/goarch"
    12  	"internal/trace/tracev2"
    13  	"unsafe"
    14  )
    15  
    16  const (
    17  	// logicalStackSentinel is a sentinel value at pcBuf[0] signifying that
    18  	// pcBuf[1:] holds a logical stack requiring no further processing. Any other
    19  	// value at pcBuf[0] represents a skip value to apply to the physical stack in
    20  	// pcBuf[1:] after inline expansion.
    21  	logicalStackSentinel = ^uintptr(0)
    22  )
    23  
    24  // traceStack captures a stack trace from a goroutine and registers it in the trace
    25  // stack table. It then returns its unique ID. If gp == nil, then traceStack will
    26  // attempt to use the current execution context.
    27  //
    28  // skip controls the number of leaf frames to omit in order to hide tracer internals
    29  // from stack traces, see CL 5523.
    30  //
    31  // Avoid calling this function directly. Prefer traceEventWriter.stack.
    32  func traceStack(skip int, gp *g, tab *traceStackTable) uint64 {
    33  	var pcBuf [tracev2.MaxFramesPerStack]uintptr
    34  
    35  	// Figure out gp and mp for the backtrace.
    36  	var mp *m
    37  	if gp == nil {
    38  		mp = getg().m
    39  		gp = mp.curg
    40  	}
    41  
    42  	// Double-check that we own the stack we're about to trace.
    43  	if debug.traceCheckStackOwnership != 0 && gp != nil {
    44  		status := readgstatus(gp)
    45  		// If the scan bit is set, assume we're the ones that acquired it.
    46  		if status&_Gscan == 0 {
    47  			// Use the trace status to check this. There are a number of cases
    48  			// where a running goroutine might be in _Gwaiting, and these cases
    49  			// are totally fine for taking a stack trace. They're captured
    50  			// correctly in goStatusToTraceGoStatus.
    51  			switch goStatusToTraceGoStatus(status, gp.waitreason) {
    52  			case tracev2.GoRunning, tracev2.GoSyscall:
    53  				if getg() == gp || mp.curg == gp {
    54  					break
    55  				}
    56  				fallthrough
    57  			default:
    58  				print("runtime: gp=", unsafe.Pointer(gp), " gp.goid=", gp.goid, " status=", gStatusStrings[status], "\n")
    59  				throw("attempted to trace stack of a goroutine this thread does not own")
    60  			}
    61  		}
    62  	}
    63  
    64  	if gp != nil && mp == nil {
    65  		// We're getting the backtrace for a G that's not currently executing.
    66  		// It may still have an M, if it's locked to some M.
    67  		mp = gp.lockedm.ptr()
    68  	}
    69  	nstk := 1
    70  	if tracefpunwindoff() || (mp != nil && mp.hasCgoOnStack()) {
    71  		// Slow path: Unwind using default unwinder. Used when frame pointer
    72  		// unwinding is unavailable or disabled (tracefpunwindoff), or might
    73  		// produce incomplete results or crashes (hasCgoOnStack). Note that no
    74  		// cgo callback related crashes have been observed yet. The main
    75  		// motivation is to take advantage of a potentially registered cgo
    76  		// symbolizer.
    77  		pcBuf[0] = logicalStackSentinel
    78  		if getg() == gp {
    79  			nstk += callers(skip+1, pcBuf[1:])
    80  		} else if gp != nil {
    81  			nstk += gcallers(gp, skip, pcBuf[1:])
    82  		}
    83  	} else {
    84  		// Fast path: Unwind using frame pointers.
    85  		pcBuf[0] = uintptr(skip)
    86  		if getg() == gp {
    87  			nstk += fpTracebackPCs(unsafe.Pointer(getfp()), pcBuf[1:])
    88  		} else if gp != nil {
    89  			// Three cases:
    90  			//
    91  			// (1) We're called on the g0 stack through mcall(fn) or systemstack(fn). To
    92  			// behave like gcallers above, we start unwinding from sched.bp, which
    93  			// points to the caller frame of the leaf frame on g's stack. The return
    94  			// address of the leaf frame is stored in sched.pc, which we manually
    95  			// capture here.
    96  			//
    97  			// (2) We're called against a gp that we're not currently executing on, but that isn't
    98  			// in a syscall, in which case it's currently not executing. gp.sched contains the most
    99  			// up-to-date information about where it stopped, and like case (1), we match gcallers
   100  			// here.
   101  			//
   102  			// (3) We're called against a gp that we're not currently executing on, but that is in
   103  			// a syscall, in which case gp.syscallsp != 0. gp.syscall* contains the most up-to-date
   104  			// information about where it stopped, and like case (1), we match gcallers here.
   105  			if gp.syscallsp != 0 {
   106  				pcBuf[1] = gp.syscallpc
   107  				nstk += 1 + fpTracebackPCs(unsafe.Pointer(gp.syscallbp), pcBuf[2:])
   108  			} else {
   109  				pcBuf[1] = gp.sched.pc
   110  				if gp.syncSafePoint {
   111  					// We're stopped in morestack, which is an odd state because gp.sched.bp
   112  					// refers to our parent frame, since we haven't had the chance to push our
   113  					// frame pointer to the stack yet. If we just start walking from gp.sched.bp,
   114  					// we'll skip a frame as a result. Luckily, we can find the PC we want right
   115  					// at gp.sched.sp on non-LR platforms, and we have it directly on LR platforms.
   116  					// See issue go.dev/issue/68090.
   117  					if usesLR {
   118  						pcBuf[2] = gp.sched.lr
   119  					} else {
   120  						pcBuf[2] = *(*uintptr)(unsafe.Pointer(gp.sched.sp))
   121  					}
   122  					nstk += 2 + fpTracebackPCs(unsafe.Pointer(gp.sched.bp), pcBuf[3:])
   123  				} else {
   124  					nstk += 1 + fpTracebackPCs(unsafe.Pointer(gp.sched.bp), pcBuf[2:])
   125  				}
   126  			}
   127  		}
   128  	}
   129  	if nstk > 0 {
   130  		nstk-- // skip runtime.goexit
   131  	}
   132  	if nstk > 0 && gp.goid == 1 {
   133  		nstk-- // skip runtime.main
   134  	}
   135  	id := tab.put(pcBuf[:nstk])
   136  	return id
   137  }
   138  
   139  // traceStackTable maps stack traces (arrays of PC's) to unique uint32 ids.
   140  // It is lock-free for reading.
   141  type traceStackTable struct {
   142  	tab traceMap
   143  }
   144  
   145  // put returns a unique id for the stack trace pcs and caches it in the table,
   146  // if it sees the trace for the first time.
   147  func (t *traceStackTable) put(pcs []uintptr) uint64 {
   148  	if len(pcs) == 0 {
   149  		return 0
   150  	}
   151  	id, _ := t.tab.put(noescape(unsafe.Pointer(&pcs[0])), uintptr(len(pcs))*unsafe.Sizeof(uintptr(0)))
   152  	return id
   153  }
   154  
   155  // dump writes all previously cached stacks to trace buffers,
   156  // releases all memory and resets state. It must only be called once the caller
   157  // can guarantee that there are no more writers to the table.
   158  func (t *traceStackTable) dump(gen uintptr) {
   159  	stackBuf := make([]uintptr, tracev2.MaxFramesPerStack)
   160  	w := unsafeTraceWriter(gen, nil)
   161  	if root := (*traceMapNode)(t.tab.root.Load()); root != nil {
   162  		w = dumpStacksRec(root, w, stackBuf)
   163  	}
   164  	w.flush().end()
   165  	t.tab.reset()
   166  }
   167  
   168  func dumpStacksRec(node *traceMapNode, w traceWriter, stackBuf []uintptr) traceWriter {
   169  	stack := unsafe.Slice((*uintptr)(unsafe.Pointer(&node.data[0])), uintptr(len(node.data))/unsafe.Sizeof(uintptr(0)))
   170  
   171  	// N.B. This might allocate, but that's OK because we're not writing to the M's buffer,
   172  	// but one we're about to create (with ensure).
   173  	n := fpunwindExpand(stackBuf, stack)
   174  	frames := makeTraceFrames(w.gen, stackBuf[:n])
   175  
   176  	// The maximum number of bytes required to hold the encoded stack, given that
   177  	// it contains N frames.
   178  	maxBytes := 1 + (2+4*len(frames))*traceBytesPerNumber
   179  
   180  	// Estimate the size of this record. This
   181  	// bound is pretty loose, but avoids counting
   182  	// lots of varint sizes.
   183  	//
   184  	// Add 1 because we might also write tracev2.EvStacks.
   185  	var flushed bool
   186  	w, flushed = w.ensure(1 + maxBytes)
   187  	if flushed {
   188  		w.byte(byte(tracev2.EvStacks))
   189  	}
   190  
   191  	// Emit stack event.
   192  	w.byte(byte(tracev2.EvStack))
   193  	w.varint(uint64(node.id))
   194  	w.varint(uint64(len(frames)))
   195  	for _, frame := range frames {
   196  		w.varint(uint64(frame.PC))
   197  		w.varint(frame.funcID)
   198  		w.varint(frame.fileID)
   199  		w.varint(frame.line)
   200  	}
   201  
   202  	// Recursively walk all child nodes.
   203  	for i := range node.children {
   204  		child := node.children[i].Load()
   205  		if child == nil {
   206  			continue
   207  		}
   208  		w = dumpStacksRec((*traceMapNode)(child), w, stackBuf)
   209  	}
   210  	return w
   211  }
   212  
   213  // makeTraceFrames returns the frames corresponding to pcs. It may
   214  // allocate and may emit trace events.
   215  func makeTraceFrames(gen uintptr, pcs []uintptr) []traceFrame {
   216  	frames := make([]traceFrame, 0, len(pcs))
   217  	ci := CallersFrames(pcs)
   218  	for {
   219  		f, more := ci.Next()
   220  		frames = append(frames, makeTraceFrame(gen, f))
   221  		if !more {
   222  			return frames
   223  		}
   224  	}
   225  }
   226  
   227  type traceFrame struct {
   228  	PC     uintptr
   229  	funcID uint64
   230  	fileID uint64
   231  	line   uint64
   232  }
   233  
   234  // makeTraceFrame sets up a traceFrame for a frame.
   235  func makeTraceFrame(gen uintptr, f Frame) traceFrame {
   236  	var frame traceFrame
   237  	frame.PC = f.PC
   238  
   239  	fn := f.Function
   240  	const maxLen = 1 << 10
   241  	if len(fn) > maxLen {
   242  		fn = fn[len(fn)-maxLen:]
   243  	}
   244  	frame.funcID = trace.stringTab[gen%2].put(gen, fn)
   245  	frame.line = uint64(f.Line)
   246  	file := f.File
   247  	if len(file) > maxLen {
   248  		file = file[len(file)-maxLen:]
   249  	}
   250  	frame.fileID = trace.stringTab[gen%2].put(gen, file)
   251  	return frame
   252  }
   253  
   254  // tracefpunwindoff returns true if frame pointer unwinding for the tracer is
   255  // disabled via GODEBUG or not supported by the architecture.
   256  func tracefpunwindoff() bool {
   257  	return debug.tracefpunwindoff != 0 || (goarch.ArchFamily != goarch.AMD64 && goarch.ArchFamily != goarch.ARM64)
   258  }
   259  
   260  // fpTracebackPCs populates pcBuf with the return addresses for each frame and
   261  // returns the number of PCs written to pcBuf. The returned PCs correspond to
   262  // "physical frames" rather than "logical frames"; that is if A is inlined into
   263  // B, this will return a PC for only B.
   264  func fpTracebackPCs(fp unsafe.Pointer, pcBuf []uintptr) (i int) {
   265  	for i = 0; i < len(pcBuf) && fp != nil; i++ {
   266  		// return addr sits one word above the frame pointer
   267  		pcBuf[i] = *(*uintptr)(unsafe.Pointer(uintptr(fp) + goarch.PtrSize))
   268  		// follow the frame pointer to the next one
   269  		fp = unsafe.Pointer(*(*uintptr)(fp))
   270  	}
   271  	return i
   272  }
   273  
   274  //go:linkname pprof_fpunwindExpand
   275  func pprof_fpunwindExpand(dst, src []uintptr) int {
   276  	return fpunwindExpand(dst, src)
   277  }
   278  
   279  // fpunwindExpand expands a call stack from pcBuf into dst,
   280  // returning the number of PCs written to dst.
   281  // pcBuf and dst should not overlap.
   282  //
   283  // fpunwindExpand checks if pcBuf contains logical frames (which include inlined
   284  // frames) or physical frames (produced by frame pointer unwinding) using a
   285  // sentinel value in pcBuf[0]. Logical frames are simply returned without the
   286  // sentinel. Physical frames are turned into logical frames via inline unwinding
   287  // and by applying the skip value that's stored in pcBuf[0].
   288  func fpunwindExpand(dst, pcBuf []uintptr) int {
   289  	if len(pcBuf) == 0 {
   290  		return 0
   291  	} else if len(pcBuf) > 0 && pcBuf[0] == logicalStackSentinel {
   292  		// pcBuf contains logical rather than inlined frames, skip has already been
   293  		// applied, just return it without the sentinel value in pcBuf[0].
   294  		return copy(dst, pcBuf[1:])
   295  	}
   296  
   297  	var (
   298  		n          int
   299  		lastFuncID = abi.FuncIDNormal
   300  		skip       = pcBuf[0]
   301  		// skipOrAdd skips or appends retPC to newPCBuf and returns true if more
   302  		// pcs can be added.
   303  		skipOrAdd = func(retPC uintptr) bool {
   304  			if skip > 0 {
   305  				skip--
   306  			} else if n < len(dst) {
   307  				dst[n] = retPC
   308  				n++
   309  			}
   310  			return n < len(dst)
   311  		}
   312  	)
   313  
   314  outer:
   315  	for _, retPC := range pcBuf[1:] {
   316  		callPC := retPC - 1
   317  		fi := findfunc(callPC)
   318  		if !fi.valid() {
   319  			// There is no funcInfo if callPC belongs to a C function. In this case
   320  			// we still keep the pc, but don't attempt to expand inlined frames.
   321  			if more := skipOrAdd(retPC); !more {
   322  				break outer
   323  			}
   324  			continue
   325  		}
   326  
   327  		u, uf := newInlineUnwinder(fi, callPC)
   328  		for ; uf.valid(); uf = u.next(uf) {
   329  			sf := u.srcFunc(uf)
   330  			if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(lastFuncID) {
   331  				// ignore wrappers
   332  			} else if more := skipOrAdd(uf.pc + 1); !more {
   333  				break outer
   334  			}
   335  			lastFuncID = sf.funcID
   336  		}
   337  	}
   338  	return n
   339  }
   340  
   341  // startPCForTrace returns the start PC of a goroutine for tracing purposes.
   342  // If pc is a wrapper, it returns the PC of the wrapped function. Otherwise it
   343  // returns pc.
   344  func startPCForTrace(pc uintptr) uintptr {
   345  	f := findfunc(pc)
   346  	if !f.valid() {
   347  		return pc // may happen for locked g in extra M since its pc is 0.
   348  	}
   349  	w := funcdata(f, abi.FUNCDATA_WrapInfo)
   350  	if w == nil {
   351  		return pc // not a wrapper
   352  	}
   353  	return f.datap.textAddr(*(*uint32)(w))
   354  }
   355  

View as plain text