2013-12-18 7 views
6

go testコマンドは次のように説明し、-cフラグをサポートしています:GDBを使って `go test -c`で生成されたバイナリを正しくデバッグするには?

-c Compile the test binary to pkg.test but do not run it. 
    (Where pkg is the last element of the package's import path.) 

私の知る限り理解し、このようなバイナリを生成する対話形式でGDBを使用してそれを実行する方法があります。テストバイナリが/ディレクトリにいくつか/ tmpに一時的にソースとテストファイルを組み合わせて作成されているのでしかし、これは私はGDBでlistを実行したときに何が起こるかです:

Loading Go Runtime support. 
(gdb) list 
42  github.com/<username>/<project>/_test/_testmain.go: No such file or directory. 

これは、私は喜んで行くのソースを調べることができないことを意味します私が慣れていたようにGDBのコード。 -workフラグをgo testコマンドに渡すことで一時ディレクトリを強制的に保持することは可能ですが、バイナリがそのディレクトリなどに作成されていないので、まだ大きな悩みです。誰かがこの問題のきれいな解決策を見つけたのかどうか疑問に思っていました。

答えて

2

残念ながら、これは修正されない既知の問題のようです。私はこの問題には、2つのソリューションを見てきました

https://groups.google.com/forum/#!topic/golang-nuts/nIA09gp3eNU

:この議論を参照してください。

1)set substitute-pathコマンドを使用して.gdbinitファイルを作成し、 ソースの実際の場所にgdbをリダイレクトします。このファイルはgoツールで生成された になる可能性がありますが、誰かのカスタム .gdbinitファイルを上書きする危険性があり、goツールをgdbと結びつけて悪いと思われます。 アイデア

2)実行ファイル(/ tmp/...に を指している)のソースファイルパスを、それらがディスク上にある場所に置き換えます。実際のパスが/ tmp/...パスより短い場合は、これは です。 これは、このソリューションをより一般的にするために、コンパイラ/ リンカからの追加サポートを必要とする可能性があります。

それはどの決定がされてしまったために、ゴーGoogle Codeの課題トラッカーでこの問題を生み出した:

https://code.google.com/p/go/issues/detail?id=2881

をこれは迷惑ですが、それは多くの迷惑な可能性の少なくともあります。 原則として、gooolツールはソース ディレクトリに書かれてはいけません。書き込みできないディレクトリもあります。 は、終了後にファイルを他の場所に残すべきではありません。 _testmain.goでは面白くない部分があります。 gdbを使用してテストを行っている人は、 testing.Mainを破損する可能性があります。

ラス ステータス:不幸

だから、要するに、それは吸う、あなたはそれとGDBを中心にテストを実行可能に働くことができる一方で、開発チームは、それができるよう、それはのように簡単に作ることはほとんどありませんあなたのために。

0

私はまだゴランゲームには新しくありませんが、基本的なデバッグには価値があると思われます。

作業中のリストコマンドは、コード内のどこかのブレークポイントにいる限り使用できます。たとえば:

(gdb) b aws.go:54 
Breakpoint 1 at 0x61841: file /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.go, line 54. 
(gdb) r 
Starting program: /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.test 
[snip: some arnings about BinaryCache] 

Breakpoint 1, github.com/stellar/deliverator/aws.imageIsNewer (latest=0xc2081fe2d0, ami=0xc2081fe3c0, ~r2=false) 
    at /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.go:54 
54  layout := "2006-01-02T15:04:05.000Z" 
(gdb) list 
49 func imageIsNewer(latest *ec2.Image, ami *ec2.Image) bool { 
50  if latest == nil { 
51   return true 
52  } 
53 
54  layout := "2006-01-02T15:04:05.000Z" 
55 
56  amiCreationTime, amiErr := time.Parse(layout, *ami.CreationDate) 
57  if amiErr != nil { 
58   panic(amiErr) 

これはちょうど私のプロジェクトのawsサブディレクトリで次のコマンドを実行した後にされています。追加の注意点として

go test -c 
gdb aws.test 

、それはブレークポイントを置くことができる場所について非常に選択思えません。表現でなければならないように思えますが、結論は実験によるものです。

0

Go 1.5がリリースされました。公式に許可されているGoデバッガはまだありません。 Goプログラムやテストバイナリを効果的にデバッグするために、GDBを使って大した成功を収めていませんでした。 https://github.com/derekparker/delve

をデバッガでテストコードを実行するには、単に掘り下げるをインストールします:しかし、私は掘り下げ、まだ開発中である非公式のデバッガを使用して成功を収めている

go get -u github.com/derekparker/delve/cmd/dlv

...その後、ワークスペース内からのデバッガでテストを開始します。

dlv test

デバッガ・プロンプトから、あなたはシングルステップなど、ブレークポイントを設定することができます

0

GDB以外のツールを使用する場合は、godebugをチェックしてください。これを使用するには、最初にインストールします。

go get github.com/mailgun/godebug 

次に、あなたのコードに次のステートメントを追加することで、どこかにブレークポイントを挿入します。

_ = "breakpoint" 

は今godebug testコマンドを使用してテストを実行します。

godebug test 

これは、go testコマンドの多くのパラメータをサポートしています。

-test.bench string 
     regular expression per path component to select benchmarks to run 
    -test.benchmem 
     print memory allocations for benchmarks 
    -test.benchtime duration 
     approximate run time for each benchmark (default 1s) 
    -test.blockprofile string 
     write a goroutine blocking profile to the named file after execution 
    -test.blockprofilerate int 
     if >= 0, calls runtime.SetBlockProfileRate() (default 1) 
    -test.count n 
     run tests and benchmarks n times (default 1) 
    -test.coverprofile string 
     write a coverage profile to the named file after execution 
    -test.cpu string 
     comma-separated list of number of CPUs to use for each test 
    -test.cpuprofile string 
     write a cpu profile to the named file during execution 
    -test.memprofile string 
     write a memory profile to the named file after execution 
    -test.memprofilerate int 
     if >=0, sets runtime.MemProfileRate 
    -test.outputdir string 
     directory in which to write profiles 
    -test.parallel int 
     maximum test parallelism (default 4) 
    -test.run string 
     regular expression to select tests and examples to run 
    -test.short 
     run smaller test suite to save time 
    -test.timeout duration 
     if positive, sets an aggregate time limit for all tests 
    -test.trace string 
     write an execution trace to the named file after execution 
    -test.v 
     verbose: print additional output 
関連する問題