-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstack_panel.go
209 lines (175 loc) · 4.13 KB
/
stack_panel.go
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
package main
import (
"fmt"
"log"
"strings"
"time"
"gioui.org/widget"
. "github.com/emad-elsaid/debugger/ui"
"github.com/go-delve/delve/pkg/proc"
)
type StackPanel struct {
Filter widget.Editor
List ClickableList
RoutineList ClickableList
StackVarsList List
Vars []W
// Cache
GoRoutines []*proc.G
DebuggerState DebuggerState
}
func NewStackPanel() StackPanel {
return StackPanel{
Filter: LineEditor(),
List: NewClickableList(),
RoutineList: NewClickableList(),
StackVarsList: NewVerticalList(),
Vars: []W{},
GoRoutines: []*proc.G{},
}
}
func (sp *StackPanel) Layout(d *Debugger) W {
if d.State != sp.DebuggerState {
sp.DebuggerState = d.State
sp.SetVariables(d)
sp.SetGoRoutines(d)
}
return Columns(
Flexed(1, sp.GoroutinesPanel(d)),
ColSpacer2,
Flexed(4, sp.StackPanel(d)),
ColSpacer2,
Flexed(2, sp.VariablesPanel),
)
}
func (sp *StackPanel) GoroutinesPanel(d *Debugger) W {
count := len(sp.GoRoutines)
elem := func(c C, i int) D {
if i >= count {
return D{}
}
r := sp.GoRoutines[i]
g, _ := d.SelectedGoRoutine()
label := ""
if r.CurrentLoc.Fn != nil {
label = r.CurrentLoc.Fn.Name
} else {
label = fmt.Sprintf("%s:%d", r.CurrentLoc.File, r.CurrentLoc.Line)
}
w := Inset1(Label(fmt.Sprintf("%d: %s", r.ID, label)))
if g != nil && g.ID == r.ID {
w = Background(HighlightColor, w)
}
return w(c)
}
onClick := func(i int) {
if i >= count {
return
}
r := sp.GoRoutines[i]
log.Printf("Switching Go routine to: %d: %s", r.ID, r.Go().File)
d.Target().SwitchGoroutine(r)
}
return Panel(
fmt.Sprintf("Go Routines: (%d)", count),
sp.RoutineList.Layout(count, elem, onClick),
)
}
func (sp *StackPanel) StackPanel(d *Debugger) W {
stack, err := d.Stacktrace()
if err != nil {
return Centered(Label(err.Error()))
}
filter := sp.Filter.Text()
stk := make([]proc.Stackframe, 0, len(stack))
for _, i := range stack {
l := ""
if i.Current.Fn != nil {
l = i.Current.Fn.Name
} else {
l = fmt.Sprintf("%s:%d", i.Current.File, i.Current.Line)
}
if strings.Contains(l, filter) {
stk = append(stk, i)
}
}
ele := func(c C, i int) D {
s := stk[i]
l := ""
if s.Current.Fn != nil {
l = s.Current.Fn.Name
} else {
l = fmt.Sprintf("%s:%d", s.Current.File, s.Current.Line)
}
w := Inset1(Label(l))
if d.File == s.Current.File && d.Line == s.Current.Line {
w = Background(HighlightColor, w)
}
return w(c)
}
click := func(i int) {
d.StackFrame = i
s := stk[i]
d.SetFileLine(s.Current.File, s.Current.Line)
}
return Rows(
RowSpacer1,
Rigid(
FormRow(
Rigid(Label(fmt.Sprintf("%d", len(stk)))),
ColSpacer1,
Flexed(1, TextInput(&sp.Filter, "Search Stack...")),
),
),
RowSpacer1,
Flexed(1, sp.List.Layout(len(stk), ele, click)),
)
}
func (sp *StackPanel) SetVariables(d *Debugger) {
sp.Vars = []W{}
args, err := d.FunctionArguments()
if err != nil {
return
}
sp.varsToVarWidget("Arguments", args)
vars, err := d.LocalVariables()
if err != nil {
return
}
sp.varsToVarWidget("Local Variables", vars)
g, err := d.SelectedGoRoutine()
if err != nil {
return
}
sp.GoRoutineToWidgets(g)
}
func (sp *StackPanel) SetGoRoutines(d *Debugger) {
routines, err := d.GoRoutines()
if err == nil {
sp.GoRoutines = routines
}
}
func (sp *StackPanel) varsToVarWidget(title string, args []*proc.Variable) {
sp.Vars = append(sp.Vars, Bold(Text(title)))
for _, v := range args {
w := NewVarWidget(v)
sp.Vars = append(sp.Vars, w.Layout)
}
}
func (sp *StackPanel) GoRoutineToWidgets(g *proc.G) {
sp.Vars = append(sp.Vars,
Bold(Text("Go Routine properties")),
Text(fmt.Sprintf("Status: %s", GoRoutineStatus(g.Status))),
Text(fmt.Sprintf("SystemStack: %v", g.SystemStack)),
Text(fmt.Sprintf("Wait Since: %s", time.Duration(g.WaitSince))),
Text(fmt.Sprintf("Wait Reason: %s", waitReason(g.WaitReason))),
Text(fmt.Sprintf("Go statement: %s:%d", g.Go().File, g.Go().Line)),
)
}
func (sp *StackPanel) VariablesPanel(c C) D {
ele := func(c C, i int) D {
item := sp.Vars[i]
return item(c)
}
return sp.StackVarsList.Layout(c, len(sp.Vars), ele)
}