| 1 | // Copyright 2020 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 | // Package stack provides support for parsing standard goroutine stack traces. |
| 6 | package stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/ |
| 7 | |
| 8 | import ( |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "text/tabwriterscip-go gomod github.com/golang/go/src go1.25.0 `text/tabwriter`/" |
| 11 | ) |
| 12 | |
| 13 | // Dump is a raw set of goroutines and their stacks. |
| 14 | type Dumpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Dump# []Goroutinescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Goroutine# |
| 15 | |
| 16 | // Goroutine is a single parsed goroutine dump. |
| 17 | type Goroutinescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Goroutine# struct { |
| 18 | Statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Goroutine#State. string // state that the goroutine is in. |
| 19 | IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Goroutine#ID. int // id of the goroutine. |
| 20 | Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Goroutine#Stack. Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack# // call frames that make up the stack |
| 21 | } |
| 22 | |
| 23 | // Stack is a set of frames in a callstack. |
| 24 | type Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack# []Framescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame# |
| 25 | |
| 26 | // Frame is a point in a call stack. |
| 27 | type Framescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame# struct { |
| 28 | Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Function. Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function# |
| 29 | Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Position. Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position# |
| 30 | } |
| 31 | |
| 32 | // Function is the function called at a frame. |
| 33 | type Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function# struct { |
| 34 | Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function#Package. string // package name of function if known |
| 35 | Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function#Type. string // if set function is a method of this type |
| 36 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function#Name. string // function name of the frame |
| 37 | } |
| 38 | |
| 39 | // Position is the file position for a frame. |
| 40 | type Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position# struct { |
| 41 | Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Filename. string // source filename |
| 42 | Linescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Line. int // line number within file |
| 43 | } |
| 44 | |
| 45 | // Summary is a set of stacks processed and collated into Calls. |
| 46 | type Summaryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary# struct { |
| 47 | Totalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary#Total. int // the total count of goroutines in the summary |
| 48 | Callsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary#Calls. []Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Call# // the collated stack traces |
| 49 | } |
| 50 | |
| 51 | // Call is set of goroutines that all share the same callstack. |
| 52 | // They will be grouped by state. |
| 53 | type Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Call# struct { |
| 54 | Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Call#Stack. Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack# // the shared callstack information |
| 55 | Groupsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Call#Groups. []Groupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Group# // the sets of goroutines with the same state |
| 56 | } |
| 57 | |
| 58 | // Group is a set of goroutines with the same stack that are in the same state. |
| 59 | type Groupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Group# struct { |
| 60 | Statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Group#State. string // the shared state of the goroutines |
| 61 | Goroutinesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Group#Goroutines. []Goroutinescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Goroutine# // the set of goroutines in this group |
| 62 | } |
| 63 | |
| 64 | // Delta represents the difference between two stack dumps. |
| 65 | type Deltascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Delta# struct { |
| 66 | Beforescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Delta#Before. Dumpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Dump# // The goroutines that were only in the before set. |
| 67 | Sharedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Delta#Shared. Dumpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Dump# // The goroutines that were in both sets. |
| 68 | Afterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Delta#After. Dumpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Dump# // The goroutines that were only in the after set. |
| 69 | } |
| 70 | |
| 71 | func (s Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack#) equalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack#equal().(other Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack#) bool { |
| 72 | if len(s) != len(other) { |
| 73 | return false |
| 74 | } |
| 75 | for i, frame := range s { |
| 76 | if !frame.equalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#equal().(other[i]) { |
| 77 | return false |
| 78 | } |
| 79 | } |
| 80 | return true |
| 81 | } |
| 82 | |
| 83 | func (s Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack#) lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack#less().(other Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Stack#) bool { |
| 84 | for i, frame := range s { |
| 85 | if i >= len(other) { |
| 86 | return false |
| 87 | } |
| 88 | if frame.lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#less().(other[i]) { |
| 89 | return true |
| 90 | } |
| 91 | if !frame.equalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#equal().(other[i]) { |
| 92 | return false |
| 93 | } |
| 94 | } |
| 95 | return len(s) < len(other) |
| 96 | } |
| 97 | |
| 98 | func (f Framescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#) equalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#equal().(other Framescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#) bool { |
| 99 | return f.Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Position..equalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#equal().(other.Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Position.) |
| 100 | } |
| 101 | |
| 102 | func (f Framescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#) lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#less().(other Framescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#) bool { |
| 103 | return f.Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Position..lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#less().(other.Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Position.) |
| 104 | } |
| 105 | |
| 106 | func (p Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#) equalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#equal().(other Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#) bool { |
| 107 | return p.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Filename. == other.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Filename. && p.Linescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Line. == other.Linescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Line. |
| 108 | } |
| 109 | |
| 110 | func (p Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#) lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#less().(other Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#) bool { |
| 111 | if p.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Filename. < other.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Filename. { |
| 112 | return true |
| 113 | } |
| 114 | if p.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Filename. > other.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Filename. { |
| 115 | return false |
| 116 | } |
| 117 | return p.Linescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Line. < other.Linescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Line. |
| 118 | } |
| 119 | |
| 120 | func (s Summaryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary#) Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary#Format().(w fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Statescip-go gomod github.com/golang/go/src go1.25.0 fmt/State#, r rune) { |
| 121 | tw := tabwriterscip-go gomod github.com/golang/go/src go1.25.0 `text/tabwriter`/.NewWriterscip-go gomod github.com/golang/go/src go1.25.0 `text/tabwriter`/NewWriter().(w, 0, 0, 1, ' ', 0) |
| 122 | for i, c := range s.Callsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary#Calls. { |
| 123 | if i > 0 { |
| 124 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(tw, "\n\n") |
| 125 | tw.Flushscip-go gomod github.com/golang/go/src go1.25.0 `text/tabwriter`/Writer#Flush().() |
| 126 | } |
| 127 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(tw, c) |
| 128 | } |
| 129 | tw.Flushscip-go gomod github.com/golang/go/src go1.25.0 `text/tabwriter`/Writer#Flush().() |
| 130 | if s.Totalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary#Total. > 0 && w.Flagscip-go gomod github.com/golang/go/src go1.25.0 fmt/State#Flag().('+') { |
| 131 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "\n\n%d goroutines, %d unique", s.Totalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary#Total., len(s.Callsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summary#Calls.)) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func (c Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Call#) Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Call#Format().(w fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Statescip-go gomod github.com/golang/go/src go1.25.0 fmt/State#, r rune) { |
| 136 | for i, g := range c.Groupsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Call#Groups. { |
| 137 | if i > 0 { |
| 138 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(w, " ") |
| 139 | } |
| 140 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(w, g) |
| 141 | } |
| 142 | for _, f := range c.Stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Call#Stack. { |
| 143 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "\n%v", f) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func (g Groupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Group#) Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Group#Format().(w fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Statescip-go gomod github.com/golang/go/src go1.25.0 fmt/State#, r rune) { |
| 148 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "[%v]: ", g.Statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Group#State.) |
| 149 | for i, gr := range g.Goroutinesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Group#Goroutines. { |
| 150 | if i > 0 { |
| 151 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(w, ", ") |
| 152 | } |
| 153 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "$%d", gr.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Goroutine#ID.) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func (f Framescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#) Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Format().(w fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Statescip-go gomod github.com/golang/go/src go1.25.0 fmt/State#, c rune) { |
| 158 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "%v:\t%v", f.Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Position., f.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Frame#Function.) |
| 159 | } |
| 160 | |
| 161 | func (f Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function#) Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function#Format().(w fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Statescip-go gomod github.com/golang/go/src go1.25.0 fmt/State#, c rune) { |
| 162 | if f.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function#Type. != "" { |
| 163 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "(%v).", f.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function#Type.) |
| 164 | } |
| 165 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "%v", f.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Function#Name.) |
| 166 | } |
| 167 | |
| 168 | func (p Positionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#) Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Format().(w fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Statescip-go gomod github.com/golang/go/src go1.25.0 fmt/State#, c rune) { |
| 169 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "%v:%v", p.Filenamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Filename., p.Linescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Position#Line.) |
| 170 | } |