How to debug cocos2d-x and Java code using Eclipse

In the post Developing with cocos2d-x for android on Linux I told you how to compile your cocos2d-x and run it on Eclipse.

Now I’m going to tell you how to debug both cocos2d-x native code and Java using MOTODEV Studio for Android (You can use the instructions provided here with plain Eclipse with some extra effort).

Despite the fact I’m writing with cocos2d-x in mind, what I’m going to present here will let you debug any kind of native code mixed with Android / java.

As a reference for you, bellow is my Environment:

Notes:

  1. To illustrate the process I’m going to use the cocos2d-x sample project named HelloWorld. Adjust the procedure according to reflect your own projects.
  2. <ENTER> means that you must press the keyboard’s ENTER key.

This post has 3 main parts:

  • environment: step s presented here should be performed just once no matter how many projects you’re going to develop.
  • application setup: steps presented here should be performed once for each application you’re going to develop.
  • debugging routine: steps presented here should be performed every time you’re going to start a debug session.

 

1) Environment

I assume you already have everything configured. Don’t you? Please, refer to Developing with cocos2d-x for android on Linux in order to see how to configure it.

Bellow are some sanity checks just to be sure you have everything set up.

1.1)  Be sure to have included $JAVA_HOME/bin, $ANDROID_NDK_ROOT and $ANDROID_SDK_PLATFORM_TOOLS in the PATH variable.

Type

You should see something like

Otherwise you either don’t have Java SDK installed or it’s bin folder is not in the PATH. Please, correct this before to proceed.

Type

You should see something like

Otherwise you either don’t have Android NDK installed or it’s not in the PATH. Please, correct this before to proceed.

Type

You should see something like

Otherwise you either don’t have Android SDK installed or its platform-tools folder is not in the PATH. Please, correct this before to proceed.

1.2) This step is not specified in the Developing with cocos2d-x for android on Linux.It is mandatory.

1.2.1) Go to $ANDROID_NDK_ROOT and create a copy of ndk-gdb script. Name it ndk-gdb-eclipse:

1.2.2) Open ndk-gdb-eclipse and delete its last line ($GDBCLIENT -x native_path $GDBSETUP).

original ndk-gdb-eclipse:

ndk-gdb-eclipse after you’ve removed its last line:

gdb will be called by Eclipse, so we need to remove this call from the ndk-gdb script. We are working in a copy of this file just to play safe.

2) Application setup

2.1) Import your project into eclipse

If you don’t know how to import a cocos2d-x project into Eclipse, please refer to Developing with cocos2d-x for android on Linux.

2.2) Mixing Java and C/C++ project

Taking in account you’re doing a native development (read: C/C++) for Android (Java) you can’t have neither a pure Java project nor a pure C/C++ project. You need a mixed Java / C/C++ project.

To get it on Eclipse you need to convert an existing Java project into an C/C++ project.

Left click over the project name (HelloWorld in my case) and select Create->new ->Other… as shown in picture 1.

Picture 1 – 1st step to mix Java and C/C++

It will open a window. Select Convert to a C/C++ project and click in the Next button as shown in picture 2.

Picture 2 – 2nd step to mix Java and C/C++

Another window will open.

Do the following:

  • In Candidates for conversion select your application (HelloWorld in my case).
  • In Convert to C or C++ select the radio button C++ project.
  • In Project type select Makefile project.
  • In Toolchains select Android GCC.

After that click in the Finish button. See picture 3.

Picture 3 – 3rd step to mix Java and C/C++

2.3) Adjusting AndroidManifest.xml

In order to be able to debug you program you need to:

  • set android:debuggable=”true
  • set android:targetSdkVersion=”9″

Note: targetSDKVersion can be any value >= 9. Just remember that you will both Android SDK and emulators that are compatible with the value you choose. I’ve selected targetSdkVersion=”10″.

Do a double click over AndroidManifest.xml in order to edit it. See picture 4.

Picture 4 – Opening AndroidManifest.xml

Select Manifest tab then click over Uses SDK then set the target Sdk Version. See picture 5.

Picture 5 – Setting target SDK Version.

Select Application tab then fill debuggable field with true as shown in picture 6. Save the file.

Picture 6 – Setting application to debuggable.

2.4) Build you project

It’s done in two steps. First you build the native code of cocos2d-x project as explained in the section Building the cocos2d’s HelloWorld sample for Android in the post Developing with cocos2d-x for android on Linux.

If you’ve already done it you can safely skip it. Otherwise do it.

Note: This step is needed because the build script not only compiles the source code but it also copies files from cocos2d-x Resource folder into assets folder.

Once you have all files from Resource folder into assets folder you can compile your native code directly from Eclipse by click in  Project->Clean. Be sure that Build automatically is selected. This will compile cocos2d library,  the project library (libhelloworld.so in my case), a bunch of other files, create some directories and move the created files to them. It will take a while. See picture 7.

Picture 7 – Building the project from the second time on.

Note: if your application crashes immediately after it started check whether there are any file in the android/assets folder. If this folder is empty you probably didn’t build the native code of cocos2d-x project as explained in the section Building the cocos2d’s HelloWorld sample for Android in the post Developing with cocos2d-x for android on Linux.

2.5) Running ndk-gdb

Inside Eclipse / MOTODEV Studio for Android execute you project in debug mode. Click over its name then click in the green bug in the toolbar.

The first time it will open a window asking you how to debug the project. Chose either Android Application (if you’re using Eclipse) or  Android Application using MOTODEV Studio (if you’re using MOTODEV Studio). See picture 8.

