GordianDotNet / VSMonoDebugger

Enables Visual Studio 2017 to deploy and debug a mono application on remote linux machines via SSH.
MIT License
49 stars 25 forks source link

Add support for stack frames and expressions evaluation. This enables… #13

Closed MACRO-255 closed 4 years ago

MACRO-255 commented 5 years ago

Add support for stack frames and expressions evaluation. This enables the watch window, hovering over variables, the locals window and the callstack window.

This has been tested against an embedded mono project.

GordianDotNet commented 5 years ago

I finally had time to test the code.

Could you provide me with an embedded mono project or share a website where the creation is explained? With Mono on Linux and a simple C# project these features already exist with the current version.

I think there are certain differences to embedded mono that I don't capture with my tests. The pull request now displays many things twice for normal mono projects (for example stacktrace).

MACRO-255 commented 5 years ago

Hi there,

I have mostly used the mono docs https://www.mono-project.com/docs/advanced/embedding/ but it might take some effort to get it to work.

I haven’t tested it but this project seems to do the right things https://gist.github.com/zwcloud/b342d264176ee2143aca970a9933e5cc

Maybe it could work as a test case?

My own code is a bit difficult to untangle so I am not sure it would be useful to drop you the files where mono is called without the rest of the program.

Let me know if there is something I can do to help!

Thanks,

Marco

