'Debugging'에 해당되는 글 2건

  1. 2013.09.10 Debugging Arm application with QEMU
  2. 2012.06.09 Tips in developing with NDK

QEMU can be very useful tool in developing wide range of software from simple application to even linux kernel itself for arm based embedded system. Here is to show how developing and debugging simple application for virtual arm based platform with QEMU is actually easy to people who are lazy so hate to move his/her finger to turn on real board and for poor people who do not have any real hardware.


Note: This post assume Host system is fairly new Ubuntu machine(For my case, its ubuntu 12.04 64bit)


Below is list for bullet items which this post focus.


1. How to build simple c application with a assembly library which can be callable in c function.

2. How to run the executable in Qemu

3. Debugging it with gdb



Phase 0. TOOL Setup


Before delving into programming, we need to check all necessary tools ready. Please refer to below command to install all of them. 


First install linaro image tools.


host$ sudo apt-get install linaro-image-tools


To get qemu to work with, there are two ways to building from source code and using apt-get but here I used apt-get as like below.


host$ sudo add-apt-repository ppa:linaro-maintainers/tools
host$ sudo apt-get update
host$ sudo apt-get install qemu-user-static qemu-system

To be able to compile c and assembly, we need to install crosstool chain for arm.


host$ sudo apt-get install gcc-arm-linux-gnueabi


Phase 1. Creating a simple application made up of c and assembly combination 


I wrote some articles in this site how to create CMake project so here I would like to show only source code without detailed explanation about it and why it is needed.


There are 4 files required in the project. 

1. main.c                                          # main c source code including main()

2. square.s                                       # assembly file containing functions callable in main() 

3. CMakeLists.txt                               # main cmake project file

4. toolchain-for-qemuarm.cmake          # cmake module used for cross compilation for arm target system


So just create below all 4 files in a $(EXAMPLE) directory.


main.c


square.s


CMakeLists.txt



toolchain-for-qemuarm.cmake


Note: Here I used gcc toolchain from Freescale site but you may use other popular toolchain also.


One you have those file, it is easy to build binary which will be run on qemu.


host$ mkdir build; cd build

host$ cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain-for-qemuarm.cmake ..


Now you can check arm executable created correctly

host$ arm-linux-gnueabi-readelf ./program -a | more

ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: ARM Version: 0x1 Entry point address: 0x8538 Start of program headers: 52 (bytes into file) Start of section headers: 3592 (bytes into file) Flags: 0x5000002, has entry point, Version5 EABI

For source codes, download this. 

cassemtest.tar.gz



Phase 2. Test with QEmu

The built program depends on external libary called ld-linux.so.3 and this is arm side shared libary. So qemu need to know where to find correct shared library for this arm friendly executable. In below, I informed /usr/arm-linux-gnueabi which was created when I installed cross-toolchain mentioned in phase 0. ( FYI, when I used one included in freescale toolchain, system got corrupted for uncertain reason)

host$ qemu-arm-static -L /usr/arm-linux-gnueabi ./program

sum(1, 100) = 101 Hello, ARM World!

The printed message above shows that program ran as intended.


Phase 3. Debugging with Qemu + gdb

With gdb, user can debug application in source level. I already gave option "SET(CMAKE_BUILD_TYPE debug)" to include debug symbol in the executable by including one line in CMakeList.txt so the executable should contain symbol table in itself.

host$ qemu-arm-static -g 1234 -L /usr/arm-linux-gnueabi ./program


With -g 1234 option in the above command, qemu is waiting for gdb connection via port 1234 of localhost.

host$ sudo apt-get install gdb-multiarch

host$ gdb-multiarch

...

(gdb) file program

Reading symbols from /home/<user>/work/qemu/linaro/examples/cassemtest/build/program...done.

(gdb) target remote localhost:1234

Remote debugging using localhost:1234 [New Remote target] warning: Unable to find dynamic linker breakpoint function. GDB will be unable to debug shared library initializers and track explicitly loaded dynamic code. [Switching to Remote target] 0xf67dfc40 in ?? ()

(gdb) b main

Breakpoint 1 at 0x85f0: file /home/<user>/work/qemu/linaro/examples/cassemtest/main.c, line 8.

(gdb) c

Continuing. warning: Could not load shared library symbols for 2 libraries, e.g. /lib/libc.so.6. Use the "info sharedlibrary" command to see the complete listing. Do you need "set solib-search-path" or "set sysroot"? Breakpoint 1, main () at /home/<user>/work/qemu/linaro/examples/cassemtest/main.c:8 8 int a=1, b =100;


Conclusion


So far showed how simple application debugging with qemu can be done and hope this post will help app development for embedded system under qemu sandbox which will throw away any worry to break system due to SW bug.

   



Posted by kevino
,

Building android native application

