You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.0 KiB

  1. // Copyright 2013 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. // guru: a tool for answering questions about Go source code.
  5. //
  6. // http://golang.org/s/using-guru
  7. //
  8. // Run with -help flag or help subcommand for usage information.
  9. //
  10. package main // import "golang.org/x/tools/cmd/guru"
  11. import (
  12. "bufio"
  13. "flag"
  14. "fmt"
  15. "go/build"
  16. "go/token"
  17. "io"
  18. "log"
  19. "os"
  20. "runtime/pprof"
  21. "strings"
  22. "sync"
  23. "golang.org/x/tools/go/buildutil"
  24. )
  25. // flags
  26. var (
  27. modifiedFlag = flag.Bool("modified", false, "read archive of modified files from standard input")
  28. scopeFlag = flag.String("scope", "", "comma-separated list of `packages` the analysis should be limited to")
  29. ptalogFlag = flag.String("ptalog", "", "write points-to analysis log to `file`")
  30. jsonFlag = flag.Bool("json", false, "emit output in JSON format")
  31. reflectFlag = flag.Bool("reflect", false, "analyze reflection soundly (slow)")
  32. cpuprofileFlag = flag.String("cpuprofile", "", "write CPU profile to `file`")
  33. )
  34. func init() {
  35. flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
  36. }
  37. const useHelp = "Run 'guru -help' for more information.\n"
  38. const helpMessage = `Go source code guru.
  39. Usage: guru [flags] <mode> <position>
  40. The mode argument determines the query to perform:
  41. callees show possible targets of selected function call
  42. callers show possible callers of selected function
  43. callstack show path from callgraph root to selected function
  44. definition show declaration of selected identifier
  45. describe describe selected syntax: definition, methods, etc
  46. freevars show free variables of selection
  47. implements show 'implements' relation for selected type or method
  48. peers show send/receive corresponding to selected channel op
  49. pointsto show variables the selected pointer may point to
  50. referrers show all refs to entity denoted by selected identifier
  51. what show basic information about the selected syntax node
  52. whicherrs show possible values of the selected error variable
  53. The position argument specifies the filename and byte offset (or range)
  54. of the syntax element to query. For example:
  55. foo.go:#123,#128
  56. bar.go:#123
  57. The -json flag causes guru to emit output in JSON format;
  58. golang.org/x/tools/cmd/guru/serial defines its schema.
  59. Otherwise, the output is in an editor-friendly format in which
  60. every line has the form "pos: text", where pos is "-" if unknown.
  61. The -modified flag causes guru to read an archive from standard input.
  62. Files in this archive will be used in preference to those in
  63. the file system. In this way, a text editor may supply guru
  64. with the contents of its unsaved buffers. Each archive entry
  65. consists of the file name, a newline, the decimal file size,
  66. another newline, and the contents of the file.
  67. The -scope flag restricts analysis to the specified packages.
  68. Its value is a comma-separated list of patterns of these forms:
  69. golang.org/x/tools/cmd/guru # a single package
  70. golang.org/x/tools/... # all packages beneath dir
  71. ... # the entire workspace.
  72. A pattern preceded by '-' is negative, so the scope
  73. encoding/...,-encoding/xml
  74. matches all encoding packages except encoding/xml.
  75. User manual: http://golang.org/s/using-guru
  76. Example: describe syntax at offset 530 in this file (an import spec):
  77. $ guru describe src/golang.org/x/tools/cmd/guru/main.go:#530
  78. `
  79. func printHelp() {
  80. fmt.Fprintln(os.Stderr, helpMessage)
  81. fmt.Fprintln(os.Stderr, "Flags:")
  82. flag.PrintDefaults()
  83. }
  84. func main() {
  85. log.SetPrefix("guru: ")
  86. log.SetFlags(0)
  87. // Don't print full help unless -help was requested.
  88. // Just gently remind users that it's there.
  89. flag.Usage = func() { fmt.Fprint(os.Stderr, useHelp) }
  90. flag.CommandLine.Init(os.Args[0], flag.ContinueOnError) // hack
  91. if err := flag.CommandLine.Parse(os.Args[1:]); err != nil {
  92. // (err has already been printed)
  93. if err == flag.ErrHelp {
  94. printHelp()
  95. }
  96. os.Exit(2)
  97. }
  98. args := flag.Args()
  99. if len(args) != 2 {
  100. flag.Usage()
  101. os.Exit(2)
  102. }
  103. mode, posn := args[0], args[1]
  104. if mode == "help" {
  105. printHelp()
  106. os.Exit(2)
  107. }
  108. // Set up points-to analysis log file.
  109. var ptalog io.Writer
  110. if *ptalogFlag != "" {
  111. if f, err := os.Create(*ptalogFlag); err != nil {
  112. log.Fatalf("Failed to create PTA log file: %s", err)
  113. } else {
  114. buf := bufio.NewWriter(f)
  115. ptalog = buf
  116. defer func() {
  117. if err := buf.Flush(); err != nil {
  118. log.Printf("flush: %s", err)
  119. }
  120. if err := f.Close(); err != nil {
  121. log.Printf("close: %s", err)
  122. }
  123. }()
  124. }
  125. }
  126. // Profiling support.
  127. if *cpuprofileFlag != "" {
  128. f, err := os.Create(*cpuprofileFlag)
  129. if err != nil {
  130. log.Fatal(err)
  131. }
  132. pprof.StartCPUProfile(f)
  133. defer pprof.StopCPUProfile()
  134. }
  135. ctxt := &build.Default
  136. // If there were modified files,
  137. // read them from the standard input and
  138. // overlay them on the build context.
  139. if *modifiedFlag {
  140. modified, err := buildutil.ParseOverlayArchive(os.Stdin)
  141. if err != nil {
  142. log.Fatal(err)
  143. }
  144. // All I/O done by guru needs to consult the modified map.
  145. // The ReadFile done by referrers does,
  146. // but the loader's cgo preprocessing currently does not.
  147. if len(modified) > 0 {
  148. ctxt = buildutil.OverlayContext(ctxt, modified)
  149. }
  150. }
  151. var outputMu sync.Mutex
  152. output := func(fset *token.FileSet, qr QueryResult) {
  153. outputMu.Lock()
  154. defer outputMu.Unlock()
  155. if *jsonFlag {
  156. // JSON output
  157. fmt.Printf("%s\n", qr.JSON(fset))
  158. } else {
  159. // plain output
  160. printf := func(pos interface{}, format string, args ...interface{}) {
  161. fprintf(os.Stdout, fset, pos, format, args...)
  162. }
  163. qr.PrintPlain(printf)
  164. }
  165. }
  166. // Avoid corner case of split("").
  167. var scope []string
  168. if *scopeFlag != "" {
  169. scope = strings.Split(*scopeFlag, ",")
  170. }
  171. // Ask the guru.
  172. query := Query{
  173. Pos: posn,
  174. Build: ctxt,
  175. Scope: scope,
  176. PTALog: ptalog,
  177. Reflection: *reflectFlag,
  178. Output: output,
  179. }
  180. if err := Run(mode, &query); err != nil {
  181. log.Fatal(err)
  182. }
  183. }