Picture 8 – First time your application is debugged

Note: I don’t know why but always you perform a Debug As or Run  As command for the first time Eclipse / MOTODEV Studio will use the first emulator you’ve added to it. If the IDE complains it cannot run the application / game because API level problem open your debug configuration and chose an emulator that has API greater than or equal to API 9.

Once the application is up and running, open the terminal, go to  <PATH TO COCOS2DX_ROOT>/<PROJECT NAME>/android directory and run ndk-gdb script. In my case, it is

You must see something like

Type

and then

to quit gdb and return to the command prompt.

Edited: If you’re using android-ndk-r7 (or newer versions) and you see the error message

ERROR: ndk-gdb requires a target device running Android 2.2 (API level 8) or higher.
The target device is running API level 7!

your emulator don’t have the minimum API required. You must install an emulator that runs at least the API informed in the error message (API 8 in the sample above)

It will generate, among other files, app_process, gdb.setup and libc.so files in obj/local/armeabi/ subdirectory of our project. It means

Those files will be needed in later steps.

In my case it means

I don’t know why but sometimes with no reason I get some errors when I try to debug (it doesn’t matter whether it’s pure Java application). In this situation I do this: I stop the emulator, start it again, wait until it is up and running then I click in Debug As again.

Notes:

  1. Whenever you stop the native debugger (gdb), application quits.
  2. Bellow are some error messages you can come across and how to solve them.

Error:

Reason: You project is NOT running in debug mode in the emulator. Run it in debug mode as show above.

Error:

Reason: You’re NOT under <PATH TO COCOS2DX_ROOT>/<PROJECT NAME>/android directory. Go there and run ndk-gdb again.

2.6) Creating GDB command file (gdb2.setup)

Go to

and create a copy of gdb.setup named gdb2.setup.

Open gdb2.setup and remove the command

It is in the last line. It’s needed because Eclipse will run this command internally and it will conflict with the script. Taking in account that gdb.setup is generated over and over again by ndk scripts we can’t modify it directly but we need to use a copy instead.

2.7) Creating a C/C++ debug configuration

The following steps will guide you in order to create a debug configuration for you native code. Once you got this configuration, you’ll be able to debug native code inside Eclipse / MOTODEV Studio for Android.

  • Click in the Debug symbol then click in the as show in picture 9.

Picture 9 – 1st step to create native debugging configuration

  • In Debug Configurations window select C/C++ Application and press New button as advised on the right pane. See picture 10.

Picture 10 – 2nd step to create debugging configuration

  • The window will show a bunch of options. In its bottom there is a link Select other. Click in this link (see picture 11).

Picture 11 – Link in which you must click

  • A windows will pop up. Mark the Use configuration specific settings checkbox and then select Standard Create Process Launcher option as shown in picture 12.

Picture 12 –  Selecting the correct gdb launcher

  • In the Name field enter whatever text you wish. I like to name cocos<MY PROJECT NAME> to ease identification. In my case I named it cocosHelloWorld.
  • Click in the Browse button near Project: field in the Main tab. It will pop up a window with all open projects . Select your native project (in my case, HelloWorld) and click in the OK button.
  • The field C/C++ Application: in Main tab needs to be filled with full path to app_process (generated in the step 2.5). Use the browse button to navigate to <PATH TO COCOS2DX_ROOT>/<PROJECT NAME>/android directory and then select app_process.
    In my case, the full path is /home/plicatibu/local/sdk/cocos2d/HelloWorld/android/obj/local/armeabi/app_process.
  • I like to let build configurations as active and I like to enable auto build in order to let Eclipse build the project before launch whenever it’s needed. You can play with these options as you wish and see what fits better for you. Click in the Apply button.

Note: If you try to fill C/C++ Application: before you’ve set Project: Eclipse will complain with the error message Project must first be entered before broswing for a program.

You can see how Main tab it was set by me in picture 13.

Picture 13 – Main tab is configured

  • Click in the Debugger tab.
  • In the Debugger: combobox select gdbserver. See picture 14.

Picture 14 – Selecting gdbserver in Debugger combobox

  • In the GDB Debugger: you need to insert the full path to gdb binary provided by Android NDK. It’s located at

    In my case, it’s located at
  • In the GDB Command File: you must insert the full path to file gdb2.setup We’ve created in the step 2.6. It should be

    In my case, it is at
  • Mark the Verbose console mode checkbox.
  • Set the initial break point. Android projects doesn’t have a traditional main function. As I want start debugging my own code I will chose the first function to be called by my program.
    This can vary a bit but usually you will pick a function from a file inside

    I chose Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInitfunction from

    See picture 15 to have an idea how Debugger tab will look like.

Picture 15 – Debug Tab properly configured

  • Still in the Debugger Tab click in Connection.
  • Select TCP in the Type: combobox.
  • Set Port Number to 5039.
  • Click in the Apply and Close buttons.

You can see how Connection configuration will look like in picture 16.

Picture 16 – Connection properly con figured in the Debugger tab.

Finally we finished configuring the application in order to be able to debug our native code using Eclipse.

3) Debugging routine

Steps presented here must be repeated every time you wish to debug your application.

  • Set a break point int the Java source code. This break point must be put somewhere after System.loadLibrary()has been executed. It’s needed to assure that all native code libraries have been loaded.I recommend you to put a break point in the first line of onCreate() funtion in the main Activity. In my case, as I’m using the HelloWorld sample, the break point was set in the onCreation() function that belongs to ApplicationDemo activity, as showin in picture 17.

Picture 17 – Break point in Java code.

