How do you debug in Cocos2d-x?

I would like to debug the C++ on my Android game in the Eclipse IDE, but I haven’t found any sites that had successfully taught me how to do this.

please, add more context.
what do you want to debug? what target platform? and what host platform are you using?

whoops, I’ve edited my posting.

Have you tried with Android Studio?
I compile using VS, then when I upload my apk on device I open Android Studio and with logcat I can debug.

I can read from the logcat with Eclipse as well. I’m trying to actually use a debugger to set breakpoints and see where it crashes. Doing system print outs isn’t enough because there’s bad memory allocation and ndk-stack is not good for tracing either. It shows where the error occurs while it is deep inside a cocos function so it’s not very helpful to me.

Ah ok, sorry for breakpoints I actually use VS2013, but compiling for win32.
But it’s not the same thing if the memory allocation problem is android(ndk) related.

If I’m able to compile (and then debug) with android studio on android I’ll let you know… (hard try :stuck_out_tongue:)

I have used the Tegra Android Development Eclipse based solution from NVDIA with breakpoint and Logcat to debug cocos2d-x Android C++ and Java code simultaneously. Works great to isolate those Android specific issues that cannot be caught when developing/testing in Xcode for iOS/Mac or Visual Studio WP8.

This post talks about a similar NVIDIA Visual Studio breakpoint enabled NDK debugging solution for Android called Nsight. But it looks like you need to buy Visual Studio Professional 2013 to use Nsight. Debugging Android Cocos2dx-3.0(rc2) with Visual Studio and Tegra Nsight 1.5

Android NDK development has always been a thorn in my side. I wish google would put out and IDE like VS or Xcode that supports C++. It is kind of shocking to me that Google stopped supporting their ADT complete IDE and left just the eclipse plugin before providing Android Studio with NDK support :open_mouth:.

Tegra Android and the NVIDIA Debug Manager for Android NDK (breakpoints and all) is free, you just have to sign up for their Gameworks free membership: https://developer.nvidia.com/nvidia-debug-manager-android-ndk

You can get just the NVIDIA Debug Manager for Android NDK eclipse plugin or their entire NDK Eclipse Android IDE. If you use their NDK, be sure to double check the NDK version compatibility.

Here are steps on how to do so that I just finished testing.

1) Signup for NVDIA Registered Developer Program

2) Register for the NVDIA GameWorks so that you can download the Android Tegra Developer Tools

3) Download and Install the Android Tegra Developer Tools.

4) Download Cocos2d-x 3.4 (this process does not work in 3.5+).

5) Unzip Cocos2d-x 3.4 into the location where you installed NVDIA’s tools (or any other desired location and note the location). Default location for the NVPACK is your home directory in the folder titled NVPACK. So in that case, unzip the Cocos2d-x 3.4 zip file inside of the NVPACK folder; the result will be the new folder cocos2d-x-3.4 inside of the NVPACK folder.

6) Run setup.py Run the cocos script $ cd cocos2d-x $ ./setup.py

7) Read the output of the setup.py script. It is important that all variables are set properly. If any are not set, you will need to set these manually. If you followed the steps above, using the versions described above, then you should not need to set any variables manually. The key here is to ensure that your environment variables point to the desired Ant, cocos2d-x root, NDK root, Android SDK, etc.

8) Execute cocos new SampleOne -p com.heyalda.sampleOne -d /Your/Project/Path -l cpp.

9) Open Eclipse and enter a desired working directory.

10) Right-click in the left panel and select Import. Then select the General folder and select Existing Projects into Workspace. Select Browse and then navigate to the newly created game titled SampleOne and in the folder cocos/platform/android/java and then select open.

11) You might see errors after importing regarding the missing Android Library API 10. Disregard those errors for now.

12) From the top title bar navigation menu in eclipse, select Window -> Android SDK Manager.
Deselect Installed in the SDK Manager window and ensure that Updates/New is selected.
Select the Android 3.2 (API 13), or whichever Android version levels you want.

