Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use lldb to debug native libraries with Xamarin

The Xamarin debugging documentation indicates:

Use Xamarin Studio's native debugging support for debugging C# and other managed languages code and use LLDB when you need to debug C, C++ or Objective C codethat you might be linking with your Xamarin.iOS project.

However I cannot find any documentation on how to use LLDB to debug a Xamarin app. If I run my app in the iPhone Simulator and try to attach to it using LLDB I get the following error:

(lldb) attach --pid 37993
Process 37993 exited with status = -1 (0xffffffff) lost connection

error: attach failed: lost connection

Attaching using Xcode doesn't work either. I tried different variations of attach but none of them worked.

Can someone point me in the correct direction on how to debug Xamarin apps with LLDB? Moreover is this something I could do on the device and not just in the simulator? I didn't find any information on how to use LLDB to attach to a process on a device.

Update

It looks like the debugserver process is crashing whenever I use lldb to connect to my binary. Here is a link to the crash report for debugserver: https://www.dropbox.com/s/9lizhl2quj9n0cc/debugserver_2015-07-07-131423_gauss.crash?dl=0

Update 2

When I run dtruss on the app it prints the system calls till it encounters

dtrace: error on enabled probe ID 2475 (ID 194: syscall::ptrace:return): invalid user access in action #5 at DIF offset 0

which happens when something calls ptrace(PT_DENY_ATTACH, 0, 0, 0); Why is PT_DENY_ATTACH called?

Update 3

I traced the ptrace system call to this function: mono_assembly_init_with_opt which happens very early in the life of the program. All that function does is call ptrace, so if I just return early from that function, I can debug with lldb.

Basically, I can do:

(lldb) process attach --name AppName --waitfor
# when the process starts
(lldb) b mono_assembly_init_with_opt    
(lldb) c
# when the thread breaks
(lldb) thread return 0
(lldb) c

and now I can happily debug with lldb.

But, I shouldn't have to do this. Is there something wrong with my project config (I can debug simpler apps with lldb) or is Xamarin being evil?

like image 875
Kostub Deshmukh Avatar asked Jul 02 '15 17:07

Kostub Deshmukh


2 Answers

Codesigned apps on Mac OS X can only be debugged if they have a particular attribute set in their app plist. You want something that looks like:

<key>SecTaskAccess</key>
<array>
    <string>allowed</string>
    <string>debug</string>
</array>

You can look at the man page for taskgated for a somewhat terse description of this process.

Usually for Xcode projects, this attribute gets consed up and inserted into your debug builds by Xcode, so you don't need to do anything to get this to happen.

I don't know how Xamarin works, but it is possible that it is not setting this attribute. On older OS X systems, root can debug anything, so you could try sudo -s and then debugging from there. But starting with Yosemite, the request not to be debugged is being more broadly honored...

like image 127
Jim Ingham Avatar answered Sep 29 '22 11:09

Jim Ingham


Have you tried using the pid from the Activity Monitor? Just type your app name into the search box in Activity Monitor when running it in Debug.

If it still doesnt work can you try just creating a new project and attaching to that just to rule out any project config.

like image 24
Iain Smith Avatar answered Sep 29 '22 10:09

Iain Smith