mono / sdb

A command line client for the Mono soft debugger.
https://www.mono-project.com
MIT License
116 stars 44 forks source link

Stdout in sdb plugin #48

Closed callmekohei closed 6 years ago

callmekohei commented 6 years ago

Hello, I'm callmekohei!

Problem

I want to display app's stdout in sdb plugin.

code

open Mono.Debugger.Client
open Mono.Debugging.Client

open System.IO

[<Sealed; Command>]
type MyCommand() =
    inherit Command()
    override __.Names   = [|"mycmd"|]
    override __.Summary = "aaa bbb ccc"
    override __.Syntax  = "ddd eee fff"
    override __.Help    = "Help Help Help"
    override __.Process(args) =

    Log.Info("-----------------")

    /// I want to display app's stdout!
    ????

    Log.Info("-----------------")

I'm happy to have a tip (^_^)/

callmekohei commented 6 years ago

Hello again! I'm callmekohei!

So, I can do it!

screen shot 2018-02-15 at 3 36 18

Thank you (^_^)/


( code )

namespace Mono.Debugger.Client.Commands

#r "/usr/local/lib/sdb/sdb.exe"
#r "/usr/local/lib/sdb/Mono.Debugging.dll"
#r "/usr/local/lib/sdb/Mono.Debugging.Soft.dll"

open Mono.Debugger.Client
open Mono.Debugging.Client

open System
open System.IO

module Foo =

    let gatherOutput f args =

        try
            // Switch MemoryStream
            let ms = new MemoryStream()
            let sw = new StreamWriter(ms)
            let tw = TextWriter.Synchronized(sw)
            sw.AutoFlush <- true
            Console.SetOut(tw)

            f args

            // read data from MemoryStream
            let sr = new System.IO.StreamReader(ms)
            let mutable tmp = int64 0
            let mutable flg = true

            while flg = true do
                // wait for output
                System.Threading.Thread.Sleep 50

                if tmp = int64 0 then
                    tmp <- ms.Position
                else
                    if tmp = ms.Position then
                        flg <- false
                    else
                        tmp <- ms.Position

            ms.Position <- int64 0
            let rtn = sr.ReadToEnd()

            // Switch StandardOut
            let std = new StreamWriter(Console.OpenStandardOutput())
            std.AutoFlush <- true
            Console.SetOut(std)

            rtn
        with e -> e.Message

    let run args =
        try
            Debugger.Run(new FileInfo(args))
        with e -> Log.Info(e.Message)

    type MyRun() =
        inherit Command()
        override __.Names         = [|"run"|]
        override __.Summary       = ""
        override __.Syntax        = ""
        override __.Help          = ""
        override __.Process(args) =

            Log.Info("-----------------")
            Log.Info( gatherOutput run args )
            Log.Info("-----------------")

    [<Sealed; Command>]
    type MyCommand() =
        inherit MultiCommand()
        do base.AddCommand<MyRun>()
        override this.Names   = [|"mycmd"|]
        override this.Summary = ""
        override this.Syntax  = ""
        override this.Help    = ""