github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/stl/debug.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package stl 16 17 import ( 18 "bytes" 19 "runtime" 20 "strconv" 21 "sync" 22 ) 23 24 var ( 25 _callersPool = sync.Pool{ 26 New: func() interface{} { 27 return newCallers(32) 28 }, 29 } 30 ) 31 32 func GetCalllers(skip int) *Callers { 33 c := _callersPool.Get() 34 cc := c.(*Callers) 35 var n int 36 for { 37 n = runtime.Callers(skip+2, cc.storage) 38 if n < len(cc.storage) { 39 break 40 } 41 size := len(cc.storage) 42 cc.Close() 43 cc = newCallers(size * 2) 44 } 45 cc.num = n 46 return cc 47 } 48 49 type Callers struct { 50 storage []uintptr 51 num int 52 } 53 54 func newCallers(size int) *Callers { 55 return &Callers{ 56 storage: make([]uintptr, size), 57 } 58 } 59 60 func (c *Callers) Close() { 61 c.num = 0 62 _callersPool.Put(c) 63 } 64 65 func (c *Callers) String() string { 66 var buffer bytes.Buffer 67 i := 0 68 frames := runtime.CallersFrames(c.storage[:c.num]) 69 for frame, more := frames.Next(); more; frame, more = frames.Next() { 70 if i != 0 { 71 buffer.WriteByte('\n') 72 } 73 i++ 74 // buffer.WriteByte('[') 75 // buffer.WriteString(frame.Function) 76 // buffer.WriteByte(']') 77 // buffer.WriteByte('|') 78 buffer.WriteString(frame.File) 79 buffer.WriteByte(':') 80 buffer.WriteString(strconv.Itoa(frame.Line)) 81 } 82 return buffer.String() 83 }