Start the application in debug mode. It will hang in the line set in the step above. Picture 18 shows application stopped at break point.

Picture 18 – Application paused at break point

  • Open terminal and go to

and type

In my case, I would type ndk-gdb-eclipse command in the directory

Once you hit ENTER key you should immediately come back to command prompt without any kind of message. At this point gdb server is up and running just waiting for connections.

  • In Eclipse, goto Debug Configurations (see picture 9 above). It will open the Debug configurations window. Select the C/C++ debug configuration we created in 2.7 (I called it cocosHelloWord) and click in the Debug button. See picture 19.

Picture 19 – Debug configurations window

After  you’ve clicked in the Debug button, Eclipse will connect into gdb server. If everything went fine you should see something like in picture 20.

Picture 20 – Eclipse successfully connected into gdb server

  • Resume application execution by clicking in F8 key or by clicking in the resume button. the application will run and stop in the C/C++ breakpoint, as show in picture 21.

Picture 21 – We hit breakpoint in the native code

From now on you can debug your native code using Eclipse as if it were pure C/C++: you can both see and set variable values, set new break points and so on.

In picture 22 you can see that I’m inspecting local variable w with mouse over it.

Picture 22 – Inspecting local variable w

Below are some problems that you may encounter and possible solutions.

Error:

Reason: You’re NOT under <PATH TO COCOS2DX_ROOT>/<PROJECT NAME>/android directory. Go there and run ndk-gdb-eclipse again.

Error:

Reason: You didn’t removed the line target remote :5039 from file gdb2.setup. Remove it and start again.

Let me warn you that debug section is a little slow. despite that it works like a charm.

Once you’re interested in cocos2d-x I think you will like the book Learning Cocos2D: A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk.

Disclaimer: Please note that this article is heavily based on posts Using Eclipse for Android C/C++ Debugging, by MartinH and A step-by-step guide for debugging native code, by Carlos SoutoH. They deserve all kudos.

Please note that the link for the book Learning Cocos2D: A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk in this post is an affiliate link and I do earn a commission through any purchases that you make.
If you do make a purchase, I appreciate it greatly! If you wish, shoot me an email or let me know on the Facebook Page so I can thank you personally.
Rest assured that I do not recommend a product / service that I don’t trust and that I don’t use.

Please, try my games, play free on-line games on my site, tweet this post url and share it on facebook, google+ and other social medias.

