From f9be63b9ba583d093f74ebe233f55e9e1ac365ce Mon Sep 17 00:00:00 2001 From: Damien Lespiau Date: Wed, 19 Apr 2017 19:24:27 +0100 Subject: [PATCH] cmd/compile: provide a way to auto-discover -d debug keys Currently one needs to refer to the sources to have a list of accepted debug keys. We can copy what 'ssa/help' does and introspect the list of debug keys to print a more detailed help: $ go tool compile -d help usage: -d arg[,arg]* and arg is [=] is one of: append print information about append compilation closure print information about closure compilation disablenil disable nil checks dclstack run internal dclstack check gcprog print dump of GC programs nil print information about nil checks panic do not hide any compiler panic slice print information about slice compilation typeassert print information about type assertion inlining wb print information about write barriers export print export data pctab print named pc-value table ssa/help print help about SSA debugging is key-specific. Key "pctab" supports values: "pctospadj", "pctofile", "pctoline", "pctoinline", "pctopcdata" For '-d help' to be discoverable, a hint is given in the -d flag description. A last thing, today at least one go file needs to be provided to get to the code printing ssa/help. $ go tool compile -d ssa/help foo.go Add a check so one can just do '-d help' or '-d ssa/help' Caught by trybot: I needed to update fmt_test.go as I'm introducing the usage of %-*s in a format string. Fixes #20041 Change-Id: Ib2858b038c1bcbe644aa3b1a371009710c6d957d Reviewed-on: https://go-review.googlesource.com/41091 Run-TryBot: Alberto Donizetti TryBot-Result: Gobot Gobot Reviewed-by: David Chase --- src/cmd/compile/fmt_test.go | 1 + src/cmd/compile/internal/gc/main.go | 59 ++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/cmd/compile/fmt_test.go b/src/cmd/compile/fmt_test.go index 5163bee0e6..f5d491e70b 100644 --- a/src/cmd/compile/fmt_test.go +++ b/src/cmd/compile/fmt_test.go @@ -687,6 +687,7 @@ var knownFormats = map[string]string{ "reflect.Type %s": "", "rune %#U": "", "rune %c": "", + "string %-*s": "", "string %-16s": "", "string %.*s": "", "string %q": "", diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go index cf97d0cee1..6d7afa2869 100644 --- a/src/cmd/compile/internal/gc/main.go +++ b/src/cmd/compile/internal/gc/main.go @@ -51,22 +51,36 @@ var ( // Each option accepts an optional argument, as in "gcprog=2" var debugtab = []struct { name string + help string val interface{} // must be *int or *string }{ - {"append", &Debug_append}, // print information about append compilation - {"closure", &Debug_closure}, // print information about closure compilation - {"disablenil", &disable_checknil}, // disable nil checks - {"dclstack", &debug_dclstack}, // run internal dclstack checks - {"gcprog", &Debug_gcprog}, // print dump of GC programs - {"nil", &Debug_checknil}, // print information about nil checks - {"panic", &Debug_panic}, // do not hide any compiler panic - {"slice", &Debug_slice}, // print information about slice compilation - {"typeassert", &Debug_typeassert}, // print information about type assertion inlining - {"wb", &Debug_wb}, // print information about write barriers - {"export", &Debug_export}, // print export data - {"pctab", &Debug_pctab}, // print named pc-value table + {"append", "print information about append compilation", &Debug_append}, + {"closure", "print information about closure compilation", &Debug_closure}, + {"disablenil", "disable nil checks", &disable_checknil}, + {"dclstack", "run internal dclstack check", &debug_dclstack}, + {"gcprog", "print dump of GC programs", &Debug_gcprog}, + {"nil", "print information about nil checks", &Debug_checknil}, + {"panic", "do not hide any compiler panic", &Debug_panic}, + {"slice", "print information about slice compilation", &Debug_slice}, + {"typeassert", "print information about type assertion inlining", &Debug_typeassert}, + {"wb", "print information about write barriers", &Debug_wb}, + {"export", "print export data", &Debug_export}, + {"pctab", "print named pc-value table", &Debug_pctab}, } +const debugHelpHeader = `usage: -d arg[,arg]* and arg is [=] + + is one of: + +` + +const debugHelpFooter = ` + is key-specific. + +Key "pctab" supports values: + "pctospadj", "pctofile", "pctoline", "pctoinline", "pctopcdata" +` + func usage() { fmt.Printf("usage: compile [options] file.go...\n") objabi.Flagprint(1) @@ -171,7 +185,7 @@ func Main(archInit func(*Arch)) { flag.StringVar(&asmhdr, "asmhdr", "", "write assembly header to `file`") flag.StringVar(&buildid, "buildid", "", "record `id` as the build id in the export metadata") flag.BoolVar(&pure_go, "complete", false, "compiling complete package (no C or assembly)") - flag.StringVar(&debugstr, "d", "", "print debug information about items in `list`") + flag.StringVar(&debugstr, "d", "", "print debug information about items in `list`; try -d help") flag.BoolVar(&flagDWARF, "dwarf", true, "generate DWARF symbols") objabi.Flagcount("e", "no limit on number of errors reported", &Debug['e']) objabi.Flagcount("f", "debug stack frames", &Debug['f']) @@ -223,7 +237,7 @@ func Main(archInit func(*Arch)) { Ctxt.DebugInfo = debuginfo } - if flag.NArg() < 1 { + if flag.NArg() < 1 && debugstr != "help" && debugstr != "ssa/help" { usage() } @@ -273,6 +287,23 @@ func Main(archInit func(*Arch)) { if name == "" { continue } + // display help about the -d option itself and quit + if name == "help" { + fmt.Printf(debugHelpHeader) + maxLen := len("ssa/help") + for _, t := range debugtab { + if len(t.name) > maxLen { + maxLen = len(t.name) + } + } + for _, t := range debugtab { + fmt.Printf("\t%-*s\t%s\n", maxLen, t.name, t.help) + } + // ssa options have their own help + fmt.Printf("\t%-*s\t%s\n", maxLen, "ssa/help", "print help about SSA debugging") + fmt.Printf(debugHelpFooter) + os.Exit(0) + } val, valstring, haveInt := 1, "", true if i := strings.IndexAny(name, "=:"); i >= 0 { var err error -- 2.50.0