microsoft / vscode-mono-debug

A simple VS Code debug adapter for mono
Other
159 stars 173 forks source link

Missing file name and line number when using embedded mono #90

Closed gris-martin closed 1 year ago

gris-martin commented 1 year ago

Hi! I'm using embedded mono to call C# code from C++ and vice versa. Everything has been working fine so far, but I can't get the C# debugging to work. I'm able to attach the debugger to the mono application, and the debugger is detached when the application is closed.

I'm also able to get a break in the debugger when I trigger an exception in the C# code. The stack trace shows the correct class and method name, but lacks file name and line number:

image

I'm guessing this isn't the plugin's fault, but maybe someone can help me understand what I need to do to get that information to the debugger? Do I have to point out where the pdbs are located somehow?

I'm embedding Mono using the following mono API calls:

    static const char* options[] = {
        "--soft-breakpoints",
        "--debugger-agent=transport=dt_socket,server=y,address=127.0.0.1:55555"
    };
    mono_jit_parse_options(sizeof(options) / sizeof(char*), (char**)options);

    mono_debug_init(MONO_DEBUG_FORMAT_MONO);
    m_RootDomain = mono_jit_init("MyRuntime");

    mono_debug_domain_create(g_RootDomain);

    std::string appDomainName = "MyAppDomain";
    g_AppDomain = mono_domain_create_appdomain(appDomainName.data(), nullptr);
    mono_domain_set(g_AppDomain, true);

And then I'm loading the assembly using

        MonoImage* image = mono_image_open_from_data_full((*fileData).data(), (*fileData).size(), 1, &status, 0);
        g_MainAssembly = mono_assembly_load_from_full(image, assemblyPath.c_str(), &status, 0);
        mono_image_close(image);

I'm building my .NET assembly using csc version 3.9.0-6.21124.20 with the following options:

csc -target:library -out:CSharpScripts.dll -debug:portable *.cs

This places the .dll and .pdb in the same directory with the same name.

My VSCode launch file looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Mono",
            "request": "attach",
            "type": "mono",
            "address": "localhost",
            "port": 55555
        }
    ]
}

Help me open source community, you're my only hope.

akoeplinger commented 1 year ago

The mono_debug_init() should be enough if the .pdb is next to the assembly.

If you trigger an exception without having the debugger attached the stack trace needs to contain file+line or something is wrong. Can you also try running the assembly with mono (remember to pass the --debug flag to enable .pdb support, this is the equivalent of what mono_debug_init() does) and see if you get line numbers there?

gris-martin commented 1 year ago

@akoeplinger Thank you for your answer! I added an entrypoint to my library and ran it with

mono --debug --debugger-agent=transport=dt_socket,server=y,address=127.0.0.1:55555

And the debugger then works. I can also get it to work with my original setup if I build the symbols into the binary using the -debug:embedded option of csc, but I would ideally like to have the pdbs as a separate file.

EDIT: I'm building the mono runtime myself and can debug the sources, so if you have any tips on where to look I might be able to figure something out from there too :)

akoeplinger commented 1 year ago

Hmm that's quite weird having embedded PDBs working, so that means the debugger itself is fine it's just not finding the separate PDB for some reason.

You can try debugging mono_ppdb_load_file() in debug-mono-ppdb.c and see what's going on there.

I'm closing this for now since it's not a vscode mono debugger issue :)