> On 5 Aug 2015, at 02:12, jean-michel.perr...@csiro.au wrote:
> 
> I am trying to debug C code, mostly to step through the mono runtime itself.
I too would like to debug the Mono runtime itself and so would really, really, 
really like to able to build an MDK with debug symbols and source code 
references.

Why do I need to debug the runtime?
I have written an Obj-C > C# bridge utilising the runtime and in some 
situations I cannot figure out what the correct embedded API function 
signatures are for more complex generic managed methods. Being able to debug 
the runtime would give me some insight into what is going on in the method 
signature search process.

>  That said, I would not at all refuse a howto guide for mixed mode (C and C#) 
> debugging on Linux (not possible so far as I know). I may try to give a try 
> on Windows with Visual Studio, but as a fallback option as this issue may not 
> be reproducible on it anyway.

Here is my take on debugging Mono based on working on OS X.

C
==

On the Obj-C side (Obj-C is a strict superset of C) I should be able to step 
into the Mono runtime code via the Xcode side if my Mono runtime build 
supported debug symbols and their source code references. I reckon that most C 
IDEs should be fit to do the same.


C#
==

I don’t know of a way to seamlessly debug both C and C# via the same IDE on OS 
X (perhaps VS can so this on windows).
To debug the managed code in my OS X app I do roughly the following (more 
detail follows in the notes below).

1. Configure the OS X app embedded Mono code to use the Mono soft debugger 
(this is a remote capable software debugger that runs over TCP-IP see 
http://www.mono-project.com/docs/advanced/runtime/docs/soft-debugger/).
2. Run my OS X outside of my Xcode IDE. If I don’t do this then the signals 
generated by the debugee tend to get caught by the IDE debugger rather than the 
Mono soft debugger.
3. Fire up Xamarin Studio (XS) on a Windows machine (in my case a Windows VM on 
the same physical machine but a networked base PC should work just fine too).
4. In XS connect to the remote debugger, set breakpoints etc.
5. Exercise the OS X app and trigger breakpoints in XS.

Getting the soft debugger/Xamarin setup correctly can be fiddly but it does 
work.
It may be possible to debug the managed code by Using Xamarin Studio on OS X or 
Linux but in my case (as I was building the Managed code assemblies on the 
Windows VM anyway) it was easiest to run XS on Windows.

Debugging Mono Embedded API
===========================

The embedded managed code can be debugged (including breakpoints and single 
stepping) using Xamarin Studio.

1. Config OS X app to attach to Mono soft Debugger.

Xamarin Studio must be running on machine with IP 192.168.1.72 (in this case my 
windows VM)
 [DBManagedEnvironment setRuntimeOptions:@{@"address" : @"192.168.1.72", 
@"port" : @"10000", @"loglevel" : @"0"}];

2. Call DBManagedEnvironment +setRuntimeOptions: with IP address and port of 
Windows VM running the Xamarin Studio debugger.

+ (void)setRuntimeOptions:(NSDictionary *)options
{
    // NOTE: be sure to call this before -initWithDomainName
    
    // for info on these options see man mono
    // the debugger can be configured either as a client or a server
    NSString *address = options[@"address"]?:@"127.0.0.1";
    NSString *port = options[@"port"]?:@"10000";
    NSString *server = options[@"server"]?:@"n";
    NSString *suspend = options[@"suspend"]?:@"y";
    NSString *loglevel = options[@"loglevel"]?:@"1";
    NSString *timeout = options[@"timeout"]?:@"10";
    
    NSString *agent = [NSString 
stringWithFormat:@"--debugger-agent=transport=dt_socket,address=%@:%@,server=%@,suspend=%@,loglevel=%@,timeout=%@",
 address, port, server, suspend, loglevel,timeout];
    const char* jit_options[] = {
        "--soft-breakpoints",
        [agent UTF8String]
    };
    
    mono_jit_parse_options(2, (char**)jit_options);
    
    mono_debug_init(MONO_DEBUG_FORMAT_MONO);
}

see 
https://github.com/ThesaurusSoftware/Dubrovnik/blob/master/Framework/XCode/DBManagedEnvironment.m#L167


3. Run app outside of Xcode. The app will pause if the remote debugger is not 
responding.
4. Run Xamarin Studio (preconfigure env var to enable the soft debug menu if it 
is not visible) on the VM and load the solution being debugged on OS X.
5. Set start up project as appropriate (May be better to have a dummy exe 
project rather than rebuild this all the time).
6. Select Run - Run with - Custom command Mono soft debugger.
7. Enter IP address and port.
8. Click Listen.
9. Set breakpoints as normal.

Notes on Use of the Mono Soft Debugger
======================================

See http://www.jeffongames.com/2012/03/debugging-embedded-mono/
http://mono.1490590.n4.nabble.com/remote-debugging-a-hello-world-application-td4591791.html

The debugee should connect to the debugger on the configured listener IP and 
port.
The Windows firewall will need to allow the incoming connection.
To check if the connection is up and running;

// manual check if connection to debugger listener can be established
teqlnet 192.168.1.72 10000

// check state of established network connections 
netstat -n -f inet

For the soft debugger signal handler to work correctly the app must be executed 
outside of Xcode.
Other signal handlers, such as those installed by HockeyApp, must be disabled 
while the soft debugger is in use.
Remember to use a debug build of the managed code!

http://www.mono-project.com/docs/advanced/runtime/docs/soft-debugger/

HTH

Jonathan
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list

Reply via email to