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  	pcBuf := getg().m.profStack
    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 IDs.
   140  //
   141  // ID 0 is reserved for a zero-length stack.
   142  type traceStackTable struct {
   143  	tab traceMap
   144  }
   145  
   146  // put returns a unique id for the stack trace pcs and caches it in the table,
   147  // if it sees the trace for the first time.
   148  func (t *traceStackTable) put(pcs []uintptr) uint64 {
   149  	// Even though put will handle this for us, taking the address of pcs forces a bounds check
   150  	// that will fail if len(pcs) == 0.
   151  	if len(pcs) == 0 {
   152  		return 0 // ID 0 is reserved for zero-length stacks.
   153  	}
   154  	id, _ := t.tab.put(noescape(unsafe.Pointer(&pcs[0])), uintptr(len(pcs))*unsafe.Sizeof(uintptr(0)))
   155  	return id
   156  }
   157  
   158  // dump writes all previously cached stacks to trace buffers,
   159  // releases all memory and resets state. It must only be called once the caller
   160  // can guarantee that there are no more writers to the table.
   161  func (t *traceStackTable) dump(gen uintptr) {
   162  	stackBuf := make([]uintptr, tracev2.MaxFramesPerStack)
   163  	w := unsafeTraceWriter(gen, nil)
   164  	if root := (*traceMapNode)(t.tab.root.Load()); root != nil {
   165  		w = dumpStacksRec(root, w, stackBuf)
   166  	}
   167  	w.flush().end()
   168  	t.tab.reset()
   169  }
   170  
   171  func dumpStacksRec(node *traceMapNode, w traceWriter, stackBuf []uintptr) traceWriter {
   172  	stack := unsafe.Slice((*uintptr)(unsafe.Pointer(&node.data[0])), uintptr(len(node.data))/unsafe.Sizeof(uintptr(0)))
   173  
   174  	// N.B. This might allocate, but that's OK because we're not writing to the M's buffer,
   175  	// but one we're about to create (with ensure).
   176  	n := fpunwindExpand(stackBuf, stack)
   177  	frames := makeTraceFrames(w.gen, stackBuf[:n])
   178  
   179  	// The maximum number of bytes required to hold the encoded stack, given that
   180  	// it contains N frames.
   181  	maxBytes := 1 + (2+4*len(frames))*traceBytesPerNumber
   182  
   183  	// Estimate the size of this record. This
   184  	// bound is pretty loose, but avoids counting
   185  	// lots of varint sizes.
   186  	//
   187  	// Add 1 because we might also write tracev2.EvStacks.
   188  	var flushed bool
   189  	w, flushed = w.ensure(1 + maxBytes)
   190  	if flushed {
   191  		w.byte(byte(tracev2.EvStacks))
   192  	}
   193  
   194  	// Emit stack event.
   195  	w.byte(byte(tracev2.EvStack))
   196  	w.varint(node.id)
   197  	w.varint(uint64(len(frames)))
   198  	for _, frame := range frames {
   199  		w.varint(uint64(frame.PC))
   200  		w.varint(frame.funcID)
   201  		w.varint(frame.fileID)
   202  		w.varint(frame.line)
   203  	}
   204  
   205  	// Recursively walk all child nodes.
   206  	for i := range node.children {
   207  		child := node.children[i].Load()
   208  		if child == nil {
   209  			continue
   210  		}
   211  		w = dumpStacksRec((*traceMapNode)(child), w, stackBuf)
   212  	}
   213  	return w
   214  }
   215  
   216  // makeTraceFrames returns the frames corresponding to pcs. It may
   217  // allocate and may emit trace events.
   218  func makeTraceFrames(gen uintptr, pcs []uintptr) []traceFrame {
   219  	frames := make([]traceFrame, 0, len(pcs))
   220  	ci := CallersFrames(pcs)
   221  	for {
   222  		f, more := ci.Next()
   223  		frames = append(frames, makeTraceFrame(gen, f))
   224  		if !more {
   225  			return frames
   226  		}
   227  	}
   228  }
   229  
   230  type traceFrame struct {
   231  	PC     uintptr
   232  	funcID uint64
   233  	fileID uint64
   234  	line   uint64
   235  }
   236  
   237  // makeTraceFrame sets up a traceFrame for a frame.
   238  func makeTraceFrame(gen uintptr, f Frame) traceFrame {
   239  	var frame traceFrame
   240  	frame.PC = f.PC
   241  
   242  	fn := f.Function
   243  	const maxLen = 1 << 10
   244  	if len(fn) > maxLen {
   245  		fn = fn[len(fn)-maxLen:]
   246  	}
   247  	frame.funcID = trace.stringTab[gen%2].put(gen, fn)
   248  	frame.line = uint64(f.Line)
   249  	file := f.File
   250  	if len(file) > maxLen {
   251  		file = file[len(file)-maxLen:]
   252  	}
   253  	frame.fileID = trace.stringTab[gen%2].put(gen, file)
   254  	return frame
   255  }
   256  
   257  // tracefpunwindoff returns true if frame pointer unwinding for the tracer is
   258  // disabled via GODEBUG or not supported by the architecture.
   259  func tracefpunwindoff() bool {
   260  	return debug.tracefpunwindoff != 0 || (goarch.ArchFamily != goarch.AMD64 && goarch.ArchFamily != goarch.ARM64)
   261  }
   262  
   263  // fpTracebackPCs populates pcBuf with the return addresses for each frame and
   264  // returns the number of PCs written to pcBuf. The returned PCs correspond to
   265  // "physical frames" rather than "logical frames"; that is if A is inlined into
   266  // B, this will return a PC for only B.
   267  func fpTracebackPCs(fp unsafe.Pointer, pcBuf []uintptr) (i int) {
   268  	for i = 0; i < len(pcBuf) && fp != nil; i++ {
   269  		// return addr sits one word above the frame pointer
   270  		pcBuf[i] = *(*uintptr)(unsafe.Pointer(uintptr(fp) + goarch.PtrSize))
   271  		// follow the frame pointer to the next one
   272  		fp = unsafe.Pointer(*(*uintptr)(fp))
   273  	}
   274  	return i
   275  }
   276  
   277  //go:linkname pprof_fpunwindExpand
   278  func pprof_fpunwindExpand(dst, src []uintptr) int {
   279  	return fpunwindExpand(dst, src)
   280  }
   281  
   282  // fpunwindExpand expands a call stack from pcBuf into dst,
   283  // returning the number of PCs written to dst.
   284  // pcBuf and dst should not overlap.
   285  //
   286  // fpunwindExpand checks if pcBuf contains logical frames (which include inlined
   287  // frames) or physical frames (produced by frame pointer unwinding) using a
   288  // sentinel value in pcBuf[0]. Logical frames are simply returned without the
   289  // sentinel. Physical frames are turned into logical frames via inline unwinding
   290  // and by applying the skip value that's stored in pcBuf[0].
   291  func fpunwindExpand(dst, pcBuf []uintptr) int {
   292  	if len(pcBuf) == 0 {
   293  		return 0
   294  	} else if len(pcBuf) > 0 && pcBuf[0] == logicalStackSentinel {
   295  		// pcBuf contains logical rather than inlined frames, skip has already been
   296  		// applied, just return it without the sentinel value in pcBuf[0].
   297  		return copy(dst, pcBuf[1:])
   298  	}
   299  
   300  	var (
   301  		n          int
   302  		lastFuncID = abi.FuncIDNormal
   303  		skip       = pcBuf[0]
   304  		// skipOrAdd skips or appends retPC to newPCBuf and returns true if more
   305  		// pcs can be added.
   306  		skipOrAdd = func(retPC uintptr) bool {
   307  			if skip > 0 {
   308  				skip--
   309  			} else if n < len(dst) {
   310  				dst[n] = retPC
   311  				n++
   312  			}
   313  			return n < len(dst)
   314  		}
   315  	)
   316  
   317  outer:
   318  	for _, retPC := range pcBuf[1:] {
   319  		callPC := retPC - 1
   320  		fi := findfunc(callPC)
   321  		if !fi.valid() {
   322  			// There is no funcInfo if callPC belongs to a C function. In this case
   323  			// we still keep the pc, but don't attempt to expand inlined frames.
   324  			if more := skipOrAdd(retPC); !more {
   325  				break outer
   326  			}
   327  			continue
   328  		}
   329  
   330  		u, uf := newInlineUnwinder(fi, callPC)
   331  		for ; uf.valid(); uf = u.next(uf) {
   332  			sf := u.srcFunc(uf)
   333  			if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(lastFuncID) {
   334  				// ignore wrappers
   335  			} else if more := skipOrAdd(uf.pc + 1); !more {
   336  				break outer
   337  			}
   338  			lastFuncID = sf.funcID
   339  		}
   340  	}
   341  	return n
   342  }
   343  
   344  // startPCForTrace returns the start PC of a goroutine for tracing purposes.
   345  // If pc is a wrapper, it returns the PC of the wrapped function. Otherwise it
   346  // returns pc.
   347  func startPCForTrace(pc uintptr) uintptr {
   348  	f := findfunc(pc)
   349  	if !f.valid() {
   350  		return pc // may happen for locked g in extra M since its pc is 0.
   351  	}
   352  	w := funcdata(f, abi.FUNCDATA_WrapInfo)
   353  	if w == nil {
   354  		return pc // not a wrapper
   355  	}
   356  	return f.datap.textAddr(*(*uint32)(w))
   357  }
   358  

View as plain text