-
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathcallstack.go
More file actions
440 lines (373 loc) · 9.86 KB
/
Copy pathcallstack.go
File metadata and controls
440 lines (373 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright 2021-present by Nedim Sabic Sabic
* https://www.fibratus.io
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package callstack
import (
"os"
"path/filepath"
"strconv"
"strings"
"github.com/rabbitstack/fibratus/pkg/sys"
"github.com/rabbitstack/fibratus/pkg/util/va"
"golang.org/x/arch/x86/x86asm"
"golang.org/x/sys/windows"
)
// FrameProvenance designates the frame provenance
type FrameProvenance uint8
const (
Kernel FrameProvenance = iota
System
User
)
// SummaryMode defines the callstack summary mode
type SummaryMode uint8
const (
UserSummary SummaryMode = iota
KernelSummary
)
// unbacked represents the identifier for unbacked regions in stack frames
const unbacked = "unbacked"
var pageSize = uint64(os.Getpagesize())
// buildNumber stores the Windows OS build number
var _, _, buildNumber = windows.RtlGetNtVersionNumbers()
// Frame describes a single stack frame.
type Frame struct {
PID uint32 // pid owning thread's stack
Addr va.Address // return address
Offset uint64 // symbol offset
Symbol string // symbol name
Module string // module name
ModuleAddress va.Address // module base address
}
// Provenance resolves the frame provenance.
func (f Frame) Provenance() FrameProvenance {
if f.Addr.InSystemRange() {
return Kernel
}
mod := filepath.Base(strings.ToLower(f.Module))
if mod == "ntdll.dll" || mod == "kernel32.dll" || mod == "kernelbase.dll" {
return System
}
return User
}
// IsUnbacked returns true if this frame is originated
// from unbacked memory section
func (f Frame) IsUnbacked() bool { return f.Module == unbacked }
// AllocationSize calculates the private region size
// to which the frame return address pertains if the
// memory pages within the region are private and
// non-shareable pages.
func (f *Frame) AllocationSize(proc windows.Handle) uint64 {
if f.Addr.InSystemRange() {
return 0
}
r := va.VirtualQuery(proc, f.Addr.Uint64())
if r == nil || (r.State != windows.MEM_COMMIT || r.Protect == windows.PAGE_NOACCESS || r.Type != va.MemImage) {
return 0
}
pageCount := r.Size / pageSize
m := make([]sys.MemoryWorkingSetExInformation, pageCount)
for n := range pageCount {
addr := f.Addr.Inc(n * pageSize)
m[n].VirtualAddress = addr.Uintptr()
}
ws := va.QueryWorkingSet(proc, m)
if ws == nil {
return 0
}
var size uint64
// traverse all pages in the region
for _, r := range ws {
attr := r.VirtualAttributes
if !attr.Valid() {
continue
}
// use SharedOriginal after RS3/1709
if buildNumber >= 16299 {
if !attr.SharedOriginal() {
size += pageSize
}
} else {
if !attr.Shared() {
size += pageSize
}
}
}
return size
}
// Protection resolves the memory protection
// of the pages within the region that contains the
// frame return address.
func (f *Frame) Protection(proc windows.Handle) string {
if f.Addr.InSystemRange() {
return ""
}
r := va.VirtualQuery(proc, f.Addr.Uint64())
if r == nil {
return "?"
}
return r.ProtectMask()
}
// CallsiteAssembly decodes the callsite trailing/leading
// bytes depending on the value of the `leading` argument.
// The resulting string contains the decoded x86 machine
// opcodes in Intel assembler syntax.
func (f *Frame) CallsiteAssembly(proc windows.Handle, leading bool) string {
if f.Addr.InSystemRange() {
return ""
}
size := uint(512)
base := f.Addr.Uintptr()
if leading {
base -= uintptr(size)
}
buf := va.ReadArea(proc, base, size, size, false)
if len(buf) == 0 || va.Zeroed(buf) {
return ""
}
var b strings.Builder
for i := 0; i < len(buf); {
ins, err := x86asm.Decode(buf[i:], 64)
if err != nil {
return b.String()
}
b.WriteString(x86asm.IntelSyntax(ins, f.Addr.Uint64(), nil))
b.WriteRune('|')
i += ins.Len
}
return b.String()
}
// Callstack is a sequence of stack frames
// representing function executions.
type Callstack []Frame
// Init allocates the initial callstack capacity.
func (s *Callstack) Init(n int) {
*s = make(Callstack, 0, n)
}
// PushFrame pushes a new from to the call stack.
func (s *Callstack) PushFrame(f Frame) {
if f.Module == "" {
f.Module = unbacked
}
*s = append(*s, f)
}
// FrameAt returns the stack frame at the specified index.
func (s *Callstack) FrameAt(i int) Frame {
if i > len(*s)-1 {
return Frame{}
}
return (*s)[i]
}
// Depth returns the number of frames in the call stack.
func (s *Callstack) Depth() int { return len(*s) }
// IsEmpty returns true if the callstack has no frames.
func (s *Callstack) IsEmpty() bool { return s.Depth() == 0 }
// FinalUserFrame returns the final frame that corresponds
// to the user code execution. That usually translates to
// the last frame before ntdll or kernel32 modules.
func (s *Callstack) FinalUserFrame() *Frame {
if s.IsEmpty() {
return nil
}
var n int
for n = s.Depth() - 1; n > 0; n-- {
f := (*s)[n]
if f.Addr.InSystemRange() {
continue
}
mod := filepath.Base(strings.ToLower(f.Module))
if mod != "ntdll.dll" && mod != "kernel32.dll" && mod != "kernelbase.dll" {
break
}
}
if n >= 0 && n < s.Depth()-1 {
return &(*s)[n]
}
return nil
}
// FinalUserspaceFrame returns the final userspace frame. This
// frame is typically backed by the ntdll module.
func (s *Callstack) FinalUserspaceFrame() *Frame {
if s.IsEmpty() {
return nil
}
for n := s.Depth() - 1; n > 0; n-- {
f := (*s)[n]
if f.Addr.InSystemRange() {
continue
}
return &f
}
return nil
}
// FinalKernelFrame returns the final kernel space frame.
func (s *Callstack) FinalKernelFrame() *Frame {
if s.IsEmpty() {
return nil
}
return &(*s)[s.Depth()-1]
}
// Summary returns a sequence of non-repeated module names.
func (s Callstack) Summary(mode SummaryMode) string {
var b strings.Builder
b.Grow(len(s) * 16) // preallocate the buffer
var prev string
for i := range s {
frame := s[len(s)-i-1]
switch mode {
case UserSummary:
if frame.Addr.InSystemRange() {
continue
}
case KernelSummary:
if !frame.Addr.InSystemRange() {
return b.String()
}
}
var n string
if frame.IsUnbacked() {
n = unbacked
} else {
n = filepath.Base(frame.Module)
}
if n == prev {
continue
}
if b.Len() > 0 {
b.WriteRune('|')
}
b.WriteString(n)
prev = n
}
return b.String()
}
func (s Callstack) String() string {
var b strings.Builder
for i := range s {
frame := s[len(s)-i-1]
b.WriteString("0x")
b.WriteString(frame.Addr.String())
b.WriteString(" ")
if frame.Addr.InSystemRange() && frame.Module == unbacked {
b.WriteString("?")
} else {
b.WriteString(frame.Module)
}
b.WriteRune('!')
if frame.Symbol != "" && frame.Symbol != "?" {
b.WriteString(frame.Symbol)
} else {
b.WriteRune('?')
}
if frame.Offset != 0 {
b.WriteString("+0x")
b.WriteString(strconv.FormatUint(frame.Offset, 16))
}
if i != len(s)-1 {
b.WriteRune('|')
}
}
return b.String()
}
// ContainsUnbacked returns true if there is a frame
// pertaining to the function call initiated from the
// unbacked memory section. This method only checks
// user space frames for such a condition.
func (s Callstack) ContainsUnbacked() bool {
for _, frame := range s {
if !frame.Addr.InSystemRange() && frame.IsUnbacked() {
return true
}
}
return false
}
// ContainsSymbol checks if the supplied symbol name is present in the callstack.
func (s Callstack) ContainsSymbol(sym string) bool {
for _, frame := range s {
if frame.Symbol == sym {
return true
}
}
return false
}
// Addresses returns stack retrun addresses.
func (s Callstack) Addresses() []string {
addrs := make([]string, len(s))
for i, frame := range s {
addrs[i] = frame.Addr.String()
}
return addrs
}
// Modules returns all modules comprising the thread stack.
func (s Callstack) Modules() []string {
mods := make([]string, len(s))
for i, f := range s {
mods[i] = f.Module
}
return mods
}
// Symbols returns all symbols comprising the call stack.
// Each symbol name is prefixed with the source module.
func (s Callstack) Symbols() []string {
syms := make([]string, len(s))
for i, f := range s {
syms[i] = filepath.Base(f.Module) + "!" + f.Symbol
}
return syms
}
// AllocationSizes returns allocation size of each stack frame
// in terms of allocation/module private non-shareable pages.
func (s Callstack) AllocationSizes(pid uint32) []uint64 {
proc, err := windows.OpenProcess(windows.PROCESS_QUERY_INFORMATION, false, pid)
if err != nil {
return nil
}
defer windows.Close(proc)
sizes := make([]uint64, len(s))
for i, f := range s {
sizes[i] = f.AllocationSize(proc)
}
return sizes
}
// Protections returns page protection mask for every
// frame comprising the stack.
func (s Callstack) Protections(pid uint32) []string {
proc, err := windows.OpenProcess(windows.PROCESS_QUERY_INFORMATION, false, pid)
if err != nil {
return nil
}
defer windows.Close(proc)
prots := make([]string, len(s))
for i, f := range s {
prots[i] = f.Protection(proc)
}
return prots
}
// CallsiteInsns returns callsite assembly opcodes
// for leading/trailing bytes contained in each frame.
func (s Callstack) CallsiteInsns(pid uint32, leading bool) []string {
proc, err := windows.OpenProcess(windows.PROCESS_QUERY_INFORMATION|windows.PROCESS_VM_READ, false, pid)
if err != nil {
return nil
}
defer windows.Close(proc)
opcodes := make([]string, len(s))
for i, f := range s {
opcodes[i] = f.CallsiteAssembly(proc, leading)
}
return opcodes
}