File: stackdump.go

package info (click to toggle)
golang-glog 1.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: makefile: 2
file content (127 lines) | stat: -rw-r--r-- 3,773 bytes parent folder | download
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
// Copyright 2023 Google Inc. 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 stackdump provides wrappers for runtime.Stack and runtime.Callers
// with uniform support for skipping caller frames.
//
// ⚠ Unlike the functions in the runtime package, these may allocate a
// non-trivial quantity of memory: use them with care. ⚠
package stackdump

import (
	"bytes"
	"runtime"
)

// runtimeStackSelfFrames is 1 if runtime.Stack includes the call to
// runtime.Stack itself or 0 if it does not.
//
// As of 2016-04-27, the gccgo compiler includes runtime.Stack but the gc
// compiler does not.
var runtimeStackSelfFrames = func() int {
	for n := 1 << 10; n < 1<<20; n *= 2 {
		buf := make([]byte, n)
		n := runtime.Stack(buf, false)
		if bytes.Contains(buf[:n], []byte("runtime.Stack")) {
			return 1
		} else if n < len(buf) || bytes.Count(buf, []byte("\n")) >= 3 {
			return 0
		}
	}
	return 0
}()

// Stack is a stack dump for a single goroutine.
type Stack struct {
	// Text is a representation of the stack dump in a human-readable format.
	Text []byte

	// PC is a representation of the stack dump using raw program counter values.
	PC []uintptr
}

func (s Stack) String() string { return string(s.Text) }

// Caller returns the Stack dump for the calling goroutine, starting skipDepth
// frames before the caller of Caller.  (Caller(0) provides a dump starting at
// the caller of this function.)
func Caller(skipDepth int) Stack {
	return Stack{
		Text: CallerText(skipDepth + 1),
		PC:   CallerPC(skipDepth + 1),
	}
}

// CallerText returns a textual dump of the stack starting skipDepth frames before
// the caller.  (CallerText(0) provides a dump starting at the caller of this
// function.)
func CallerText(skipDepth int) []byte {
	for n := 1 << 10; ; n *= 2 {
		buf := make([]byte, n)
		n := runtime.Stack(buf, false)
		if n < len(buf) {
			return pruneFrames(skipDepth+1+runtimeStackSelfFrames, buf[:n])
		}
	}
}

// CallerPC returns a dump of the program counters of the stack starting
// skipDepth frames before the caller.  (CallerPC(0) provides a dump starting at
// the caller of this function.)
func CallerPC(skipDepth int) []uintptr {
	for n := 1 << 8; ; n *= 2 {
		buf := make([]uintptr, n)
		n := runtime.Callers(skipDepth+2, buf)
		if n < len(buf) {
			return buf[:n]
		}
	}
}

// pruneFrames removes the topmost skipDepth frames of the first goroutine in a
// textual stack dump.  It overwrites the passed-in slice.
//
// If there are fewer than skipDepth frames in the first goroutine's stack,
// pruneFrames prunes it to an empty stack and leaves the remaining contents
// intact.
func pruneFrames(skipDepth int, stack []byte) []byte {
	headerLen := 0
	for i, c := range stack {
		if c == '\n' {
			headerLen = i + 1
			break
		}
	}
	if headerLen == 0 {
		return stack // No header line - not a well-formed stack trace.
	}

	skipLen := headerLen
	skipNewlines := skipDepth * 2
	for ; skipLen < len(stack) && skipNewlines > 0; skipLen++ {
		c := stack[skipLen]
		if c != '\n' {
			continue
		}
		skipNewlines--
		skipLen++
		if skipNewlines == 0 || skipLen == len(stack) || stack[skipLen] == '\n' {
			break
		}
	}

	pruned := stack[skipLen-headerLen:]
	copy(pruned, stack[:headerLen])
	return pruned
}