From: GordianDotNet notifications@github.com Sent: Wednesday, October 9, 2019 1:06 PM To: GordianDotNet/VSMonoDebugger VSMonoDebugger@noreply.github.com Cc: Marco Alamia marco.alamia@codinglabs.net; Author author@noreply.github.com Subject: Re: [GordianDotNet/VSMonoDebugger] Add support for stack frames and expressions evaluation. This enables… (#13)

I finally had time to test the code.

Could you provide me with an embedded mono project or share a website where the creation is explained? With Mono on Linux and a simple C# project these features already exist with the current version.

I think there are certain differences to embedded mono that I don't capture with my tests. The pull request now displays many things twice for normal mono projects (for example stacktrace).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/GordianDotNet/VSMonoDebugger/pull/13?email_source=notifications&email_token=AFQVD3EFKXXRRBUGSWGDWNLQNY2R3A5CNFSM4ISVSVS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAZFAOA#issuecomment-540168248 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AFQVD3AWEBLR75VTYUOA5YDQNY2R3ANCNFSM4ISVSVSQ . https://github.com/notifications/beacon/AFQVD3HDLZHZBQV225UH5OTQNY2R3A5CNFSM4ISVSVS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAZFAOA.gif

GordianDotNet commented 5 years ago

I created a test project in Visual Studio 2017 with C/C++ and can load and debug a .Net assembly with Mono. And I also get normal stack frames and expressions evaluation. My steps:

  1. I deploy the C/C++ embedded mono file
  2. I set in the VSMonoDebugger settings that i deploy to the same folder
  3. I changed the pre-script: echo "test"
  4. I changed the debug script: (export DEBUG=1; ./EmbeddedMonoCPP.out $(TARGET_EXE_FILENAME) $(START_ARGUMENTS))
  5. I start the Mono/Deploy and Debug (SSH) OR I start the embedded Mono Application via SSH and call Mono/Attache to mono debugger
  6. As soon as C/C++ embedded Mono executes the .net code, the debugging works like under Windows.

I used this sample: test-invoke.c and added the mono_jit_parse_options method to enter the debug mode:

int
main(int argc, char* argv[]) {
    MonoDomain* domain;
    const char* file;
    int retval;

    if (argc < 2) {
        fprintf(stderr, "Please provide an assembly to load\n");
        return 1;
    }
    file = argv[1];

    std::cout << "DEBUG = " << std::getenv("DEBUG") << std::endl;

    if (std::getenv("DEBUG"))
    {
        // see https://github.com/mono/mono/blob/master/mono/mini/debugger-agent.c
        const char* opt[2] =
        {
            "--debugger-agent=address=0.0.0.0:11000,transport=dt_socket,server=y",
            "--soft-breakpoints"
        };

        mono_jit_parse_options(2, reinterpret_cast<char**>(&opt));
        fprintf(stdout, "Remote debugging enabled. Will block for debugger.\n");
    }

    mono_debug_init(MONO_DEBUG_FORMAT_MONO);

Thanks!

MACRO-255 commented 5 years ago

Intersting, maybe I am using the plugin incorrectly then?

I am afraid I don’t fully understand all your points for your steps so I will try to explain how I was using the plugin so hopefully you can help me understand if I am not using it correctly.

Context: I have a game engine and I want to develop some of the logic in C#. The engine is native code (say C++), the logic (say a function that adds 2 numbers together) is in its own DLL, C# code.

All this is done on one windows machine and I am trying to use the soft debugger in the embedded mono runtime.

1 – Run the engine

2- At this point the engine is frozen waiting for the connection to come through

3 – Open VS2019 (2017 is the same),

4 – Select the DLL as starting project

5 - Go to Extentions->Mono->Build Startup Project With MDB files

6 - Go to Extentions->Mono->Attach To Mono Debugger (without SSH)

Without the pull request I can connect and pause the soft debugger, but I don’t get watch, callstacks, breakpoints or source code. With the pull request changes I do get all those.

In the scenario I described, yes

No, I only get one, so I assume I am not triggering the second codepath you are hitting

This is all done on Win10, one machine, VS2017 or 2019, Mono version I think it’s 6.0

Without the pull request I can only break and resume. Apart from that, nothing else works.

Not sure what you mean here; if you mean to connect, just the steps above. If you mean during debugging, I mostly view variables and callstacks.

if INTERNAL_BUILD

   // "critical", "error", "warning", "message", "info", and "debug"

if MONO_DEBUGGER_ENABLED

   mono_trace_set_level_string("debug");

else

   mono_trace_set_level_string("warning");

endif

   mono_trace_set_log_handler(__logHandler, NULL);

   mono_trace_set_print_handler(__printHandler);

endif

if MONO_DEBUGGER_ENABLED

   const char* options[] = {

          "--soft-breakpoints",

          "--verbose",

          "--debugger-agent=transport=dt_socket,address=127.0.0.1:11000,server=y",

   };

   mono_jit_parse_options(sizeof(options)/sizeof(void*), (char**)options);

   mono_debug_init(MONO_DEBUG_FORMAT_MONO);

endif

   m_domain = mono_jit_init("Engine");

Later, once I load the module:

   char domainName[512];

   sprintf_s(domainName, "Domain:%s:%llX", toString(m_dllFullPath).c_str(), Time::getTickCount());

   m_domain = mono_domain_create_appdomain(domainName, nullptr);

if MONO_DEBUGGER_ENABLED

   mono_debug_domain_create(m_domain);

endif

   m_assembly = mono_domain_assembly_open(m_domain, m_dllFullPath.u8string().c_str());

Thank you for looking into this and for your help,

Marco

From: GordianDotNet notifications@github.com Sent: Monday, October 14, 2019 1:09 PM To: GordianDotNet/VSMonoDebugger VSMonoDebugger@noreply.github.com Cc: Marco Alamia marco.alamia@codinglabs.net; Author author@noreply.github.com Subject: Re: [GordianDotNet/VSMonoDebugger] Add support for stack frames and expressions evaluation. This enables… (#13)

I created a test project in Visual Studio 2017 with C/C++ and can load and debug a .Net assembly with Mono. And I also get normal stack frames and expressions evaluation. My steps:

  1. I deploy the C/C++ embedded mono file
  2. I set in the VSMonoDebugger settings that i deploy to the same folder
  3. I changed the pre-script: echo "test"
  4. I changed the debug script: (export DEBUG=1; ./EmbeddedMonoCPP.out $(TARGET_EXE_FILENAME) $(START_ARGUMENTS))
  5. I start the Mono/Deploy and Debug (SSH) OR I start the embedded Mono Application via SSH and call Mono/Attache to mono debugger
  6. As soon as C/C++ embedded Mono executes the .net code, the debugging works like under Windows.

I used this sample: test-invoke.c https://github.com/mono/mono/blob/master/samples/embed/test-invoke.c and added the mono_jit_parse_options method to enter the debug mode:

int main(int argc, char argv[]) { MonoDomain domain; const char* file; int retval;

    if (argc < 2) {
           fprintf(stderr, "Please provide an assembly to load\n");
           return 1;
    }
    file = argv[1];

    std::cout << "DEBUG = " << std::getenv("DEBUG") << std::endl;

    if (std::getenv("DEBUG"))
    {
           // see https://github.com/mono/mono/blob/master/mono/mini/debugger-agent.c
           const char* opt[2] =
           {
                   "--debugger-agent=address=0.0.0.0:11000,transport=dt_socket,server=y",
                   "--soft-breakpoints"
           };

           mono_jit_parse_options(2, reinterpret_cast<char**>(&opt));
           fprintf(stdout, "Remote debugging enabled. Will block for debugger.\n");
    }

    mono_debug_init(MONO_DEBUG_FORMAT_MONO);

Thanks!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/GordianDotNet/VSMonoDebugger/pull/13?email_source=notifications&email_token=AFQVD3HTGFGO72MWQYB7RXTQOTGWVA5CNFSM4ISVSVS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBGKG6Y#issuecomment-541893499 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AFQVD3CANEJQHCEYAPBDW3LQOTGWVANCNFSM4ISVSVSQ . https://github.com/notifications/beacon/AFQVD3E54Z7EOF5QUDP2LOLQOTGWVA5CNFSM4ISVSVS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBGKG6Y.gif

GordianDotNet commented 5 years ago

Okay, I didn't know that the plugin could also support debugging Mono under Windows :-)

I did a test project and it works without your code changes. My Steps:

  1. Install Mono 32bit for Windows Change the "Remote Host IP" to 127.0.0.1 under Mono -> Settings
  2. Select the C# DLL as starting project
  3. Go to Mono->Build Startup Project With MDB files
  4. Build the embedded Mono application (C++)
  5. Copy all files from bin/Debug/. to the embedded Mono application path.
  6. Start the embedded Mono application with enabled soft debugger.
  7. Go to Extentions->Mono->Attach To Mono Debugger (without SSH)
  8. And now I can debug with Stacktrace, Locals, Watch

If I delete the *.mdb file, I don't get any debug information anymore. Is the mdb file in the same folder as the C# dll? What's your folder structure like? Where is the C# dll in relation to the embedded Mono Application?

MACRO-255 commented 5 years ago

So, I uninstall the modified version and installed the one that is currently available online and I get back to not seeing anything ☹

VS gives me a screen with this message “The selected debug engine does not support any code executing on the current thread (e.g. only native runtime code is executing).”

I did not do step 1 though; what do you mean with install mono and change remote ip? Since it’s embedded I don’t need to have the binaries of mono, all code lives inside the app. Or am I missing something?

If it works for you though I must have something wrong in my setup. I don’t have the time right now, this weekend I will try and debug the plugin and see what it’s doing.

Can you point me to what codepath the plugin should definetly take for me to see the callstack? I can follow the execution through and see where it breaks away from the codepath.

Btw I don’t know if this is useful for you and your plugin, if not I can stick to my fork for now until I understand better how mono works? The only reason I did the pull request was to give back to you since you made the plugin in the first place, but if it’s just me using the plugin incorrectly I don’t think there is much value in fixing the pull request.

Thanks for all the help! 😊

From: GordianDotNet notifications@github.com Sent: Tuesday, October 15, 2019 1:21 PM To: GordianDotNet/VSMonoDebugger VSMonoDebugger@noreply.github.com Cc: Marco Alamia marco.alamia@codinglabs.net; Author author@noreply.github.com Subject: Re: [GordianDotNet/VSMonoDebugger] Add support for stack frames and expressions evaluation. This enables… (#13)

Okay, I didn't know that the plugin could also support debugging Mono under Windows :-)

I did a test project and it works without your code changes. My Steps:

  1. Install Mono 32bit for Windows Change the "Remote Host IP" to 127.0.0.1 under Mono -> Settings
  2. Select the C# DLL as starting project
  3. Go to Mono->Build Startup Project With MDB files
  4. Build the embedded Mono application (C++)
  5. Copy all files from bin/Debug/. to the embedded Mono application path.
  6. Start the embedded Mono application with enabled soft debugger.
  7. Go to Extentions->Mono->Attach To Mono Debugger (without SSH)
  8. And now I can debug with Stacktrace, Locals, Watch

If I delete the *.mdb file, I don't get any debug information anymore. Is the mdb file in the same folder as the C# dll? What's your folder structure like? Where is the C# dll in relation to the embedded Mono Application?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/GordianDotNet/VSMonoDebugger/pull/13?email_source=notifications&email_token=AFQVD3FMGWTPGM6ULFTSTFLQOYQ33A5CNFSM4ISVSVS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBKC4GA#issuecomment-542387736 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AFQVD3ARSL4H47BTOTICJVTQOYQ33ANCNFSM4ISVSVSQ . https://github.com/notifications/beacon/AFQVD3EWG65FH3HUGZ4I3RDQOYQ33A5CNFSM4ISVSVS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBKC4GA.gif