This entry was posted in android, cocos2d, games and tagged , , , , , , . Bookmark the permalink.
  • Awesome guide, thank you so much! But my, they don’t make it easy, do they?

    • Hi. Yes, its quit a bunch of work.

      I really hope same day it will be easier do do native development on Android.

      Thanks.

  • Peter

    Great Article, Thanks!
    But how can i include the other .cpp files? I can only find the main.cpp file in the eclipse project.

    • Hi. To say the truth I still didn’t developed any line of code on my own. My first concern was to be able to compile and debug the native code.

      As usual in C development you will put them at some place you wish and adjust Makefile scritpt to let compiler and linker find them.

      I strongly recommend you to read gameit.ro  (http://gameit.ro/) .

      I don’t know the guy who writes there but I’m a big fan of him. He has many cocos2d-x tutorials based in the great Ray Wenderlich’s tutorials (http://www.raywenderlich.com/)

      I think the guy from gameit.ro can explain it too you.

      Thanks.

      • Peter

        it works!

        Goto “Java Build Path” in project properties.
        Select “Link Source…”
        Choose cpp src folder an name it like “src_cpp”
        set a breakpoint in a new cpp file
        done!

  • Hello! Thanks for the great tutorial. I’ve been waiting for some great guy figure this out!
    I almost successfully followed your tutorial, however, I ran into this problem on attached image at last stage. It seems that gdb message is streamed to eclipse console and the only problem is gdb cannot find appropriate source file like main.cpp. I am wondering if I have to add all the source files (cocos2d-x and game sources) into eclipse project. These files are usually under Xcode Project template and I have separate android project.
     I tried to add those *.cpp files on eclipse project, but all failed. Could you give me more detail on how all the source files are integrated into eclipse project for GUI break point debugging? 

    Thank your very much.

    • Hi, YoungJae Kwon.

      Thanks for your comments.

      I don’t have a Mac so I’m unable to test a project the mixes both iPhone and Android.

      Anyway I think you’re missing some point. Following are some guesses and comments:

      – You can see in picture 15 that there is a checkbox “On startup at:” There you should put he name you the function you want to break.  Please check whether you’ve set it correctly.

      – main.cpp is under //android/jni/.

      For instance, in my case, full path to main.cpp is /home/plicatibu/local/sdk/cocos2d/HelloWorld/android/jni/helloworld/main.cpp

      – You should not need to add main.cpp manually. It’s added by makefile script //android/jni/Android.mk.

      – There is a tutorial that teaches how to integrate iphone and android projects written by clawoo: http://gameit.ro/2011/08/creating-an-iphone-and-android-cocos2d-x-hybrid-project/

      Once you’ve made integration you can adapt my tutorial to enable debugging.

      Please, let me know whether that helped and what solution you found out.

      Regards.

      • Yeah I already set up the makefile for my project, and all is well except debugging.  So I couldn’t find the reason why the gdb cannot find starting function in main.cpp. I checked the point you mentioned, and I found no problem on my path setup. 
         And I am also wondering that how you can set break point under Eclipse GUI if the project doesn’t include any source files. Do you set the break point by using gdb command line or  you just set by  double click on the line in Eclipse window?Anyway, I really appreciate your help. Thanks:D

        • Hi.

          Sorry for the long delay to answer.

          Please, pay attention to these points:

          1) You should copy
          <PATH_TO_COCOS2DX_ROOT>/<PROJECT_NAME>/android/obj/local/armeabi/gdb.setup

          into

          <PATH_TO_COCOS2DX_ROOT>/<PROJECT_NAME>/android/obj/local/armeabi/gdb2.setup

          And then you should remove line

          target remote :5039

          from gdb2.setup

          2) You must start Java debug before you start native debugging. Otherwise native debug won’t work

          Put the break point into Java code anywhere in the onCreate() function (any place is okay IF your libraries were already loaded

          3)Once java debugging session is running and it has stopped at a java function then open terminal and go to

          <PATH_TO_COCOS2DX_ROOT>/<PROJECT_NAME>/android

          and type

          ngk-gdb-eclipse

          4) Now you can start the native debug.
          If everything go fine gdb will stop at the beginning of the function you’ve configured in the setup step.

          Once gdb is stopped in the function you’ve setup you pu your break points as you would do normally.

          I’m able to debug my native code without any problem. Only When I forget to perform any step it doesn’t work.Please review everything, especially step 3) Debugging

          Regards.

      • I made it! The problem was gdb.setup file I copied as you instructed. 
        Two paths have gdb.setup
        1. PROJECT_NAME/android/libs/armeabi
        2. PROJECT_NAME/android/obj/local/armeabi

        I don’t know why ndk or COCOS2D-x project build script respectively create each gdb.setup file on separated diectory. Anyway the gdb.setup on 1 is updated everytime I modified my Android.mk and build. But the gdb.setup on 2 is not updated since the project is created.

        You instructed to copy gdb.setup from 2. That caused problem for me.
        I copied and paste updated directory info from 1 into gdb2.setup.

        And everything works well now. Thanks again!

  • Pingback: please help with linux with redirecting errors and output? | Froggy IT Solutions()

  • Pingback: Wall Decals – 2,960 Ideas For Decorating With Wall Decals()

  • Pingback: Developing with cocos2d-x for android on Linux | Plicatibu Software Development()

  • Brian

    Hi! First of all, thanks for this detailed tutorial. I really appreciate your effort. I have a question though. When I try to convert the project to C/C++ in Eclipse, I don’t see Android GCC as an option. Do you know what I’m possibly missing?

  • Alexey Slepov

    Please, help to solve the problem with 
    ‘Program “ndk-build” is not found in PATH’ error I’ve got on step 2.4 after clean project
    My $PATH is
    [email protected]:~$ echo $PATH
    /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/alexey/app-dev/jdk:/home/alexey/app-dev/jdk/jre/bin:/home/alexey/app-dev/android-sdk-linux/platform-tools/:/home/alexey/app-dev/android-ndk-r7/:/home/alexey/app-dev/android-ndk-r7/ndk-build:ndk-build

    Line in terminal ‘ndk-build -h’ works as it should be i.e. gives instructions for using ndk-build. 
    What have I missed?

    OS is Ubuntu 11.10 64bit.

    • Hi, Alexey.

      I think something is wrong with your PATH variable. You’ve put the following paths in it:

      /home/alexey/app-dev/android-ndk-r7/ndk-build
      and
      ndk-build 

      but they are not paths to directories.

      You should remove them. 

      The only path you need in your PATH variable should be 

      /home/alexey/app-dev/android-ndk-r7 (assuming that you android ndk is installed there).

      Other point you should pay attention is that in this tutorial I assume your android ndk is installed in android-ndk but you’re using android-ndk-r7. 

      You should use a soft link named android-ndk that points to android-ndk-r7 and update your PATH to use the soft link (/home/alexey/app-dev/android-ndk) instead of real path /home/alexey/app-dev/android-ndk-r7.

      Once you’ve set your PATH environment variable please log out and log in again.

      Please, read the tutorial Developing with cocos2d-x for android on Linux to configure your environment correctly prior to follow this one.

      Regards.

      • Alexey Slepov

        I still have the same problem. Right after Clean operation in Eclipse Console it says
        Cannot run program “ndk-build”: Unknown reasonError: Program “ndk-build” is not found in PATHPATH=[/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games]**** Build Finished ****

        When I type ‘echo $PATH’ in terminal it says
        [email protected]:~$ echo $PATH/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/jdk1.6.0_30/bin:/home/alexey/app-dev/android-ndk:/home/alexey/app-dev/android-sdk:/home/alexey/app-dev/android-sdk/platform-tools:/home/alexey/app-dev/cocos2dx:/home/alexey/workspace

        PATH is setted in /etc/bash.bashrc file as
        export JAVA_HOME=/opt/jdk1.6.0_30export ANDROID_NDK_ROOT=$HOME”/app-dev/android-ndk”export ANDROID_SDK_ROOT=$HOME”/app-dev/android-sdk”export ANDROID_SDK_PLATFORM_TOOLS=$ANDROID_SDK_ROOT”/platform-tools”export COCOS2DX_ROOT=$HOME”/app-dev/cocos2dx”export WORKSPACE=$HOME”/workspace”PATH=$PATH:$JAVA_HOME/bin:$ANDROID_NDK_ROOT:$ANDROID_SDK_ROOT:$ANDROID_SDK_PLATFORM_TOOLS:$COCOS2DX_ROOT:$WORKSPACE

        • Hi, Alexey.
          What you reported is a little strange.

          When an environment variable (like PATH) is updated, created or destroyed no program sees modifications until user logs off and then logs in again.

          Looking to PATH value reported by eclipse and the one you’ve set seems that you’ve updated your PATH and was using eclipse without the log off / log in procedure.

          Was that? If that was the case everything should work fine after the first time you’ve logged of and logged in again.

          If after the log off / log in procedure you still have problems I don’t know what is happening but I’ll try to discover what is going on.

          Regards.

          • Michael

            Hi and sorry for my english.
            I had the same problem as Alexey Slepov had:
            “Right after Clean operation in Eclipse CDT Build Console it says
            **** Build of configuration Default for project C2dxHelloWorld ****

            ndk-build V=1

            Cannot run program “ndk-build”: Unknown reason
            Error: Program “ndk-build” is not found in PATH”

             The solution was the same – use the “/home/iz/opt/andrid-ndk-r7/ndk-build” instead of ndk-build. And my PATH variable in MOTODEV Eclipse is NOT THE SAME as PATH in linux Ubuntu. This is strange.My development machine:Linux Ubuntu 32bitandroid-ndk-r7cocos2d-1.0.1-x-0.11.0jdk-6u30Motodev studio for Android 3.1.If someone knows what’s going on, tell me please.

          • Hi, Michael.
            Could you please post your PATH here ? I’d like to see it prior to say something.

            Regards.

          • Michael

            It seems, that Eclipse uses it’s own PATH variable instead of system in MOTODEV for Android 3.1. Here is what I found:
            If you go to Project->Properties->C/C++ Build->Environment you can see, that PATH already defined there and it is not the same as PATH in Linux (Ubuntu). I pressed the Edit button (to the right) and replaced the contents of this Eclipse’s PATH with the needed one. Then saved changes. Pressed Project->Build all and saw, that error is gone. ndk-build is now in PATH and it works!
            I added screenshot, how this thing looked before I made changes.

          • Miachel, thanks for sharing your solution.

            It’s intriguing why on your MOTODEV Project->Properties->C/C++ Build->Environment the PATH is set. 

            On  my MOTODEV, there is nothing declared in Project->Properties->C/C++ Build->Environment.

            I will update the  tutorial explaining about this issue and the solution you’ve found out (off course, I’ll give you all credits).

            May I use the screeshoot you provided into my post?

            Regards.

          • By the way, could you make me a favor?

            Could you set your PATH on Linux and remove PATH from MOTODEV’s Project->Properties->C/C++ Build->Environment ?

            I’d like to confirm whether Eclipse will find ndk-build out this way.

            Thanks.

          • Michael Turner

            I tried to remove. When I press “delete” (on the right side) it does not delete the variable PATH in MOTODEV. It sets it to default “/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games”. “undefine” button does not make sense too. Of course, you can use screenshot. (I’ve attached better one, I think)

  • Barney

    Hi,

    I have managed to get as far as step 2.5 following your tutorial.  But when I run ndk-gdb I get the following error:

    “Could not extract package’s data directory. Are you sure thatyour installed application is debuggable?”

    I have read that I need to root my phone to fix this but can I fix it another way?

    • Hi, Barney.

      I’m sorry but I didn’t understand your problem (my English is poor. Sorry).

      At step 2.5 are you trying to connect to a real device? At this step you’re not able to connect to devices. You must complete everything up to 3.0.

      Could you please give me more details?

      Have you followed my tutorial Developing with cocos2d-x for android on Linux ? It gives you advices on how to set your environment prior to enable debug.

      Regards.

      • Barney

        Hi Marcio,

        Yes I was trying to connect to a real device.  I fixed it by changing these lines in ndk-gdb:

        old line: run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET –attach $PID &new line: run $ADB_CMD shell “(cd $DATA_DIR; lib/gdbserver +$DEBUG_SOCKET –attach $PID)”

        and

        old line: adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwdnew line: DATA_DIR=”/data/data/$PACKAGE_NAME”

        more details here:

        http://stackoverflow.com/questions/6158953/running-ndk-gdb-with-package-not-found-error-on-motorola-phone 

        Thanks for the great tutorial.

        • Barney

          old line: run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET –attach $PID &
          new line: run $ADB_CMD shell “(cd $DATA_DIR; lib/gdbserver +$DEBUG_SOCKET –attach $PID)”

          and 

          old line: adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd

          new line: DATA_DIR=”/data/data/$PACKAGE_NAME” 

          • Hi, Barney.

            Using my tutorial I was able to debug on simulator but I didn’t tried to debug on real device.

            I will use all information you kindly provided to make some tests and them I will either update this tutorial or create a new one explaining how to debug on real devices (of course, I will give you all credits).

            Thanks a lot!

          • Barney

            Hi Marcio,

            Unofortunatley I could not solve the “Remote communication error: Bad file descriptor.” error.

            I think my problem may be because I am using Samsung Galaxy S2 – if I make it work I will tell you how!

            Thanks for your help.

            Barney

          • Hi Barney.

            Sorry for the long delay to answer.

            I was trying to get a device with Android >= 2.2 to perform some tests and try to make it to work (my Xperia X8 uses Android 2.1-update1 and this version doesn’t allow native code debugging).

            Unfortunately I couldn’t get one. It seems everyone I know uses either iPhone or Nokia.

            As far as I get device with Android >= 2.2 I will try to discover how to debug native code on real devices.

            Regards.

        • Barney

          I now get the following in the Eclipse console when I try to debug:

          117-target-select remote localhost:5039&”Remote communication error: Bad file descriptor.n”117^error,msg=”Remote communication error: Bad file descriptor.”(gdb) 118-gdb-exitRemote communication error: Bad file descriptor.118^exit

          Do you know why this is?

          Thanks,
          Barney

  • Hi. Thanks a lot for the detailed instructions. It worked well for stopping at a breakpoint in the main.cpp file. However, I couldn’t breakpoint any files from cocos2dx itself.

    What I’ve done is:
    1. In project settings in C / C++ General -> Paths and Symbols -> Source Location added links to 2 more folders which are libs/cocos2dx and Classes.
    2. Put a breakpoint in, for example, CCMenu ccTouchBegan functions.

    Then Console immediately showed error:
    No line 173 in file “../libs/cocos2dx/menu_nodes/CCMenu.cpp”.
    and eclipse also marked the breakpoint as unrecognised.

    Any ideas why it doesn’t work?

    I’m just guessing that it couldn’t find debug info.

  • I’ve been playing a bit more. So it seems, that all source code is attached correctly, because I can put a breakpoints but in a few files and couldn’t work out any logic.

    So, for example, in cocos2dx folder I can put breakpoints in files: CCCamera.cpp,  cocos2d.cpp, CCAffineTransform.cpp, CCGeometry.cpp, CCObject.cpp, SimpleSoundEngline.cpp and can debug them.

    But not in CCNode.cpp, CCSet.cpp and almost any other – in all cases gdb shows error something like “No line 33 in file ../libs/cocos2dx/cocoa/CCSet.cpp”.

    I changed all relative paths in gdb2.setup to absolute, still doesn’t help. But then it shouldn’t be an issue, as some file do work.

  • Ok, after a long search on internet, it seems that gdb in the ndk r7 is screwed up (http://groups.google.com/group/android-ndk/browse_thread/thread/c7e967c342aaaf21)

    There is a solution in this thread http://groups.google.com/group/android-ndk/browse_thread/thread/ebd969a055af3196
    which is:
    “To overcome this i switched the debug info from DWARF ( default) to 
    stabs by adding -gstabs+ to LOCAL_CPPFLAGS, LOCAL_CFLAGS, 
    LOCAL_LDLIBS.”
    And it really helped and I finally can debug all my files including cocos2dx source.

    • Hi, Dmitry.

      Thanks for stopping by the blog.

      I wasn’t aware of this problem because when I wrote this post I was using NDK r6 and every thing worked fine.

      Since then I’m not having time to play with cocos2d-x. 

      When NDK r7 was released by Google I made a simple test and it seemed all stuff worked so I updated the tutorial.

      To make some tests I used HelloWorld project and the problem you reported really happended to me.

      Then I made what you suggested by adding -gstabs+ into LOCAL_CFLAGS variables in

      <PATH_TO_COCOS2D>/chipmunk/Android.mk
      <PATH_TO_COCOS2D>/cocos2dx/Android.mk

      After that I was able to put break points on CCNode.cpp but I wasn’t able to set breakpoints on /HelloWorld/Classes/HelloWorldScene.cpp.

      So I added 
      LOCAL_CFLAGS += -gstabs+

      into
      <PATH_TO_COCOS2D>/HelloWorld/Classes/Android.mk

      After that I’m not able to start native bedug anymore. It stops with the error message “Error stopping at Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit

      Do you have any idea? Could you please tell me exactly where you put -gstabs+ ?

      Thanks for your help.

      • Michael Turner

        Dmitry Matyukhin, Marcio Andrey Oliveira
        Thank’s for:
        “So I added
        LOCAL_CFLAGS += -gstabs+
        into
        {PATH_TO_COCOS2D}/HelloWorld/Classes/Android.mk”

        I had “No line 33 in file {any project C++ source file, except main.cpp}” error when tried to add breakpoint. After adding specified string I can add breakpoint and app stops on breakpont well. Thank you!

  • Regis

    Thanks for this tutorial! You write “Otherwise you either don’t have Android NDK installed or it’s not in the PATH. Please, correct this before to proceed.” What does “it’s not in the PATH” mean ? What are the steps to ‘put the Android NDK in the path’? Thanks!

    • Hi, Regis.

      Sorry. My English is poor and some (many?) times it’s hard to understand what I want to say.
      When you execute the command 

      ndk-gdb –help

      you should see the result I’ve posted:

      Usage: ndk-gdb [options] Setup a gdb debugging session for your Android NDK application.Read 28805NDK/docs/NDK-GDB.TXT for complete usage instructions….

      If you don’t see it, there are two possibilities:

      1 – you don’t have NDK installed on your machine 

      or

      2 – you have NDK installed but you forgot to set your PATH environment variable to include NDK.

      By the way if any of the above situations were true you will see 

      ndk-gdb: command not found

      Hope it helped.

  • Regis

    Please have a look to the question posted here : http://www.cocos2d-x.org/boards/6/topics/8512 Any help to solve this issue would be appreciated ! Thanks

  • Michael Turner

    Thanks for such detailed instructions!
    I had an error with PATH variable (when development IDE used it’s own PATH instead of system one), using MOTODEV for Android 3.1 (Eclipse). And I finally found solution!
    Already post it here:
    How to solve problem with Motodev Eclipse for Android 3.1: wrong PATH

  • jchanchawat

    hi, I am having this error. during debugging.

    [email protected]_14-PC /cygdrive/c/Development/cocos2d-x12/ecli/android
    $ /cygdrive/c/Development/android-ndk-r7b/ndk-gdb-eclipse
    cygwin warning:
      MS-DOS style path detected: C:Developmentandroid-ndk-r7b
      Preferred POSIX equivalent is: /cygdrive/c/Development/android-ndk-r7b
      CYGWIN environment variable option “nodosfilewarning” turns off this warning.
      Consult the user’s guide for more details about POSIX paths:
        http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
    awk: fatal: can’t open source file /cygdrive/c/Development/cocos2d-x12/ecli/android/C:Developmentandroid-ndk-r7b/build/awk/check-awk.awk' for reading (No such file or directory)
    ERROR: Could not run '/usr/bin/awk' command. Do you have it installed properly?

    shall I download gawk from:
    http://gnuwin32.sourceforge.net/packages/gawk.htm  ??
    if yes, than which download ? ("Complete package, except sources" OR "Sources")
    where do I need to setup the gawk's path ?

    thanks in advance :)

    EDIT:

    on rerunning the same code i got a different error

    [email protected]_14-PC /cygdrive/c/Development/cocos2d-x12/ecli/android
    $ /cygdrive/c/Development/android-ndk-r7b/ndk-gdb-eclipse
    awk: fatal: can't open source file
    /cygdrive/c/Development/cocos2d-x12/ecli/android/C:Developmentandroid-ndk-r7b/build/awk/check-awk.awk’ for reading (No such file or directory)
    ERROR: Could not run ‘/usr/bin/awk’ command. Do you have it installed properly?

    I visited my C:Developmentandroid-ndk-r7b/build/awk/check-awk.awk the file exists there. which path do i need to edit ?

  • jchanchawat

    hi, sorry for bothering you again.
    now, I am stuck on step  3!
    please help.
    for the sake of brevity , console log is in comments.

    • jchanchawat

      61-gdb-set confirm off
      ~”(no debugging symbols found)n”
      (no debugging symbols found)
      &”Warning: /c/Development/android-ndk-r7b/platforms/android-14/arch-arm/usr/include: No such file or directory.n”
      &”Warning: /c/Development/android-ndk-r7b/sources/cxx-stl/gnu-libstdc++/include: No such file or directory.n”
      &”Warning: /c/Development/android-ndk-r7b/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include: No such file or directory.n”
      ~”(no debugging symbols found)n”
      (no debugging symbols found)
      (gdb) 
      Warning: /c/Development/android-ndk-r7b/platforms/android-14/arch-arm/usr/include: No such file or directory.
      Warning: /c/Development/android-ndk-r7b/sources/cxx-stl/gnu-libstdc++/include: No such file or directory.
      Warning: /c/Development/android-ndk-r7b/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include: No such file or directory.
      61^done
      (gdb) 
      62-gdb-set width 0
      62^done
      (gdb) 
      63-gdb-set height 0
      63^done
      64-interpreter-exec console echo
      (gdb) 
      64^done
      (gdb) 
      65-gdb-show prompt
      65^done,value=”(gdb) ”
      (gdb) 
      66-gdb-set auto-solib-add on
      66^done
      (gdb) 
      67-gdb-set stop-on-solib-events 0
      67^done
      (gdb) 
      68-gdb-set stop-on-solib-events 1
      68^done
      (gdb) 
      69-target-select remote localhost:5039
      &”localhost:5039: No connection could be made because the target machine actively refused it.n”
      localhost:5039: No connection could be made because the target machine actively refused it.
      69^error,msg=”localhost:5039: No connection could be made because the target machine actively refused it.”
      (gdb) 
      70-gdb-exit
      70^exit

      • jchanchawat

        these error didn’t appeared in log. though file exists at C:Developmentcocos2d-x12ecliandroidobjlocalarmeabi

        • jchanchawat

          tried these commands in cygwin

          make –version
          g++ –version
          gmake –version 
          -bash: gmake: command not found

          means gmake is not installed or path is not set (g++ and make are installed). do i need it installed ? please try these commands on your system if all of them works.

  • Chris

    Hi, great tutorial, very clear thank you.

    I have a problem, when I run ndk-gdb-eclipse (or just ndk-gdb from cygwin command line) the debugger is unable to find any debugging symbols. I have done everything to make sure it is in debug mode, the –verbose output tells me it found debuggable flag, but it will not find any symbols. Only thing I can think of is the debug symbols are being stripped before being installed, but readelf shows the full set of debug symbols present. Any idea why this might happen? 

    • Hi, Chris.

      Dmitry Matyukhin found both the reason and the solution for this problem.

      Please, read his comments on this post.

      Regards.

  • Shota

    Hi there
    I followed the tutorial to a point I need to launch CDT debugger but fail

    right click Debug As->choose my c++ debug config->debug

    a error pops up, it only said

    Error creating session
    Process Terminated
    Process Terminated
    Process Terminatedhave any idea whats go wrong? :(sorry for my bad english and thank you

    • Shota

      addition: I m using motodev studio 4.0 instead of 3.0

      • Shota

        fixed: my gdb path is wrong
        I pointed it to my gdbserver

        • Hi, Shota.

          I’d ask you to review all steps, specially the paths.

          Fortunately you already found the solution.

          Regards.

  • E Bustaty

     Thanks for your great work.
    I tried the tutorial and everything went fine, but when I open the file “main.cpp” many errors appear in problems window, and then eclipse refuses to build or debug until i fix them, if i deleted them from problems window it builds fine, but they appear again when i start native debugging, it’s not very cool to delete them every time i want to start the debugger, debugging routine is already long…
    any help appreciated.
    my setup:
    Mac OS X 10.7.2
    cocos2d-1.0.1-x-0.12.0
    MOTODEV Studio for Android
    android-ndk-r7b
    android-sdk-macosx: android-10

    this is listing o the errors:
    Description    Resource    Path    Location    Type
    Symbol ‘cocos2d’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 10    Semantic Error
    Type ‘JavaVM’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 15    Semantic Error
    Type ‘jint’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 15    Semantic Error
    Function ‘setJavaVM’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 17    Semantic Error
    Symbol ‘JNI_VERSION_1_4’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 19    Semantic Error
    Type ‘jint’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 22    Semantic Error
    Type ‘jint’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 22    Semantic Error
    Type ‘JNIEnv’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 22    Semantic Error
    Type ‘jobject’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 22    Semantic Error
    Function ‘sharedDirector’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 24    Semantic Error
    Method ‘getOpenGLView’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 24    Semantic Error
    Function ‘sharedOpenGLView’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 26    Semantic Error
    Type ‘cocos2d::CCEGLView’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 26    Semantic Error
    Method ‘setFrameWidthAndHeight’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 27    Semantic Error
    Function ‘sharedDirector’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 30    Semantic Error
    Method ‘setOpenGLView’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 30    Semantic Error
    Type ‘AppDelegate’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 32    Semantic Error
    Type ‘AppDelegate’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 32    Semantic Error
    Function ‘sharedApplication’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 33    Semantic Error
    Method ‘run’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 33    Semantic Error
    Function ‘reloadAllTextures’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 37    Semantic Error
    Function ‘sharedDirector’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 38    Semantic Error
    Method ‘setGLDefaultValues’ could not be resolved    main.cpp    /MyGameName/jni/helloworld    line 38    Semantic Error

    • Hi, 
      E Bustaty.

      It seems that either you didn’t installed native support or you haven’t converted your project to C/C++ project.

      • E Bustaty

        Thanks for the reply.

        I did both, now eclipse cleans and builds just fine, I am able to debug native code now, my problem is that these errors appears after the debugging session so I have to delete them when I want to debug again, the don’t appear after build!

        • I’m glad to hear.

          I don’t understand why you’re getting errors after the debugging session.

          My question:  Do these error messages appear in the Java perspective, in the C/C++ perspective or in both?

          If  they show up in the Java perspective, you may have imported the native code to Java project.

          Are you sure that you didn’t imported the native code in the Java project?

          • E Bustaty

            They appear in both perspectives.
            I did exactly like the tutorial said: created an android project then converted it to a C/C++ project with th exact same settings described here in the tutorial.

          • I don’t know what may be causing it. I don’t have this problem.

            Have changed you Android.mk file ?

            I don’t know whether it’s a problem specific on Mac but I think you messed something up. 

            Sometimes we either do some tiny mistakes or forget to perform some step and we don’t realize it.

            I don’t promise I will solve your problem but  I will try the following:

            This weekend I will start a fresh install of Linux and then I will follow my tutorial again to try to figure out whether I made a mistake (what I don’t believe, but I will check it anyway).

            I’ll also try to reproduce this errors and if a succeed I tell you some advice.

          • E Bustaty

            thanks for the much effort your doing.
            I had a new mac today and I will try everything again from scratch.

          • Yesterday I installed cocos2d-x 1.0.1-0.12.0 , android-ndk r7c and MOTODEV 3.0 in my Ubuntu 10.10 32 bits and I had no problem.

          • E Bustaty

             I,ve solved it…
            all I needed to do is to open Preferences -> C/C++ -> Code Analysis, and uncheck everything.
            I don’t know why no one suffered this, I used MotoDev Studio with default preferences. anyway, this is the solution just in case…
            thank you Marcio for your effort.

          • Deepscorn

            I had many errors after converting to c/c++ project too. I think it is not very comfortable to develop without errors being highlighted (no c++ code analysis). I solved it in a different way. Here, this post probably will help:
            http://serpents-iz.blogspot.com/2012/04/motodev-for-android-31-no-includes.html

          • flycarl

            for one use the eclipse 4 juno. the environment variable JAVA_HOME must be set in for example /etc/profile. other wise eclipse will not get the variable, then the auto detect path will not work, I use many time to find that, because I set environment variable in .zshrc

  • TechyNUT

     Everything is setup properly with the help of this tutorial but now when I import my project as per your first tutorial and convert the project in to C/C++ suggested as above . I get error in my android.ml file at this line  $(call import-module,tests) is not imported and something like that which does not allow me to run my application in debug mode ?

    • Hi.

      You may want to say Android.mk, right?

      What version are you using?

      If  you’re using cocos2d-x 1.x I’m pretty sure you messed something up.

      Please each and every step you performed.

      cocos2d-x 2.x removed support for Linux so this tutorial won’t work anymore.

  • hello dere
    in step : 2.5) Running ndk-gdb

    i got following error

    /Users/niteshpurohit/Desktop/NDK/ndk-gdb-eclipse: line 104: make: command not foundERROR: The device does not support the application’s targetted CPU ABIs! Device supports: armeabi Package supports:
    any help,please

    • It seems you’re using Mac, right?

      I don’t have a Mac so I just tested in the Linux. Moreover, this tutorial was developed for version 1x of cocos2d-x. Version 2.x had it’s support for Linux dropped so I can’t even try it.

      That said, let me try to guess some possible causes.

      It seems your message is incomplete ( Device supports: armeabi Package supports: ???)

      My guess is that you’re trying to build to armeabi-v7a but your target version doesn’t support it. It seems it just support armeabi.

      Provide some more information. I’ll try to figure out what is happening.

      Regards.

  • Pingback: cocos2dx and debugging on android | HexedCode()

  • Pingback: debugging cocos2dx android applications on mac with eclipse | Solutions for enthusiast and professional programmers()