dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

Target _Gdb not found in project #3778

Closed m93a closed 4 months ago

m93a commented 4 years ago

Steps to Reproduce

  1. Fetch a sample project:
    git clone git@github.com:xamarin/monodroid-samples.git
    cd monodroid-samples/Button
  2. Build and deploy it
    emulator @Nexus &
    msbuild /t:Build,Install
  3. Attach GDB and run the app
    msbuild /t:_Gdb

Expected Behavior

An app connected to gdb should start up in the emulator, like described in the docs.

Actual Behavior

Build fails, saying that the target _Gdb doesn't exist.

Project "monodroid-samples/Button/DroidButton.sln" on node 1 (_Gdb target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|Any CPU".
Project "monodroid-samples/Button/DroidButton.sln" (1) is building "monodroid-samples/Button/DroidButton.csproj" (2) on node 1 (_Gdb target(s)).
monodroid-samples/Button/DroidButton.csproj : error MSB4057: The target "_Gdb" does not exist in the project.
Done Building Project "monodroid-samples/Button/DroidButton.csproj" (_Gdb target(s)) -- FAILED.
Done Building Project "monodroid-samples/Button/DroidButton.sln" (_Gdb target(s)) -- FAILED.

Build FAILED.

"monodroid-samples/Button/DroidButton.sln" (_Gdb target) (1) ->
"monodroid-samples/Button/DroidButton.csproj" (_Gdb target) (2) ->
  monodroid-samples/Button/DroidButton.csproj : error MSB4057: The target "_Gdb" does not exist in the project.

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.76

Version Information

Log File

N/A

dellis1972 commented 4 years ago

You have to pass the path to the csproj for the _Gdb target to work. This is the same for all the custom Xamarin.Android targets. When you just call msbuild it will default to building the solution and MSBuild will not find the required targets.

So try this instead.

msbuild monodroid-samples/Button/DroidButton.csproj /t:_Gdb

dellis1972 commented 4 years ago

or

msbuild DroidButton.csproj /t:_Gdb

of you are already in the monodroid-samples/Button directory.

jonathanpeppers commented 4 years ago

It looks like the _Gdb target is in Xamarin.Android.Common.Debugging.targets and that is not in the OSS core of xamarin-android.

@dellis1972 is there a reason this part couldn't be open sourced? There are some other "private-ish" targets like _Run being used, but I don't think I see anything that has to be proprietary.

dellis1972 commented 4 years ago

@jonathanpeppers generally our debugging support is close source. The _Gdb target itself depends on a bunch of things that depend on AndroidTools which is not open sourced.

brendanzagaeski commented 4 years ago

Background info

  1. I think @jonathanpeppers was getting at this, but just to make sure we're all clear, this report is about using an open source build that the reporter built and ran locally on:

    elementaryOS 5.0 (based on Ubuntu 18.04.2)

    So invoking msbuild on the .csproj file won't change the error message. If m39a runs the following command:

    msbuild monodroid-samples/Button/DroidButton.csproj /t:_Gdb

    They will still see the same error:

    error MSB4057: The target "_Gdb" does not exist in the project.

    Because the open source build does not include the _Gdb target.

  2. That said, the fact that the open source build does not include the _Gdb target is arguably a good thing. After working on the related, though primarily contributor-focused, Development tips and native debugging document earlier this year, I had the understanding that the _Gdb target was no longer maintained and should likely be removed in a future round of code cleanup.

    In particular, https://github.com/xamarin/xamarin-android/commit/d80b485bcbc6dff29dbd79aa2423f283dd62c60a removed the gdbserver binary from debug APKs and removed support for launching the gdbserver binary via the libmono-android runtime.

    So the _Gdb target doesn't work in commercial builds on macOS or Windows right now either.

    It is still possible to debug Xamarin.Android apps from the command line using GDB, but since most Xamarin.Android users are on Windows or macOS, my default recommendation for them would be to use the integrated GDB support in Visual Studio on Windows or the mono/lldb-binaries on macOS instead.

Recommendation for this GitHub issue

For this particular GitHub issue about Linux, attaching GDB on the command line would be a fine approach. Note that that contributor-focused article only mentions libmonosgen-2.0, but you would need to copy any native unmanaged library you wanted to debug to the local gdb-symbols directory.

Another approach that might be more convenient would be to use Android Studio. I'm fairly certain the Attaching LLDB using Android Studio steps also work on Linux. Those steps are nice because LLDB takes care of setting up all the native unmanaged libraries automatically (at the cost of having to wait for it to download them from the device on the first debugging session) and you get a graphical UI for stepping through the native unmanaged source files.

m93a commented 4 years ago

Thank you all for your responses, I will give LLDB a try! However, since I came here from the specs (I've never even used GDB before), I think that it's advisable to add some deprecation warning there. Maybe even point them to the LLDB tutorial. Than I'll happilly close the issue :)

jpobst commented 4 months ago

Closing, as this seems expected with an OSS build.