<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>native &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<atom:link href="https://blog.sephiroth.it/tag/native/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.sephiroth.it</link>
	<description>programmer by pure chance</description>
	<lastBuildDate>Sat, 22 Oct 2022 07:28:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.sephiroth.it/wp-content/uploads/2019/03/cropped-sepy-big-32x32.png</url>
	<title>native &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<link>https://blog.sephiroth.it</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How-to debug native code with Android</title>
		<link>https://blog.sephiroth.it/2010/12/14/how-to-debug-native-code-with-android/</link>
					<comments>https://blog.sephiroth.it/2010/12/14/how-to-debug-native-code-with-android/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Tue, 14 Dec 2010 14:01:39 +0000</pubDate>
				<category><![CDATA[android]]></category>
		<category><![CDATA[general]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[native]]></category>
		<category><![CDATA[ndk]]></category>
		<category><![CDATA[sdk]]></category>
		<category><![CDATA[sequoyah]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=1416</guid>

					<description><![CDATA[This is a step by step guide for executing and debugging native code on Android with Eclipse. 1. Prerequisites The SDK version used for this guide is Froyo with the NDK r4b ( crystax release ). Also Eclipse CDT plugin it&#8217;s very useful for our purposes, so install it. Last plugin to install it&#8217;s the &#8230; <a href="https://blog.sephiroth.it/2010/12/14/how-to-debug-native-code-with-android/" class="more-link">Continue reading <span class="screen-reader-text">How-to debug native code with Android</span></a>]]></description>
										<content:encoded><![CDATA[<p>This is a step by step guide for executing and debugging native code on Android with Eclipse.</p>
<h3>1. Prerequisites</h3>
<p>The <a href="http://developer.android.com/sdk/index.html">SDK</a> version used for this guide is Froyo with the NDK r4b ( <a href="3http://www.crystax.net/android/ndk-r4.php">crystax release</a> ).<br />
Also <a href="http://www.eclipse.org/cdt/downloads.php">Eclipse CDT</a> plugin it&#8217;s very useful for our purposes, so install it.<br />
Last plugin to install it&#8217;s the <a href="http://www.eclipse.org/sequoyah/downloads/index.php">Sequoyah</a> plugin for Eclipse.</p>
<h3>2. Project setup</h3>
<p><a href="https://blog.sephiroth.it/wp-content/uploads/2010/12/Screen-shot-2010-12-13-at-11.24.20.png"><img decoding="async" class="alignleft size-thumbnail wp-image-1422" title="android project setup" src="https://blog.sephiroth.it/wp-content/uploads/2010/12/Screen-shot-2010-12-13-at-11.24.20-150x150.png" alt="" width="150" height="150"/></a>At this point let&#8217;s create a new Android project, name it &#8220;Example&#8221; and use the &#8220;com.darkwavegames.com&#8221; package name, add also an Activity name of your choice.<br />
Select Android 2.2 as base SDK version and complete the project wizard.</p>
<p>Now you need to add the native support for the newly created project. Just right click on the project root element in the package explorer and select &#8220;add native support&#8221;.<a href="https://blog.sephiroth.it/wp-content/uploads/2010/12/Screen-shot-2010-12-13-at-11.29.15.png"><img decoding="async" class="alignright size-thumbnail wp-image-1424" title="add native support" src="https://blog.sephiroth.it/wp-content/uploads/2010/12/Screen-shot-2010-12-13-at-11.29.15-150x150.png" alt="" width="150" height="150"/></a></p>
<p>In the next dialog write the path of your NDK folder and give also name for your library.<br />
After this operation a new folder &#8220;jni&#8221; will be created with a .cpp file, header filer and an Android makefile, Android.mk, which can be edited to modify all the includes, linker and compiler options. In the Android.mk file you also need to specify all the source file you want to use within the LOCAL_SRC_FILE directive.</p>
<h3>3. Debug</h3>
<p>In order to enable debug of native code in Android you have to face different problems, based also on the device and the firmware version, and if the native code is multi thread or single thread.<br />
First of all you need to mofify the <strong>AndroidManifest.xml</strong> file adding the attribute &#8220;debuggable&#8221; to true ( remember also to enable the &#8220;Debug USB&#8221; option under the Application device menu ).<br />
At this point you can debug all the java code within your eclipse debugger, for for the native C debug you need more steps.</p>
<p><span id="more-1416"></span></p>
<h3>3.1 Rooting</h3>
<p>In case one of the below prerequisites are not available you need to root the device. For most of the device you just need to download from the market the application <a href="http://www.google.com/search?ie=UTF-8&amp;oe=UTF-8&amp;sourceid=navclient&amp;gfns=1&amp;q=z4root">z4root</a>.</p>
<h3>3.2 PackageManagerService</h3>
<p>The first thing needed in order to let the gdb-debug script works is the presence of the &#8220;<em>com.android.server.PackageManagerServices</em>&#8221; service on the device. This serve generates, for each installed application, a new entry inside the &#8220;<em>/data/system/packages.list</em>&#8221; file.<br />
This file is used by the the &#8220;<strong>run-as</strong>&#8221; command to know the process privileges and its folder content.<br />
If on your device this service is unavailable you need to create one.</p>
<p><em>( Just skip this step if you have the PackageManagerService on your device )</em><br />
The file format is very simple. every line has 4 columns: package name, user id, debug flasg (&#8220;0&#8221; or &#8220;1&#8221;), applicatio folder.<br />
You can do this: from Eclipse install the application just using the &#8220;Run as&#8221; command ( or via shell writing: adb install package.apk ). Now in a new terminal window write:</p>
<p>[cc]host:~ $ adb shell ps | grep com.darkwavegames.esempio[/cc]</p>
<p>First output entry will be the user associated with that process ( let&#8217;s say app_42 ). In order to obtain the user_id simply write:</p>
<p>[cc]host:~ $ adb shell su app_42[/cc]</p>
<p>Just copy the output ( which is the user_id, for instance 10042 ) and exit from shell ( using &#8220;exit&#8221; command).<br />
Now we have to get the app folder. Unfortunately the /data/data folder is protected, so you need to log into the device and obtain root access.</p>
<p>[cc]host:~ $ adb shell su<br />
# cd /data/data<br />
# ls -l -h[/cc]</p>
<p>Look for the folder&nbsp; associated to our user ( in our example is: com.darkwavegames.esempio ).<br />
Now we have all the informations for creating an entry for the packages.list:</p>
<p>[cc]host:~ $ adb shell su<br />
# cd /data/system<br />
# echo com.darkwavegames.esempio 10042 1 /data/data/com.darkwavegames.esempio &gt; packages.list[/cc]</p>
<p>Now the run-as command will find all the required informations.<strong><br />
<em>Note</em></strong><em> that in case you uninstall and reinstall the application you need to to repeat this steps because the system could have assigned a new user_id to that application.<br />
</em>Now you should be able to run the ndk-gdb script ( executing it inside the project folder ) and set breakpoints on the native code.</p>
<h3>3.3 NDK-GDB</h3>
<p>At this point starting ndk-gdb from the project folder should be enough. If you&#8217;ve verified all the above prerequisites and your code is simgle thread this should be enough.<br />
Instead, if your native code is multi thread, the gdbserver which comes with the r4b can&#8217;t debug your code ( probably it has been compiled without the multi thread <a href="http://code.google.com/p/android/issues/detail?id=9713">support</a> ).</p>
<p>In this case you need to obtain from the android repository <strong>git://android.git.kernel.org/platform/prebuilt.git</strong> the &#8220;prebuilt&#8221; folder of the SDK. Now that you have download it into your <strong>$PREBUILT</strong> folder you need to download the gdbserver with multi thread support from <a href="http://android.git.kernel.org/?p=platform/prebuilt. git;a=blob_plain;f=android-arm/gdbserver/gdbserver;hb= d4f4703a1b23e6a73becec455c3d561ae3829a65">this link</a>.<br />
Copy the gdbserver just downoaded into folder $ANDROID NDK PATH/build/prebuilt/$HOST ARCH/arm-eabi-4.4.0/bin where $HOST_ARCH is your developement architecture ( darwin-x86 if you&#8217;re with mac osx ).<br />
Copy $PREBUILT/$HOST ARCH/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gdb into $ANDROID NDK PATH/build/prebuilt/$HOST ARCH/arm-eabi-4.4.0/bin and use it as debugger.<br />
We&#8217;re close to the goal!</p>
<p>Now modify the &#8220;install-binary.mk&#8221; script inside $ANDROID_NDK_PATH and change the line</p>
<p>[cc]$(call cmd-strip, $(PRIVATE_DST))[/cc]</p>
<p>into</p>
<p>[cc]ifneq ($(APP_OPTIM),debug)<br />
$(call cmd-strip, $(PRIVATE_DST))<br />
endif[/cc]</p>
<p>In this way, if in out Android.mk file there&#8217;s a directive APP_OPTIM with the value &#8220;debug&#8221;, the symbol strip will not be executed.</p>
<p>Now in order to have the debug running we need to add a breakpoint in the <strong>Java</strong> code just before the <strong>System.loadLibrary()</strong> method and start a new debug session from Eclipse.<br />
As the code reach the breakpoint, open the terminal, enter the project folder and write these commands:</p>
<p>[cc]host:~ $ adb shell ps | grep com.darkwavegames.esempio app_42 4512 2377 648 332 c0318ffc afd0eafc S com.d&#8230;<br />
host:~ $ adb shell<br />
$ su<br />
# cd /data/data/com.darkwavegames.esempio/lib<br />
# ./gdbserver :5039 &#8211;attach 4512[/cc]</p>
<p>Without close terminal, open a new termina window and execute:</p>
<p>[cc]host:~ $ cd workspace/esempio<br />
host:esempio $ adb forward tcp:5039 tcp:5039<br />
host:esempio $ adb pull /system/bin/app_process \<br />
obj/local/armeabi/app_process<br />
host:esempio $ adb pull /system/lib/libc.so \<br />
obj/local/armeabi/libc.so<br />
host:esempio $ export NDK_BIN=$ANDROID_NDK_ROOT/build/prebuilt<br />
host:esempio $ $NDK_BIN/darwin-x86/arm-eabi-4.4.0/bin/arm-eabi-gdb \ -x obj/local/armeabi/gdb.setup -e obj/local/armeabi/app_process[/cc]</p>
<p>gdb.setup is generated by the ndk-gdb, but is created into the libs/armeabi folder. It&#8217;s important to copy it into the &#8220;obj&#8221; folder and add the line &#8220;target remote :5039&#8221;.<br />
Now from the gdb prompt it&#8217;s possible to add breakpoints and execute all the debug <a href="http://www.eecs.umich.edu/~sugih/pointers/summary.html" target="_blank" rel="noopener">instructions</a> such as step into, over, continue&#8230;</p>
<p><strong>All these steps</strong> are included in the &#8220;<a href="https://blog.sephiroth.it/wp-content/uploads/2010/12/remotegdb.sh.zip">remotegdb.sh</a>&#8221; file, so you don&#8217;t need to write all these commands every time! Just execute this script in the project folder ( the same folder where the AndroidManifest.xml is located ).</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2010/12/14/how-to-debug-native-code-with-android/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Reading resource files from native code</title>
		<link>https://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/</link>
					<comments>https://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Sun, 24 Oct 2010 10:50:19 +0000</pubDate>
				<category><![CDATA[android]]></category>
		<category><![CDATA[assets]]></category>
		<category><![CDATA[native]]></category>
		<category><![CDATA[ndk]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[zip]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=1401</guid>

					<description><![CDATA[If you&#8217;re working with android-ndk and you want to open an asset included in your apk package, you should make a request via jni to the Resource manager in your java code. Something like: [cc] public int[] openAsset( String path ) { AssetFileDescriptor ad = null; try { ad = getResources().getAssets().openFd( path ); Integer off &#8230; <a href="https://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/" class="more-link">Continue reading <span class="screen-reader-text">Reading resource files from native code</span></a>]]></description>
										<content:encoded><![CDATA[<p>If you&#8217;re working with android-ndk and you want to open an asset included in your apk package, you should make a request via jni to the Resource manager in your java code.<br />
Something like:</p>
<p>[cc]<br />
public int[] openAsset( String path )<br />
{<br />
	AssetFileDescriptor ad = null;<br />
	try<br />
	{<br />
		ad = getResources().getAssets().openFd( path );<br />
		Integer off = (int) ad.getStartOffset();<br />
		Integer len = (int) ad.getLength();<br />
		int res[] = { off, len };<br />
		ad.close();<br />
		return res;<br />
	} catch( IOException e ) {<br />
		Log.e( TAG, e.toString() );<br />
	}<br />
	return null;<br />
}<br />
[/cc]
</p>
<p>However this is not a optimal solution expecially when you have tons of file io operations, also because I prefer to limit the amount of calls between java and C++.<br />
A better solution I&#8217;m using is to open the current apk application using the <a href="http://www.nih.at/libzip/" target="_blank">libzip</a> library. Just download the library and compile as static library, then include it in your Android.mk make file under the <strong>LOCAL_STATIC_LIBRARIES</strong> section.<br />
What you have to do first is to send the current application filename from java, once at startup, in this way:</p>
<p>[cc]<br />
PackageInfo info = null;<br />
try {<br />
	info = getContext().getPackageManager().getPackageInfo(&#8220;com.example.text&#8221;, 0);<br />
} catch( NameNotFoundException e ) {<br />
	Log.e( TAG, e.toString() );<br />
	return;<br />
}<br />
setAppName( info.applicationInfo.sourceDir );<br />
[/cc]</p>
<p>and in the C++ code you will have the corresponding <strong>setAppName</strong> method:</p>
<p>[cc]<br />
#include <zip .h><br />
#include <zipint .h><br />
#include <jni .h></p>
<p>char packageName[1024] = {0};<br />
zip	*pkg_zip;</p>
<p>JNIEXPORT void JNICALL Java_com_example_test_MySurfaceView_setAppName( JNIEnv * env, jobject obj, jstring pkgname )<br />
{<br />
	const char *buffer = env->GetStringUTFChars( pkgname, false );</p>
<p>	int error;<br />
	pkg_zip = zip_open( buffer, 0, &#038;error );<br />
	strcpy( packageName, buffer );<br />
	if( pkg_zip == NULL ){<br />
		LOGE(&#8220;Failed to open apk: %i&#8221;, error );<br />
	}<br />
	env->ReleaseStringUTFChars( pkgname, buffer );<br />
}<br />
[/cc]</jni></zipint></zip></p>
<p>Now you have a reference to the zip package. In this way now you can query the zip package asking if a particular file exists:</p>
<p>[cc]<br />
bool FileExists(const char* fname)<br />
{<br />
	if( pkg_zip != NULL )<br />
	{<br />
		int result = zip_name_locate( pkg_zip, fname, 0);<br />
		return result != -1;<br />
	}<br />
	return false;<br />
}<br />
[/cc]</p>
<p>And open any of the files included in your apk package:</p>
<p>[cc]<br />
FILE *FileOpen( const char* fname )<br />
{<br />
	zip_file *zfile = zip_fopen( pkg_zip, fname, 0 );<br />
	uint32_t offset = 0;<br />
	uint32_t length = 0;</p>
<p>	if( zfile != NULL )<br />
	{<br />
		offset = zfile->fpos;<br />
		length = zfile->bytes_left;<br />
		zip_fclose( zfile );<br />
		zfile = NULL;<br />
	} else<br />
	{<br />
		return NULL;<br />
	}</p>
<p>	FILE *fp = NULL;<br />
	fp = fopen( packageName, &#8220;rb&#8221; );<br />
	fseek( fp, offset, SEEK_SET );<br />
	return fp;<br />
}<br />
[/cc]</p>
<p>Using this method you will have a FILE pointer to the whole apk file so you have to take care of the <strong>offset</strong> and <strong>length</strong> field when reading and seeking the file itself.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