(Referred to following link: http://software.intel.com/en-us/articles/ndk-android-application-porting-methodologies/)



0. Prerequisites

1. Need to install Tegra android development pack from here

2. Install latest cygwin with development tools including gcc, unzip and etc.



1. Download a zipped source code from here.


2. Build apk file


   <Added>

    Set environment variable ANDROID_HOME to $ANDROID_SDK_DIR

   </Added>


    a. $ ndk-build NDK_DEBUG=1 APP_ABI="armeabi armeabi-v7a x86"

    b. $ android.bat update project --path .

           If you see a error something like "Error: The project either has no target set or the target is invalid.", then you need to define what target you want to use using "--target" option. For example, 

    b-1. $ android.bat update project --path . -t android-15

    c. $ ant -f build.xml debug


   <fix>

   f-1. If the compilation fails here with message "sdk.dir is not defined" then use following command to define sdk.dir manually.

    

        ant -Dsdk.dir=$ANDROID_HOME -f build.xml debug


   f-2. With cygwin env, check all project folder including subdirectory set to readable and writable.

        $(PROJECT_HOME) chmod -R a+rw *

    </fix>


   [Otional 1] If you want to check whether generated apk is valid or not, run below command and see the   output message. Below shows an example.

   o-1. 

unzip -l bin/HelloGdbServer-debug.apk

Archive:  bin/HelloGdbServer-debug.apk

  Length      Date    Time    Name

---------  ---------- -----   ----

     2200  06-09-2012 17:25   res/drawable/icon.png

      864  06-09-2012 19:06   res/layout/main.xml

     1500  06-09-2012 19:06   AndroidManifest.xml

     1296  06-09-2012 19:06   resources.arsc

     3976  06-09-2012 19:06   classes.dex

   125208  04-24-2012 23:20   lib/armeabi/gdbserver

    10052  06-09-2012 18:52   lib/armeabi/libhello-gdbserver.so

   125208  04-24-2012 23:20   lib/armeabi-v7a/gdbserver

    10060  06-09-2012 18:52   lib/armeabi-v7a/libhello-gdbserver.so

   214328  04-24-2012 23:20   lib/x86/gdbserver

     2504  06-09-2012 18:52   lib/x86/libhello-gdbserver.so

      881  06-09-2012 19:06   META-INF/MANIFEST.MF

      934  06-09-2012 19:06   META-INF/CERT.SF

      776  06-09-2012 19:06   META-INF/CERT.RSA

---------                     -------

   499787                     14 files



Debugging NDK application


In the project folder, run below command.

  

$ ndk-gdb --force --start --verbose

Android NDK installation path: /cygdrive/c/NVPACK/android-ndk-r8/

Using default adb command: /cygdrive/c/NVPACK/android-sdk-windows/platform-tools/adb

ADB version found: Android Debug Bridge version 1.0.29

Using ADB flags:

Using auto-detected project path: .

Found package name: com.example.hellogdbserver

ABIs targetted by application: x86

Device API Level: 15

Device CPU ABI: x86

Compatible device ABI: x86

Using gdb setup init: ./libs/x86/gdb.setup

Using toolchain prefix: /cygdrive/c/NVPACK/android-ndk-r8//toolchains/x86-4.4.3/prebuilt/windows/bin/i686-android-linux-

Using app out directory: ./obj/local/x86

Found debuggable flag: true

Found device gdbserver: /data/data/com.example.hellogdbserver/lib/gdbserver

Found data directory: '/data/data/com.example.hellogdbserver'

Found first launchable activity: .HelloGdbServer

Launching activity: com.example.hellogdbserver/.HelloGdbServer

## COMMAND: /cygdrive/c/NVPACK/android-sdk-windows/platform-tools/adb shell am start -n com.example.hellogdbserver/.HelloGdbServer

Starting: Intent { cmp=com.example.hellogdbserver/.HelloGdbServer }


.....

Error while mapping shared library sections:

libOpenglSystemCommon.so: No such file or directory.

Error while mapping shared library sections:

gralloc.goldfish.so: No such file or directory.

warning: Unable to find dynamic linker breakpoint function.

GDB will be unable to debug shared library initializers

and track explicitly loaded dynamic code.

(gdb) list

1       /*

2        * Copyright (C) 2010 Max Vilimpoc


(gdb) Line number 31 out of range; jni/hello-gdbserver.c has 30 lines.

c

Continuing.

[New Thread 2259]


Program received signal SIGSEGV, Segmentation fault.

[Switching to Thread 2259]

0xad5bd4af in Java_com_example_hellogdbserver_HelloGdbServer_invokeCrash (

    env=0x8534500, clazz=0xb49b6c70) at jni/hello-gdbserver.c:29

29              *crasher = 0xdeaddead;

(gdb) bt

#0  0xad5bd4af in Java_com_example_hellogdbserver_HelloGdbServer_invokeCrash (

    env=0x8534500, clazz=0xb49b6c70) at jni/hello-gdbserver.c:29

#1  0xb72a3b80 in ?? ()

#2  0x08534500 in ?? ()



Error case #1. 

If you cannot see debug symbols, need to check gdb.debug file whether it has wrong line ending codes. If such case, then run below command to convert dos style line ending to Unix style so that gdb can parse it to display symbols correctly. After this conversion, restart ndk-gdb command above then you will see debug symbol.


$ tr -d '\012' < libs/x86/gdb.setup | tr '\015\015' '\012\012' > libs/x86/gdb.setup


Posted by kevino
,