Skip to content

Commit 38f55c2

Browse files
committed
pp: print out legend in pp/panicparse help
The next step will be to improve the color scheme.
1 parent 5ff21cc commit 38f55c2

4 files changed

Lines changed: 118 additions & 62 deletions

File tree

internal/main.go

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,24 @@ const resetFG = ansi.DefaultFG + "\033[m"
4444

4545
// defaultPalette is the default recommended palette.
4646
var defaultPalette = Palette{
47-
EOLReset: resetFG,
48-
RoutineFirst: ansi.ColorCode("magenta+b"),
49-
CreatedBy: ansi.LightBlack,
50-
Race: ansi.LightRed,
51-
Package: ansi.ColorCode("default+b"),
52-
SrcFile: resetFG,
53-
FuncStdLib: ansi.Green,
54-
FuncStdLibExported: ansi.ColorCode("green+b"),
55-
FuncMain: ansi.ColorCode("yellow+b"),
56-
FuncOther: ansi.Red,
57-
FuncOtherExported: ansi.ColorCode("red+b"),
58-
Arguments: resetFG,
47+
EOLReset: resetFG,
48+
RoutineFirst: ansi.ColorCode("magenta+b"),
49+
CreatedBy: ansi.LightBlack,
50+
Race: ansi.LightRed,
51+
Package: ansi.ColorCode("default+b"),
52+
SrcFile: resetFG,
53+
FuncMain: ansi.ColorCode("yellow+b"),
54+
FuncLocationUnknown: ansi.Red,
55+
FuncLocationUnknownExported: ansi.ColorCode("red+b"),
56+
FuncGoMod: ansi.Red,
57+
FuncGoModExported: ansi.ColorCode("red+b"),
58+
FuncGOPATH: ansi.Red,
59+
FuncGOPATHExported: ansi.ColorCode("red+b"),
60+
FuncGoPkg: ansi.Red,
61+
FuncGoPkgExported: ansi.ColorCode("red+b"),
62+
FuncStdLib: ansi.Green,
63+
FuncStdLibExported: ansi.ColorCode("green+b"),
64+
Arguments: resetFG,
5965
}
6066

