2013-08-27 16:49:13 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
// ssadump: a tool for displaying and interpreting the SSA form of Go programs.
|
2014-12-08 21:00:58 -07:00
|
|
|
package main // import "golang.org/x/tools/cmd/ssadump"
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2013-09-04 11:15:49 -06:00
|
|
|
"go/build"
|
2015-12-29 11:06:30 -07:00
|
|
|
"go/types"
|
2013-05-17 14:25:48 -06:00
|
|
|
"os"
|
2013-08-29 19:34:36 -06:00
|
|
|
"runtime"
|
2013-05-17 14:25:48 -06:00
|
|
|
"runtime/pprof"
|
|
|
|
|
2015-04-21 12:00:20 -06:00
|
|
|
"golang.org/x/tools/go/buildutil"
|
2018-08-09 11:20:37 -06:00
|
|
|
"golang.org/x/tools/go/packages"
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/go/ssa"
|
|
|
|
"golang.org/x/tools/go/ssa/interp"
|
2015-04-14 14:56:02 -06:00
|
|
|
"golang.org/x/tools/go/ssa/ssautil"
|
2013-05-17 14:25:48 -06:00
|
|
|
)
|
|
|
|
|
2016-01-04 08:40:40 -07:00
|
|
|
// flags
|
2015-02-18 10:53:21 -07:00
|
|
|
var (
|
2016-01-04 08:40:40 -07:00
|
|
|
mode = ssa.BuilderMode(0)
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2018-08-09 11:20:37 -06:00
|
|
|
testFlag = flag.Bool("test", false, "include implicit test packages and executables")
|
2014-02-11 14:52:16 -07:00
|
|
|
|
2018-08-09 11:20:37 -06:00
|
|
|
runFlag = flag.Bool("run", false, "interpret the SSA program")
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2015-02-18 10:53:21 -07:00
|
|
|
interpFlag = flag.String("interp", "", `Options controlling the SSA test interpreter.
|
2013-05-17 14:25:48 -06:00
|
|
|
The value is a sequence of zero or more more of these letters:
|
|
|
|
R disable [R]ecover() from panic; show interpreter crash instead.
|
|
|
|
T [T]race execution of the program. Best for single-threaded programs!
|
|
|
|
`)
|
2016-01-04 08:40:40 -07:00
|
|
|
|
|
|
|
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
2018-08-09 11:20:37 -06:00
|
|
|
|
|
|
|
args stringListValue
|
2015-02-18 10:53:21 -07:00
|
|
|
)
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2016-01-04 08:40:40 -07:00
|
|
|
func init() {
|
|
|
|
flag.Var(&mode, "build", ssa.BuilderModeDoc)
|
|
|
|
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
|
2018-08-09 11:20:37 -06:00
|
|
|
flag.Var(&args, "arg", "add argument to interpreted program")
|
2016-01-04 08:40:40 -07:00
|
|
|
}
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
const usage = `SSA builder and interpreter.
|
2018-08-09 11:20:37 -06:00
|
|
|
Usage: ssadump [-build=[DBCSNFL]] [-test] [-run] [-interp=[TR]] [-arg=...] package...
|
2013-05-17 14:25:48 -06:00
|
|
|
Use -help flag to display options.
|
|
|
|
|
|
|
|
Examples:
|
2015-04-21 12:00:20 -06:00
|
|
|
% ssadump -build=F hello.go # dump SSA form of a single package
|
2017-11-28 14:15:16 -07:00
|
|
|
% ssadump -build=F -test fmt # dump SSA form of a package and its tests
|
2014-02-11 14:52:16 -07:00
|
|
|
% ssadump -run -interp=T hello.go # interpret a program, with tracing
|
2018-08-09 11:20:37 -06:00
|
|
|
|
2017-11-28 14:15:16 -07:00
|
|
|
The -run flag causes ssadump to run the first package named main.
|
|
|
|
|
|
|
|
Interpretation of the standard "testing" package is no longer supported.
|
2013-05-17 14:25:48 -06:00
|
|
|
`
|
|
|
|
|
|
|
|
func main() {
|
2014-02-11 14:52:16 -07:00
|
|
|
if err := doMain(); err != nil {
|
2014-11-17 10:58:28 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "ssadump: %s\n", err)
|
2014-02-11 14:52:16 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doMain() error {
|
2013-05-17 14:25:48 -06:00
|
|
|
flag.Parse()
|
2018-08-09 11:20:37 -06:00
|
|
|
if len(flag.Args()) == 0 {
|
|
|
|
fmt.Fprint(os.Stderr, usage)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2018-08-09 11:20:37 -06:00
|
|
|
cfg := &packages.Config{
|
|
|
|
Mode: packages.LoadSyntax,
|
|
|
|
Tests: *testFlag,
|
|
|
|
}
|
2015-04-21 12:00:20 -06:00
|
|
|
|
|
|
|
// Choose types.Sizes from conf.Build.
|
2018-08-09 11:20:37 -06:00
|
|
|
// TODO(adonovan): remove this when go/packages provides a better way.
|
2014-01-08 12:46:17 -07:00
|
|
|
var wordSize int64 = 8
|
2018-08-09 11:20:37 -06:00
|
|
|
switch build.Default.GOARCH {
|
2014-01-08 12:46:17 -07:00
|
|
|
case "386", "arm":
|
|
|
|
wordSize = 4
|
|
|
|
}
|
2018-08-09 11:20:37 -06:00
|
|
|
cfg.TypeChecker.Sizes = &types.StdSizes{
|
2014-01-08 12:46:17 -07:00
|
|
|
MaxAlign: 8,
|
|
|
|
WordSize: wordSize,
|
|
|
|
}
|
2013-05-31 14:14:13 -06:00
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
var interpMode interp.Mode
|
|
|
|
for _, c := range *interpFlag {
|
|
|
|
switch c {
|
|
|
|
case 'T':
|
|
|
|
interpMode |= interp.EnableTracing
|
|
|
|
case 'R':
|
|
|
|
interpMode |= interp.DisableRecover
|
|
|
|
default:
|
2014-04-09 16:00:57 -06:00
|
|
|
return fmt.Errorf("unknown -interp option: '%c'", c)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profiling support.
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
2014-02-11 14:52:16 -07:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2018-08-09 11:20:37 -06:00
|
|
|
// Load, parse and type-check the initial packages,
|
|
|
|
// and, if -run, their dependencies.
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
if *runFlag {
|
2018-08-09 11:20:37 -06:00
|
|
|
cfg.Mode = packages.LoadAllSyntax
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
}
|
2018-08-09 11:20:37 -06:00
|
|
|
initial, err := packages.Load(cfg, flag.Args()...)
|
2014-01-15 19:37:55 -07:00
|
|
|
if err != nil {
|
2014-02-11 14:52:16 -07:00
|
|
|
return err
|
2013-09-06 16:13:57 -06:00
|
|
|
}
|
2018-08-09 11:20:37 -06:00
|
|
|
if len(initial) == 0 {
|
|
|
|
return fmt.Errorf("no packages")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create SSA-form program representation.
|
|
|
|
prog, pkgs := ssautil.Packages(initial, mode)
|
2013-10-23 16:07:53 -06:00
|
|
|
|
2018-08-09 11:20:37 -06:00
|
|
|
for i, p := range pkgs {
|
|
|
|
if p == nil {
|
|
|
|
return fmt.Errorf("cannot build SSA for package %s", initial[i])
|
2016-07-20 13:03:06 -06:00
|
|
|
}
|
2015-04-14 14:56:02 -06:00
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2018-08-09 11:20:37 -06:00
|
|
|
if !*runFlag {
|
|
|
|
// Build and display only the initial packages
|
|
|
|
// (and synthetic wrappers).
|
|
|
|
for _, p := range pkgs {
|
|
|
|
p.Build()
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Run the interpreter.
|
|
|
|
// Build SSA for all packages.
|
2015-08-31 15:50:03 -06:00
|
|
|
prog.Build()
|
2015-04-14 14:56:02 -06:00
|
|
|
|
2018-08-09 11:20:37 -06:00
|
|
|
// The interpreter needs the runtime package.
|
|
|
|
// It is a limitation of go/packages that
|
|
|
|
// we cannot add "runtime" to its initial set,
|
|
|
|
// we can only check that it is present.
|
|
|
|
if prog.ImportedPackage("runtime") == nil {
|
|
|
|
return fmt.Errorf("-run: program does not depend on runtime")
|
2013-09-06 16:13:57 -06:00
|
|
|
}
|
2014-01-08 12:46:17 -07:00
|
|
|
|
2014-02-11 14:52:16 -07:00
|
|
|
if runtime.GOARCH != build.Default.GOARCH {
|
2015-01-30 11:30:23 -07:00
|
|
|
return fmt.Errorf("cross-interpretation is not supported (target has GOARCH %s, interpreter has %s)",
|
2014-02-11 14:52:16 -07:00
|
|
|
build.Default.GOARCH, runtime.GOARCH)
|
2014-01-08 12:46:17 -07:00
|
|
|
}
|
|
|
|
|
2018-08-09 11:20:37 -06:00
|
|
|
// Run first main package.
|
|
|
|
for _, main := range ssautil.MainPackages(pkgs) {
|
|
|
|
fmt.Fprintf(os.Stderr, "Running: %s\n", main.Pkg.Path())
|
|
|
|
os.Exit(interp.Interpret(main, interpMode, cfg.TypeChecker.Sizes, main.Pkg.Path(), args))
|
2016-07-20 13:03:06 -06:00
|
|
|
}
|
2018-08-09 11:20:37 -06:00
|
|
|
return fmt.Errorf("no main package")
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2014-02-11 14:52:16 -07:00
|
|
|
return nil
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2018-08-09 11:20:37 -06:00
|
|
|
|
|
|
|
// stringListValue is a flag.Value that accumulates strings.
|
|
|
|
// e.g. --flag=one --flag=two would produce []string{"one", "two"}.
|
|
|
|
type stringListValue []string
|
|
|
|
|
|
|
|
func newStringListValue(val []string, p *[]string) *stringListValue {
|
|
|
|
*p = val
|
|
|
|
return (*stringListValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stringListValue) Get() interface{} { return []string(*ss) }
|
|
|
|
|
|
|
|
func (ss *stringListValue) String() string { return fmt.Sprintf("%q", *ss) }
|
|
|
|
|
|
|
|
func (ss *stringListValue) Set(s string) error { *ss = append(*ss, s); return nil }
|