srplab's blog

Discuss about multi language programming of c/c ,java,c#,python,etc. Distribute object middleware

Post sort order : index

2/28 POSTS
preview
1-3/28 POSTS

close

Writing android gui using c++(3:debug)

Jun 13, 2012 7:26 PMPublicPageviews 142 0

Introduction

This article talks about how to debug c++ codes using ndk-debug tool, and give some advice about c++ programming on android. If application is written in java, programmers can use eclipse to debug the code, set break points, and trace step by step. When written in c++, how to debug? By now, for android version above 2.2, ndk provides a ndk-debug tool, which runs on linux, can be used to debug the source code.
Debug environment may vary with platforms. The steps given here is based on environment of my host, which is windows xp, android-ndk-r6b, and cgywin is used to build c++ code for android. How to install android-ndk-r6b and cgywin are out the scope of this article. You can refer to other materials. 
The example code to be debugged is list view holder, which is given in previous article. Suppose you have already prepared the environment, and the c++ code can be built for android successfully.

changes of the project


1.    change AndroidManifest.xml to enable gdbserver
Based on document of ndk, in order to debug the native code, AndroidManifest.xml should be add a flag “android:debuggable="true" “, as follows.
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
         android:debuggable="true" >
        <activity
            android:label="@string/app_name"
            android:name=".DebugActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>
2.    recompile the project with debug flag “NDK_DEBUG=1”

3.    run the project from eclipse

debug with ndk-debug


switch to cgywin console


1.    run ndk-debug as follows

There may be many warnings. But need not care, they not affect the debug proecess.
2.    type list command, you can see the source file

use b command to set the break points.
use c command to continue run.
3.    The mobile may be response slowly, please wait, until mobile can operate normaly.
If the mobile has no response for a long time, you can type CTRL+C to exit ndk-debug and repeat the above steps.
4.    click button, then the source will be broken at the break point set before.

In cygwin console, when watch variable value, you may see “value optimized out”.
In this case, you should add –Oo flags to Android.mk file, rebuild the project and restart.

LOCAL_CFLAGS += -Wno-write-strings -O0 -DENV_ANDROID
LOCAL_CPPFLAGS += -Wno-write-strings -O0 -fexceptions -DENV_ANDROID
LOCAL_LDFLAGS += -Wno-write-strings -O0 -DENV_ANDROID


Debug on simulator may be less efficient for slow response. The better choice is to buy a cheaper mobile or palm, and debug using real device. Debug c++ code on android is not an easy work, no matter you are familiar with gdb or not. I think, migrate mature code from other platform to android is a good choice, or debug on other platform using more convenient tools other than on android directly. 

debug on win32

CLE also supports windows and linux, therefore we can debug program logic on windows. Lets  discuss in deeper. To debug on windows, there are lots of work to do, not technology, but is the work to setup stubs for android classes. The work is not started by now, so this article only gives the steps to illustrate we can debug android c++ code on windows. You can also write stubs by self following the examples in this article. The examples is also includes in the download package. For the built result of c++ code is share library, on windows platform, it can only be built into a dynamic library. So the first step is to create a loader to init environment and load it. 
Before start, cle for win32 should be installed from http://www.srplab.com/data/starcore_win32.1.5.1.exe

dynamic library loader


The loader init cle environment, import wrapandroid service description file “SRPWrapAndroidEngine.xml’, and create a service for the share library. The loader can be created with java, python, lua, c#, c++, or other languages supported by CLE. Here we create it using c++. The code is shown below. 
1.    init CLE environment
    class ClassOfBasicSRPInterface *BasicSRPInterface;
    VS_CORESIMPLECONTEXT Context;
    
// init CLE environment. The first parameter Context is used to get the return value.
    BasicSRPInterface = VSCore_InitSimpleEx(&Context,0,0,NULL,0,NULL);
    if( BasicSRPInterface == NULL ){
printf("init starcore fail\n");
return -1;
    } 
    // import wrapandroid service description file. This file contains id and name of android classe.
    {
FILE *hFile;
int FileSize;
char *xmlBufer;
hFile = fopen("SRPWrapAndroidEngine.xml","rt");
fseek(hFile,0,SEEK_END);
FileSize = ftell(hFile);
fseek(hFile,0,SEEK_SET);
xmlBufer = (char *)malloc(FileSize+1);
FileSize = fread(xmlBufer,1,FileSize,hFile);
fclose(hFile);
xmlBufer[FileSize] = 0;
if( BasicSRPInterface ->ImportServiceFromXmlBuf(xmlBufer,false) == VS_FALSE ){
printf("parse [SRPWrapAndroidEngine.xml] failed\n");
return -1;
}
free(xmlBufer);
    }
    // create a service for share library.
    BasicSRPInterface -> CreateService( "","wrapandroid", NULL, "123",5,0,0,0,0,0 );
    // obtain the service interface.
    SRPInterface = BasicSRPInterface ->GetSRPInterface("wrapandroid","root","123");
// set not check password flag for share library.
    SRPInterface ->CheckPassword(VS_FALSE);
2.    Create root activity
Share library needs a pre-created activity. The following is used to create it. And write stub code for getCurrent function of the activity. Then this function can be called from share library.
The stub function of getCurrent.
void *RootActivity;
static void *ActivityClass_getCurrent(void *Object){
    return RootActivity;
}
The code of creating root activity.
    //---get ActivityClass defined in wrapandroid service description file.
    void *ActivityClass = SRPInterface -> GetObjectEx(NULL,"ActivityClass");
    //---change to atomic object
void *AtomicActivityClass = SRPInterface ->ObjectToAtomic(ActivityClass);
//---create function stub for getCurrent
    void *AtomicActivityClassFunction_getCurrent = SRPInterface ->CreateAtomicFunctionSimple(AtomicActivityClass,"getCurrent","VS_OBJPTR getCurrent();",NULL,NULL,VS_FALSE,VS_FALSE);
//---set the function address
    SRPInterface -> SetAtomicFunction(AtomicActivityClassFunction_getCurrent,(void *)ActivityClass_getCurrent);
    
    //alloc root activity
    RootActivity = SRPInterface ->MallocObjectL(&VSOBJID_ActivityClass,0,NULL);
3.    Load share library
Load share library is simple. We call cle interface DoFile function to do this, more like script interface.
    if( SRPInterface ->DoFile("","../libcode.dll",NULL, NULL, VS_FALSE) == VS_FALSE ){
printf("load library file\n");
return -1;
    }
When compile, starlib_vcm.lib should be add to the project, as shown below:


add header files to the project:

Set break points on code.cpp, and run.

create stubs for android classes

We create stub for android classes, in order to debug or others goal. It will enable to debug android c++ codes on windows platform. How to create stub? is simple, like this,
define function
static void *ActivityClass_getCurrent(void *Object){
    …
}
create stub function
void *AtomicActivityClassFunction_getCurrent = SRPInterface ->CreateAtomicFunctionSimple(AtomicActivityClass,"getCurrent","VS_OBJPTR getCurrent();",NULL,NULL,VS_FALSE,VS_FALSE);
and assign address to the stub function
SRPInterface -> SetAtomicFunction(AtomicActivityClassFunction_getCurrent,(void *)ActivityClass_getCurrent);
If you create stubs of all function of android classes, your application can be migrated to windows.

Examples

examples can be download from 

http://wrapandroid-for-multilanguage.googlecode.com/svn/wiki/examples/cgui_debug.zip

Report abuse for this article

Copyright © 2013 Yahoo!, Inc. All rights reserved