Skip to content

Commit

Permalink
cmd/*: 调整分组帮助信息
Browse files Browse the repository at this point in the history
  • Loading branch information
hitzhangjie committed Nov 25, 2020
1 parent 50066cf commit 32184c7
Show file tree
Hide file tree
Showing 24 changed files with 175 additions and 79 deletions.
7 changes: 4 additions & 3 deletions 0_godbg/godbg/cmd/debug/backtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

var backtraceCmd = &cobra.Command{
Use: "bt",
Short: "打印调用栈信息",
Use: "bt",
Short: "打印调用栈信息",
Aliases: []string{"backtrace"},
Annotations: map[string]string{
cmdGroupKey: cmdGroupInfo,
cmdGroupAnnotation: cmdGroupInfo,
},
Run: func(cmd *cobra.Command, args []string) {
os.Exit(0)
Expand Down
6 changes: 2 additions & 4 deletions 0_godbg/godbg/cmd/debug/break.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"syscall"

"godbg/target"
Expand All @@ -23,11 +22,10 @@ var breakCmd = &cobra.Command{
- [文件名:]函数名`,
Aliases: []string{"b", "breakpoint"},
Annotations: map[string]string{
cmdGroupKey: cmdGroupBreakpoints,
cmdGroupAnnotation: cmdGroupBreakpoints,
},
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("break %s\n", strings.Join(args, " "))

//fmt.Printf("break %s\n", strings.Join(args, " "))
if len(args) != 1 {
return errors.New("参数错误")
}
Expand Down
3 changes: 1 addition & 2 deletions 0_godbg/godbg/cmd/debug/breakpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ var breaksCmd = &cobra.Command{
Long: "列出所有断点",
Aliases: []string{"bs", "breakpoints"},
Annotations: map[string]string{
cmdGroupKey: cmdGroupBreakpoints,
cmdGroupAnnotation: cmdGroupBreakpoints,
},
RunE: func(cmd *cobra.Command, args []string) error {

bs := target.Breakpoints{}
for _, b := range breakpoints {
bs = append(bs, *b)
Expand Down
8 changes: 3 additions & 5 deletions 0_godbg/godbg/cmd/debug/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package debug
import (
"errors"
"fmt"
"strings"
"syscall"

"godbg/target"
Expand All @@ -12,15 +11,14 @@ import (
)

var clearCmd = &cobra.Command{
Use: "clear <n>",
Use: "clear <breakpoint no.>",
Short: "清除指定编号的断点",
Long: `清除指定编号的断点`,
Annotations: map[string]string{
cmdGroupKey: cmdGroupBreakpoints,
cmdGroupAnnotation: cmdGroupBreakpoints,
},
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("clear %s\n", strings.Join(args, " "))

//fmt.Printf("clear %s\n", strings.Join(args, " "))
id, err := cmd.Flags().GetUint64("n")
if err != nil {
return err
Expand Down
7 changes: 3 additions & 4 deletions 0_godbg/godbg/cmd/debug/clearall.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
)

var clearallCmd = &cobra.Command{
Use: "clearall <n>",
Use: "clearall",
Short: "清除所有的断点",
Long: `清除所有的断点`,
Annotations: map[string]string{
cmdGroupKey: cmdGroupBreakpoints,
cmdGroupAnnotation: cmdGroupBreakpoints,
},
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("clearall")

//fmt.Println("clearall")
for _, brk := range breakpoints {
n, err := syscall.PtracePokeData(TraceePID, brk.Addr, []byte{brk.Orig})
if err != nil || n != 1 {
Expand Down
5 changes: 2 additions & 3 deletions 0_godbg/godbg/cmd/debug/continue.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ var continueCmd = &cobra.Command{
Use: "continue",
Short: "运行到下个断点",
Annotations: map[string]string{
cmdGroupKey: cmdGroupCtrlFlow,
cmdGroupAnnotation: cmdGroupCtrlFlow,
},
Aliases: []string{"c"},
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("continue")

//fmt.Println("continue")
// 读取PC值
regs := syscall.PtraceRegs{}
err := syscall.PtraceGetRegs(TraceePID, &regs)
Expand Down
3 changes: 1 addition & 2 deletions 0_godbg/godbg/cmd/debug/disass.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ var disassCmd = &cobra.Command{
Use: "disass <locspec>",
Short: "反汇编机器指令",
Annotations: map[string]string{
cmdGroupKey: cmdGroupSource,
cmdGroupAnnotation: cmdGroupSource,
},
Aliases: []string{"dis", "disassemble"},
RunE: func(cmd *cobra.Command, args []string) error {

max, _ := cmd.Flags().GetUint("n")
syntax, _ := cmd.Flags().GetString("syntax")

Expand Down
2 changes: 1 addition & 1 deletion 0_godbg/godbg/cmd/debug/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var displayCmd = &cobra.Command{
Use: "display <var|reg>",
Short: "始终显示变量或寄存器值",
Annotations: map[string]string{
cmdGroupKey: cmdGroupInfo,
cmdGroupAnnotation: cmdGroupInfo,
},
Run: func(cmd *cobra.Command, args []string) {
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion 0_godbg/godbg/cmd/debug/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var exitCmd = &cobra.Command{
Use: "exit",
Short: "结束调试会话",
Annotations: map[string]string{
cmdGroupKey: cmdGroupOthers,
cmdGroupAnnotation: cmdGroupOthers,
},
Run: func(cmd *cobra.Command, args []string) {
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion 0_godbg/godbg/cmd/debug/finish.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var finishCmd = &cobra.Command{
Use: "finish",
Short: "退出当前函数",
Annotations: map[string]string{
cmdGroupKey: cmdGroupCtrlFlow,
cmdGroupAnnotation: cmdGroupCtrlFlow,
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("finish")
Expand Down
4 changes: 2 additions & 2 deletions 0_godbg/godbg/cmd/debug/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

var frameCmd = &cobra.Command{
Use: "frame",
Use: "frame <frame no.>",
Short: "选择调用栈中栈帧",
Annotations: map[string]string{
cmdGroupKey: cmdGroupInfo,
cmdGroupAnnotation: cmdGroupInfo,
},
Run: func(cmd *cobra.Command, args []string) {
os.Exit(0)
Expand Down
4 changes: 2 additions & 2 deletions 0_godbg/godbg/cmd/debug/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

var listCmd = &cobra.Command{
Use: "list <linespec>",
Use: "list [linespec]",
Short: "查看源码信息",
Aliases: []string{"l"},
Annotations: map[string]string{
cmdGroupKey: cmdGroupSource,
cmdGroupAnnotation: cmdGroupSource,
cobraprompt.CALLBACK_ANNOTATION: suggestionListSourceFiles,
},
Run: func(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion 0_godbg/godbg/cmd/debug/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var nextCmd = &cobra.Command{
Use: "next",
Short: "执行一条语句",
Annotations: map[string]string{
cmdGroupKey: cmdGroupCtrlFlow,
cmdGroupAnnotation: cmdGroupCtrlFlow,
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("next")
Expand Down
5 changes: 2 additions & 3 deletions 0_godbg/godbg/cmd/debug/pmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (
)

var pmemCmd = &cobra.Command{
Use: "pmem ",
Use: "pmem [flags] <address>",
Short: "打印内存数据",
Annotations: map[string]string{
cmdGroupKey: cmdGroupInfo,
cmdGroupAnnotation: cmdGroupInfo,
},
RunE: func(cmd *cobra.Command, args []string) error {

count, _ := cmd.Flags().GetUint("count")
format, _ := cmd.Flags().GetString("fmt")
size, _ := cmd.Flags().GetUint("size")
Expand Down
2 changes: 1 addition & 1 deletion 0_godbg/godbg/cmd/debug/pregs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var pregsCmd = &cobra.Command{
Use: "pregs",
Short: "打印寄存器数据",
Annotations: map[string]string{
cmdGroupKey: cmdGroupInfo,
cmdGroupAnnotation: cmdGroupInfo,
},
RunE: func(cmd *cobra.Command, args []string) error {
regsOut := syscall.PtraceRegs{}
Expand Down
7 changes: 4 additions & 3 deletions 0_godbg/godbg/cmd/debug/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

var printCmd = &cobra.Command{
Use: "print <var|reg>",
Short: "打印变量或寄存器值",
Use: "print <var|reg>",
Short: "打印变量或寄存器值",
Aliases: []string{"p"},
Annotations: map[string]string{
cmdGroupKey: cmdGroupInfo,
cmdGroupAnnotation: cmdGroupInfo,
},
Run: func(cmd *cobra.Command, args []string) {
os.Exit(0)
Expand Down
11 changes: 6 additions & 5 deletions 0_godbg/godbg/cmd/debug/ptypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import (
"github.com/spf13/cobra"
)

var ptypesCmd = &cobra.Command{
Use: "ptypes <variable>",
Short: "打印变量类型信息",
var ptypeCmd = &cobra.Command{
Use: "ptype <variable|type>",
Short: "打印变量类型信息",
Aliases: []string{"pt"},
Annotations: map[string]string{
cmdGroupKey: cmdGroupInfo,
cmdGroupAnnotation: cmdGroupInfo,
},
Run: func(cmd *cobra.Command, args []string) {
os.Exit(0)
},
}

func init() {
debugRootCmd.AddCommand(ptypesCmd)
debugRootCmd.AddCommand(ptypeCmd)
}
69 changes: 44 additions & 25 deletions 0_godbg/godbg/cmd/debug/root_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"sort"
"strings"

"godbg/target"

Expand All @@ -13,15 +14,19 @@ import (
)

const (
cmdGroupKey = "cmd_group_key"
cmdGroupBreakpoints = "breakpoint"
cmdGroupSource = "code"
cmdGroupCtrlFlow = "ctrlflow"
cmdGroupInfo = "information"
cmdGroupOthers = "other"

prefix = "godbg> "
description = "interactive debugging commands"
cmdGroupAnnotation = "cmd_group_annotation"

cmdGroupBreakpoints = "1-breaks"
cmdGroupSource = "2-source"
cmdGroupCtrlFlow = "3-execute"
cmdGroupInfo = "4-info"
cmdGroupOthers = "5-other"
cmdGroupCobra = "other"

cmdGroupDelimiter = "-"

prefix = "godbg> "
descShort = "godbg interactive debugging commands"
)

const (
Expand All @@ -34,28 +39,34 @@ var (
)

var debugRootCmd = &cobra.Command{
Use: "",
Short: description,
Use: "help [command]",
Short: descShort,
}

// NewDebugShell 创建一个debug专用的交互管理器
func NewDebugShell() *cobraprompt.CobraPrompt {

fn := func() func(cmd *cobra.Command) error {
return func(cmd *cobra.Command) error {
usage := groupDebugCommands(cmd)
fmt.Println(usage)
return nil
}
fn := func(cmd *cobra.Command, args []string) {
// 描述信息
fmt.Println(cmd.Short)
fmt.Println()

// 使用信息
fmt.Println(cmd.Use)
fmt.Println(cmd.Flags().FlagUsages())

// 命令分组
usage := helpMessageByGroups(cmd)
fmt.Println(usage)
}
debugRootCmd.SetUsageFunc(fn())
debugRootCmd.SetHelpFunc(fn)

return &cobraprompt.CobraPrompt{
RootCmd: debugRootCmd,
DynamicSuggestionsFunc: dynamicSuggestions,
ResetFlagsFlag: true,
ResetFlagsFlag: false,
GoPromptOptions: []prompt.Option{
prompt.OptionTitle(description),
prompt.OptionTitle(descShort),
prompt.OptionPrefix(prefix),
prompt.OptionSuggestionBGColor(prompt.DarkBlue),
prompt.OptionDescriptionBGColor(prompt.DarkBlue),
Expand All @@ -72,28 +83,33 @@ func NewDebugShell() *cobraprompt.CobraPrompt {
}
}

// groupDebugCommands 将各个命令按照分组归类,再展示帮助信息
func groupDebugCommands(cmd *cobra.Command) string {
// helpMessageByGroups 将各个命令按照分组归类,再展示帮助信息
func helpMessageByGroups(cmd *cobra.Command) string {

// key:group, val:sorted commands in same group
groups := map[string][]string{}
for _, c := range cmd.Commands() {
// 如果没有指定命令分组,放入other组
var groupName string
v, ok := c.Annotations[cmdGroupKey]
v, ok := c.Annotations[cmdGroupAnnotation]
if !ok {
groupName = "other"
} else {
groupName = v
}

groupCmds, ok := groups[groupName]
groupCmds = append(groupCmds, fmt.Sprintf("%-16s:\t%s", c.Use, c.Short))
groupCmds = append(groupCmds, fmt.Sprintf(" %-16s:%s", c.Name(), c.Short))
sort.Strings(groupCmds)

groups[groupName] = groupCmds
}

if len(groups[cmdGroupCobra]) != 0 {
groups[cmdGroupOthers] = append(groups[cmdGroupOthers], groups[cmdGroupCobra]...)
}
delete(groups, cmdGroupCobra)

// 按照分组名进行排序
groupNames := []string{}
for k, _ := range groups {
Expand All @@ -105,7 +121,10 @@ func groupDebugCommands(cmd *cobra.Command) string {
buf := bytes.Buffer{}
for _, groupName := range groupNames {
commands, _ := groups[groupName]
buf.WriteString(fmt.Sprintf("[%s]\n", groupName))

group := strings.Split(groupName, cmdGroupDelimiter)[1]
buf.WriteString(fmt.Sprintf("- [%s]\n", group))

for _, cmd := range commands {
buf.WriteString(fmt.Sprintf("%s\n", cmd))
}
Expand Down
2 changes: 1 addition & 1 deletion 0_godbg/godbg/cmd/debug/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var setCmd = &cobra.Command{
Use: "set <var|reg>=<value>",
Short: "设置变量或寄存器值",
Annotations: map[string]string{
cmdGroupKey: cmdGroupInfo,
cmdGroupAnnotation: cmdGroupInfo,
},
Run: func(cmd *cobra.Command, args []string) {
os.Exit(0)
Expand Down
Loading

0 comments on commit 32184c7

Please sign in to comment.