callmekohei's blog

callmekoheiのひとりごと

( Debug with FSharp ) FSharpでデバッグしてみた!

f:id:callmekohei00:20171021122049p:plain

Summary

FSharpでコマンドラインデバッグしてみた

require

sdb

How to do

(install)

$ git clone https://github.com/mono/sdb
$ cd sdb
$ git submodule update --init --recursive
$ make

(check)

$ sdb --version
Mono soft debugger (sdb) 1.5.6503.20649

$ which sdb
/usr/local/bin/sdb

$ sdb
(sdb)
(sdb) cfg list
'AllowMethodEvaluation' = 'True'
'AllowTargetInvoke' = 'True'
'AllowToStringCalls' = 'True'
'ChunkRawStrings' = 'False'
'ConnectionAttemptInterval' = '500'
'DefaultDatabaseFile' = ''
'DebugLogging' = 'True'
'DisableColors' = 'False'
'EllipsizeStrings' = 'True'
'EllipsizeThreshold' = '100'
'EnableControlC' = 'True'
'EvaluationTimeout' = '1000'
'ExceptionIdentifier' = '$exception'
'FlattenHierarchy' = 'True'
'HexadecimalIntegers' = 'False'
'InputPrompt' = '(sdb)'
'LoadDatabaseAutomatically' = 'False'
'LogInternalErrors' = 'True'
'LogRuntimeSpew' = 'True'
'MaxConnectionAttempts' = '1'
'MemberEvaluationTimeout' = '5000'
'PreferDisassembly' = 'False'
'RuntimeExecutable' = ''
'RuntimePrefix' = '/usr/local/'
'SaveDatabaseAutomatically' = 'False'
'StepOverPropertiesAndOperators' = 'True'

(point!) RuntimePrefixが違う場合があるのでその場合は変更する

(sdb) cfg s RuntimePrefix /usr/local/ ( was /usr )

(debugging) コードを書く

$ vim test.fs
let bar() = 
    stdout.WriteLine("abc")

let foo (str:string) =
    stdout.WriteLine(str)
    bar()

[<EntryPointAttribute>]
let main _ =
    let s = "Foo!"
    foo s
    stdout.WriteLine("callmekohei")
    0

デバッグ用にコンパイル

$ fsharpc -g test.fs

デバッグしてみる

デバッガを立ち上げる

$ sdb
Welcome to the Mono soft debugger (sdb 1.5.6503.20649)
Type 'help' for a list of commands or 'quit' to exit

ブレイクポイントの設定と一覧表示

(sdb) bp add at test.fs 11
Breakpoint '0' added at '/Users/callmekohei/tmp/test.fs:11'
(sdb) bp add at test.fs 12
Breakpoint '1' added at '/Users/callmekohei/tmp/test.fs:12'
(sdb) bp
#0 '/Users/callmekohei/tmp/test.fs:11'
#1 '/Users/callmekohei/tmp/test.fs:12'

コードを実行

(sdb) r test.exe
Inferior process '1766' ('test.exe') started
Event: 'TargetReady'
[Mono] Loaded assembly: /Users/callmekohei/tmp/test.exe
[Mono] Loaded assembly: /usr/local/Cellar/mono/5.0.1.1/lib/mono/gac/FSharp.Core/4.4.1.0__b03f5f7f11d50a3a/FSharp.Core.dll
[Mono] Resolved pending breakpoint at 'test.fs:11,1' to int Test.main (string[] _arg1) [0x00000].
[Mono] Resolved pending breakpoint at 'test.fs:12,1' to int Test.main (string[] _arg1) [0x0001f].
Hit breakpoint at '/Users/callmekohei/tmp/test.fs:11'
#0 [0x00000000] Test.main at /Users/callmekohei/tmp/test.fs:11
    foo s
Event: 'TargetHitBreakpoint'

ソースコードを表示する(現在の場所が緑色になっている)

(sdb) src
Source file '/Users/callmekohei/tmp/test.fs' is newer than the debuggee executable
       1:    let bar() = 
       2:        stdout.WriteLine("abc")
       3:    
       4:    let foo (str:string) =
       5:        stdout.WriteLine(str)
       6:        bar()
       7:    
       8:    [<EntryPointAttribute>]
       9:    let main _ =
      10:        let s = "Foo!"
      11:        foo s
      12:        stdout.WriteLine("callmekohei")
      13:        0

ステップ実行(次の行にすすむ)

(sdb) s
Inferior process '1766' ('test.exe') resumed
Foo!
abc
Inferior process '1766' ('test.exe') suspended
#0 [0x0000001F] Test.main at /Users/callmekohei/tmp/test.fs:12
    stdout.WriteLine("callmekohei")
Event: 'TargetStopped'

コンティニュー実行(次のブレイクポイントまで実行)

(sdb) c
Inferior process '1766' ('test.exe') resumed
Hit breakpoint at '/Users/callmekohei/tmp/test.fs:12'
#0 [0x0000001F] Test.main at /Users/callmekohei/tmp/test.fs:12
    stdout.WriteLine("callmekohei")
Event: 'TargetHitBreakpoint'

コンティニュー実行(次のブレイクポイントまで実行)

(sdb) c
Inferior process '1766' ('test.exe') resumed
callmekohei
Inferior process '1766' ('test.exe') exited with code '0'
Event: 'TargetExited'

デバッガを終了する

(sdb) q
Bye

いまのところよくわからないところ

関数(メソッド)の中身でブレイクポイントを設定できない?

どなたか、わかる方いれば教えていただけるとたすかります!

一応イシューかいてみました。

github.com