Debian Codegraph @

event/source.go

raw symbols SCIP
1 // Copyright 2019 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 //go:build !disable_events
6
7 package event
8
9 import (
10 "reflect"
11 "runtime"
12 "sort"
13 "strings"
14 )
15
16 const (
17 // this is the maximum amount of helpers we scan past to find a non helper
18 helperDepthLimit = 5
19 )
20
21 type sources struct {
22 entries []caller
23 }
24
25 type Source struct {
26 Space string
27 Owner string
28 Name string
29 }
30
31 type caller struct {
32 helper bool
33 pc uintptr
34 source Source
35 }
36
37 var globalCallers chan *sources
38
39 // RegisterHelper records a function as being an event helper that should not
40 // be used when capturing the source information on events.
41 // v should be either a string or a function pointer.
42 // If v is a string it is of the form
43 //
44 // Space.Owner.Name
45 //
46 // where Owner and Name cannot contain '/' and Name also cannot contain '.'
47 func RegisterHelper(v interface{}) {
48 g := <-globalCallers
49 defer func() { globalCallers <- g }()
50 switch v := v.(type) {
51 case string:
52 g.entries = append(g.entries, caller{source: splitName(v), helper: true})
53 default:
54 g.helperFunction(v)
55 }
56 }
57
58 func init() {
59 g := &sources{}
60 // make all entries in the event package helpers
61 globalCallers = make(chan *sources, 1)
62 globalCallers <- g
63 RegisterHelper("golang.org/x/exp/event")
64 }
65
66 func newCallers() sources {
67 g := <-globalCallers
68 defer func() { globalCallers <- g }()
69 c := sources{}
70 c.entries = make([]caller, len(g.entries))
71 copy(c.entries, g.entries)
72 return c
73 }
74
75 func (c *sources) addCaller(entry caller) {
76 i := sort.Search(len(c.entries), func(i int) bool {
77 return c.entries[i].pc >= entry.pc
78 })
79 if i >= len(c.entries) {
80 // add to end
81 c.entries = append(c.entries, entry)
82 return
83 }
84 if c.entries[i].pc == entry.pc {
85 // already present
86 return
87 }
88 //expand the array
89 c.entries = append(c.entries, caller{})
90 //make a space
91 copy(c.entries[i+1:], c.entries[i:])
92 // insert the entry
93 c.entries[i] = entry
94 }
95
96 func (c *sources) getCaller(pc uintptr) (caller, bool) {
97 i := sort.Search(len(c.entries), func(i int) bool {
98 return c.entries[i].pc >= pc
99 })
100 if i == len(c.entries) || c.entries[i].pc != pc {
101 return caller{}, false
102 }
103 return c.entries[i], true
104 }
105
106 func scanStack() Source {
107 g := <-globalCallers
108 defer func() { globalCallers <- g }()
109 return g.scanStack()
110 }
111
112 func (c *sources) scanStack() Source {
113 // first capture the caller stack
114 var stack [helperDepthLimit]uintptr
115 // we can skip the first three entries
116 // runtime.Callers
117 // event.(*sources).scanStack (this function)
118 // another function in this package (because scanStack is private)
119 depth := runtime.Callers(3, stack[:]) // start at 2 to skip Callers and this function
120 // do a cheap first pass to see if we have an entry for this stack
121 for i := 0; i < depth; i++ {
122 pc := stack[i]
123 e, found := c.getCaller(pc)
124 if found {
125 if !e.helper {
126 // exact non helper match found, return it
127 return e.source
128 }
129 // helper found, keep scanning
130 continue
131 }
132 // stack entry not found, we need to fill one in
133 f := runtime.FuncForPC(stack[i])
134 if f == nil {
135 // symtab lookup failed, pretend it does not exist
136 continue
137 }
138 e = caller{
139 source: splitName(f.Name()),
140 pc: pc,
141 }
142 e.helper = c.isHelper(e)
143 c.addCaller(e)
144 if !e.helper {
145 // found a non helper entry, add it and return it
146 return e.source
147 }
148 }
149 // ran out of stack, was all helpers
150 return Source{}
151 }
152
153 // we do helper matching by name, if the pc matched we would have already found
154 // that, but helper registration does not know the call stack pcs
155 func (c *sources) isHelper(entry caller) bool {
156 // scan to see if it matches any of the helpers
157 // we match by name in case of inlining
158 for _, e := range c.entries {
159 if !e.helper {
160 // ignore all the non helper entries
161 continue
162 }
163 if isMatch(entry.source.Space, e.source.Space) &&
164 isMatch(entry.source.Owner, e.source.Owner) &&
165 isMatch(entry.source.Name, e.source.Name) {
166 return true
167 }
168 }
169 return false
170 }
171
172 func isMatch(value, against string) bool {
173 return len(against) == 0 || value == against
174 }
175
176 func (c *sources) helperFunction(v interface{}) {
177 r := reflect.ValueOf(v)
178 pc := r.Pointer()
179 f := runtime.FuncForPC(pc)
180 entry := caller{
181 source: splitName(f.Name()),
182 pc: f.Entry(),
183 helper: true,
184 }
185 c.addCaller(entry)
186 if entry.pc != pc {
187 entry.pc = pc
188 c.addCaller(entry)
189 }
190 }
191
192 func splitName(full string) Source {
193 // Function is the fully-qualified function name. The name itself may
194 // have dots (for a closure, for instance), but it can't have slashes.
195 // So the package path ends at the first dot after the last slash.
196 entry := Source{Space: full}
197 slash := strings.LastIndexByte(full, '/')
198 if slash < 0 {
199 slash = 0
200 }
201 if dot := strings.IndexByte(full[slash:], '.'); dot >= 0 {
202 entry.Space = full[:slash+dot]
203 entry.Name = full[slash+dot+1:]
204 if dot = strings.LastIndexByte(entry.Name, '.'); dot >= 0 {
205 entry.Owner = entry.Name[:dot]
206 entry.Name = entry.Name[dot+1:]
207 }
208 }
209 return entry
210 }