13) Install the API 13 (or whichever android levels you want).

14) After installation completes, close Eclipse.

15) Change directories into the newly created game and type cocos run -p android. This will build the cocos2d-x library and the newly created android project. If you connect and Android device to your computer, after the build succeeds the app will be installed not he Android device and launched. Depending on the power of your computer, this build may take anywhere from a few minutes or longer.

16) Define the Android SDK Root for your project by right clicking on your project, select properties and then C/C++ Build -> Environment and then add the Variable ANDROID_SDK_ROOT that points to your SDK root. Click the Apply to All Configurations to apply it to Release and Debug configurations.

17) Download and install ndk-r9d since there are issue with r10d and cocos2d-x compatibility. Be sure to update your .bash_profile and eclipse NDK_ROOT settings to point to ndk-r9d. If you get a bunch of srand and rand build errors, then you are probably not pointing your environment at ndk-r9d. https://developer.android.com/tools/sdk/ndk/index.html

18) If you get the error similar to this:
Android NDK: WARNING: APP_PLATFORM android-21 is larger than android:minSdkVersion 13 in ./AndroidManifest.xml
Then open the file NVPACK/android-ndk-r10d/build/core/add-application.mk

Search for the text $(call __ndk_info,WARNING: APP_PLATFORM $(APP_PLATFORM) is larger than android:minS
Or if you are using ndk-r9d, then search for the same string in the /build/core/add-application.mk file.
Then comment out that line by adding a # to the first character of the line.
This is a hack to get the app to launch. Probably should not leave this setting permanently.

19) If you get the “Dex Loader - Unable to execute dex: Multiple dex files” if I remember correctly, you may need to comment out the cocos2d-x reference in project.properties temporarily. An alternative is that you could remove the cocos2d-x source code from your java build path, but that has implications to that may not be desired.

20) Right-Click on the project name and then in the context menu select Debug As -> Android Native Application. The Eclipse perspective should switch to the debug perspective. You can then change back and forth between the C++ and Debug perspectives in Eclipse using Window->Open Perspective

21) In the C++ perspective, you can open C++ files in the Classes folder and then double-click on the left side of a source line to set a breakpoint.

I added some pictures of the NVIDIA Tegra Eclipse IDE in a blog post if you are curious how it looks: http://heyalda.com/how-to-debug-cocos2d-x-native-code-on-android-in-eclipse/

This process works ok in 3.4, but no longer works in version 3.5 or 3.6. I originally wrote that I was using 3.5, but was actually using the 3.4 build_native.py and the 3.5 framework.
Version 3.5+ updated the build_native.py to use the cocos console and has a .externalToolBuilders folder that probably is doing something different in Eclipse… not sure.

I created a new 3.6 HelloWorld project.
When I Debug As -> Android Native Application it changes to the debug perspective, but does not stop on breakpoints anymore.
Eclipse also no longer builds c++ changes with the new build_native.py.
Haven’t looked into why that is yet, but probably will soon.

first you need to get your Android.mk file settings right. The following works for Mac. You will need to change the “gdwarf-2” to whatever debug symbols your machine uses.

APP_CPPFLAGS += -DCOCOS2D_DEBUG=1 -O0 -g -gdwarf-2

Also there is a bug in build_native.py that has been in there for ages which stops the NDK from going into debug mode:

if ndk_build_param == None:
command = ‘%s -j%d -C %s NDK_DEBUG=%d’ % (ndk_path, num_of_cpu, app_android_root, int(build_mode==‘debug’))
else:
command = ‘%s -j%d -C %s NDK_DEBUG=%d %s’ % (ndk_path, num_of_cpu, app_android_root, int(build_mode==‘debug’), ’ '.join(str(e) for e in ndk_build_param))

Oh thanks! By the way, is there any approach for Cocos2dx-3.0 to 3.3?

The approach that I listed above should work on 3.x up to 3.4. I have also used this approach on 2.x successfully.