github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/smalltrace.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package util 12 13 import ( 14 "runtime" 15 "strings" 16 17 "github.com/cockroachdb/redact" 18 ) 19 20 var prefix = func() string { 21 result := "github.com/cockroachdb/cockroachdb-parser/pkg/" 22 if runtime.Compiler == "gccgo" { 23 result = strings.Replace(result, ".", "_", -1) 24 result = strings.Replace(result, "/", "_", -1) 25 } 26 return result 27 }() 28 29 // GetSmallTrace returns a comma-separated string containing the top 30 // 5 callers from a given skip level. 31 func GetSmallTrace(skip int) redact.RedactableString { 32 var pcs [5]uintptr 33 runtime.Callers(skip, pcs[:]) 34 frames := runtime.CallersFrames(pcs[:]) 35 var callers redact.StringBuilder 36 37 var callerPrefix redact.RedactableString 38 for { 39 f, more := frames.Next() 40 function := strings.TrimPrefix(f.Function, prefix) 41 file := f.File 42 if index := strings.LastIndexByte(file, '/'); index >= 0 { 43 file = file[index+1:] 44 } 45 callers.Printf("%s%s:%d:%s", callerPrefix, redact.SafeString(file), f.Line, redact.SafeString(function)) 46 callerPrefix = "," 47 if !more { 48 break 49 } 50 } 51 52 return callers.RedactableString() 53 }