6167
func writeBucketsToConsole(out io.Writer, p *Palette, a *stack.Aggregated, pf pathFormat, needsEnv bool, filter, match *regexp.Regexp) error {
@@ -200,6 +206,39 @@ func Main() error {
200206
forceColor := flag.Bool("force-color", false, "Forcibly enable coloring when with stdout is redirected")
201207
// HTML only.
202208
html := flag.String("html", "", "Output an HTML file")
209+
210+
var out io.Writer = os.Stdout
211+
p := &defaultPalette
212+
213+
flag.CommandLine.Usage = func() {
214+
if *noColor && !*forceColor {
215+
p = &Palette{}
216+
} else {
217+
out = colorable.NewColorableStdout()
218+
}
219+
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
220+
flag.CommandLine.PrintDefaults()
221+
fmt.Fprintf(out, "\nLegend:\n")
222+
fmt.Fprintf(out, " Type Exported Private\n")
223+
fmt.Fprintf(out, " main %smain.Foo()%s %smain.foo()%s\n",
224+
p.funcColor(stack.LocationUnknown, true, false), p.EOLReset,
225+
p.funcColor(stack.LocationUnknown, true, true), p.EOLReset)
226+
fmt.Fprintf(out, " <unknown> %spkg.Foo()%s %spkg.foo()%s\n",
227+
p.funcColor(stack.LocationUnknown, false, false), p.EOLReset,
228+
p.funcColor(stack.LocationUnknown, false, true), p.EOLReset)
229+
fmt.Fprintf(out, " go.mod %spkg.Foo()%s %spkg.foo()%s\n",
230+
p.funcColor(stack.GoMod, false, false), p.EOLReset,
231+
p.funcColor(stack.GoMod, false, true), p.EOLReset)
232+
fmt.Fprintf(out, " $GOPATH/src %spkg.Foo()%s %spkg.foo()%s\n",
233+
p.funcColor(stack.GOPATH, false, false), p.EOLReset,
234+
p.funcColor(stack.GOPATH, false, true), p.EOLReset)
235+
fmt.Fprintf(out, " $GOPATH/pkg/mod %spkg.Foo()%s %spkg.foo()%s\n",
236+
p.funcColor(stack.GoPkg, false, false), p.EOLReset,
237+
p.funcColor(stack.GoPkg, false, true), p.EOLReset)
238+
fmt.Fprintf(out, " $GOROOT/src %spkg.Foo()%s %spkg.Foo()%s\n",
239+
p.funcColor(stack.Stdlib, false, false), p.EOLReset,
240+
p.funcColor(stack.Stdlib, false, true), p.EOLReset)
241+
}
203242
flag.Parse()
204243

205244
log.SetFlags(log.Lmicroseconds)
@@ -227,8 +266,6 @@ func Main() error {
227266
s = stack.AnyValue
228267
}
229268

230-
var out io.Writer = os.Stdout
231-
p := &defaultPalette
232269
if *html == "" {
233270
if *noColor && !*forceColor {
234271
p = &Palette{}

internal/main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ func TestProcess(t *testing.T) {
4141
palette: testPalette,
4242
simil: stack.AnyPointer,
4343
path: basePath,
44-
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:70 ImainL()A\n",
44+
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:70 GmainR()A\n",
4545
},
4646
{
4747
name: "FullPath",
4848
palette: testPalette,
4949
simil: stack.AnyValue,
5050
path: fullPath,
5151
// "/" is used even on Windows.
52-
want: fmt.Sprintf("GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain F%s:70 ImainL()A\n", strings.Replace(filepath.Join(filepath.Dir(d), "cmd", "panic", "main.go"), "\\", "/", -1)),
52+
want: fmt.Sprintf("GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain F%s:70 GmainR()A\n", strings.Replace(filepath.Join(filepath.Dir(d), "cmd", "panic", "main.go"), "\\", "/", -1)),
5353
},
5454
{
5555
name: "NoColor",
@@ -72,7 +72,7 @@ func TestProcess(t *testing.T) {
7272
simil: stack.AnyValue,
7373
path: basePath,
7474
filter: regexp.MustCompile(`notpresent`),
75-
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:70 ImainL()A\n",
75+
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:70 GmainR()A\n",
7676
},
7777
}
7878
for i, line := range data {

internal/ui.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ type Palette struct {
2727
Race string
2828

2929
// Call line.
30-
Package string
31-
SrcFile string
32-
FuncStdLib string
33-
FuncStdLibExported string
34-
FuncMain string
35-
FuncOther string
36-
FuncOtherExported string
37-
Arguments string
30+
Package string
31+
SrcFile string
32+
FuncMain string
33+
FuncLocationUnknown string
34+
FuncLocationUnknownExported string
35+
FuncGoMod string
36+
FuncGoModExported string
37+
FuncGOPATH string
38+
FuncGOPATHExported string
39+
FuncGoPkg string
40+
FuncGoPkgExported string
41+
FuncStdLib string
42+
FuncStdLibExported string
43+
Arguments string
3844
}
3945

4046
// pathFormat determines how much to show.
@@ -109,34 +115,41 @@ func calcGoroutinesLengths(s *stack.Snapshot, pf pathFormat) (int, int) {
109115
// functionColor returns the color to be used for the function name based on
110116
// the type of package the function is in.
111117
func (p *Palette) functionColor(c *stack.Call) string {
112-
if c.Func.IsPkgMain {
118+
return p.funcColor(c.Location, c.Func.IsPkgMain, c.Func.IsExported)
119+
}
120+
121+
func (p *Palette) funcColor(l stack.Location, main, exported bool) string {
122+
if main {
113123
return p.FuncMain
114124
}
115-
switch c.Location {
125+
switch l {
126+
default:
127+
fallthrough
116128
case stack.LocationUnknown:
117-
return p.FuncOther
129+
if exported {
130+
return p.FuncLocationUnknownExported
131+
}
132+
return p.FuncLocationUnknown
118133
case stack.GoMod:
119-
if c.Func.IsExported {
120-
return p.FuncOtherExported
134+
if exported {
135+
return p.FuncGoModExported
121136
}
122-
return p.FuncOther
137+
return p.FuncGoMod
123138
case stack.GOPATH:
124-
if c.Func.IsExported {
125-
return p.FuncOtherExported
139+
if exported {
140+
return p.FuncGOPATHExported
126141
}
127-
return p.FuncOther
142+
return p.FuncGOPATH
128143
case stack.GoPkg:
129-
if c.Func.IsExported {
130-
return p.FuncOtherExported
144+
if exported {
145+
return p.FuncGoPkgExported
131146
}
132-
return p.FuncOther
147+
return p.FuncGoPkg
133148
case stack.Stdlib:
134-
if c.Func.IsExported {
149+
if exported {
135150
return p.FuncStdLibExported
136151
}
137152
return p.FuncStdLib
138-
default:
139-
return p.FuncOther
140153
}
141154
}
142155

internal/ui_test.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ import (
1414
)
1515

1616
var testPalette = &Palette{
17-
EOLReset: "A",
18-
RoutineFirst: "B",
19-
Routine: "C",
20-
CreatedBy: "D",
21-
Package: "E",
22-
SrcFile: "F",
23-
FuncStdLib: "G",
24-
FuncStdLibExported: "H",
25-
FuncMain: "I",
26-
FuncOther: "J",
27-
FuncOtherExported: "K",
28-
Arguments: "L",
17+
EOLReset: "A",
18+
RoutineFirst: "B",
19+
Routine: "C",
20+
CreatedBy: "D",
21+
Package: "E",
22+
SrcFile: "F",
23+
FuncMain: "G",
24+
FuncLocationUnknown: "H",
25+
FuncLocationUnknownExported: "I",
26+
FuncGoMod: "J",
27+
FuncGoModExported: "K",
28+
FuncGOPATH: "L",
29+
FuncGOPATHExported: "M",
30+
FuncGoPkg: "N",
31+
FuncGoPkgExported: "O",
32+
FuncStdLib: "P",
33+
FuncStdLibExported: "Q",
34+
Arguments: "R",
2935
}
3036

3137
func TestCalcBucketsLengths(t *testing.T) {
@@ -147,19 +153,19 @@ func TestStackLines(t *testing.T) {
147153
}
148154
// When printing, it prints the remote path, not the transposed local path.
149155
want := "" +
150-
" Eruntime F/goroot/src/runtime/sys_linux_amd64.s:400 HEpollwaitL(4, 0x7fff671c7118, 0xffffffff00000080, 0, 0xffffffff0028c1be, 0, 0, 0, 0, 0, ...)A\n" +
151-
" Eruntime F/goroot/src/runtime/netpoll_epoll.go:68 GnetpollL(0x901b01, 0)A\n" +
152-
" Emain F/home/user/go/src/main.go:1472 IMainL(0xc208012000)A\n" +
153-
" Efoo F/home/user/go/src/foo/bar.go:1575 KOtherExportedL()A\n" +
154-
" Efoo F/home/user/go/src/foo/bar.go:10 JotherPrivateL()A\n" +
156+
" Eruntime F/goroot/src/runtime/sys_linux_amd64.s:400 QEpollwaitR(4, 0x7fff671c7118, 0xffffffff00000080, 0, 0xffffffff0028c1be, 0, 0, 0, 0, 0, ...)A\n" +
157+
" Eruntime F/goroot/src/runtime/netpoll_epoll.go:68 PnetpollR(0x901b01, 0)A\n" +
158+
" Emain F/home/user/go/src/main.go:1472 GMainR(0xc208012000)A\n" +
159+
" Efoo F/home/user/go/src/foo/bar.go:1575 MOtherExportedR()A\n" +
160+
" Efoo F/home/user/go/src/foo/bar.go:10 LotherPrivateR()A\n" +
155161
" (...)\n"
156162
compareString(t, want, testPalette.StackLines(s, 10, 10, fullPath))
157163
want = "" +
158-
" Eruntime Fsys_linux_amd64.s:400 HEpollwaitL(4, 0x7fff671c7118, 0xffffffff00000080, 0, 0xffffffff0028c1be, 0, 0, 0, 0, 0, ...)A\n" +
159-
" Eruntime Fnetpoll_epoll.go:68 GnetpollL(0x901b01, 0)A\n" +
160-
" Emain Fmain.go:1472 IMainL(0xc208012000)A\n" +
161-
" Efoo Fbar.go:1575 KOtherExportedL()A\n" +
162-
" Efoo Fbar.go:10 JotherPrivateL()A\n" +
164+
" Eruntime Fsys_linux_amd64.s:400 QEpollwaitR(4, 0x7fff671c7118, 0xffffffff00000080, 0, 0xffffffff0028c1be, 0, 0, 0, 0, 0, ...)A\n" +
165+
" Eruntime Fnetpoll_epoll.go:68 PnetpollR(0x901b01, 0)A\n" +
166+
" Emain Fmain.go:1472 GMainR(0xc208012000)A\n" +
167+
" Efoo Fbar.go:1575 MOtherExportedR()A\n" +
168+
" Efoo Fbar.go:10 LotherPrivateR()A\n" +
163169
" (...)\n"
164170
compareString(t, want, testPalette.StackLines(s, 10, 10, basePath))
165171
}

0 commit comments

Comments
 (0)