Use GDB to debug C Programs in Android emulator

Source: Internet
Author: User

1. Download Android debugging utilities

GDB and GDB-server (6.8)

2. Start the android emulator Simulator

$ Emulator @ 1.5 _ r2

3. Place the GDB and debugging files and source code on the simulator.

$ ADB push GDB/data/bin

$ ADB push helloworld/data/bin

Take helloworld as an example. The source code should be placed in the/data/bin/development/Hello directory.

Some articles say that the above files are stored in the/system/bin directory, but the consequence is that once the simulator is turned off, these files will disappear and will be re-transmitted next time ~ Therefore, we recommend that you store the data in the/data/bin directory.

4. Start Shell

$ ADB Shell

If you are prompted that the file system cannot be written, run:

# Mount-O remount //

5. Because the android file system does not have/bin/sh, we copy a bash file (click to download
) To the/bin directory:

# Mkdir/bin

$ ADB push bash/bin

Or set the shell path:

# Export shell =/system/bin/sh

6. Enter the/data/bin directory and run GDB. You can start to debug the program.

# Cd/data/bin

#./GDB helloworld

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

Debugging C/C ++ programs on Android has always been a problem.
The problem is solved by outputting logs. For a slightly complicated project, this is almost an impossible task, especially some errors. It is okay in wincewindows, only on Android.
It is even harder to find. After reading some information, I know that gdbserver can be used for debugging. Today I decided to make it clear first. Otherwise, the efficiency will be too low. I found a lot of websites and
. Here we will sort out the entire process for future reference.

1. Prepare gdbserver.
For Android 1.0
When the code was just opened, there was no gdbserver in it, and some strong people compiled gdbserver for use. But now, the new Android source code already contains
Gdbserver is in the prebuilt directory. If you want to use in Android 1.0, you can download: http://android.git.kernel.org /? P = platform/prebuilt. Git; A = tree
. The gdbserver binary file is stored in Android-arm/gdbserver. We only need to put the gdbserver executable file on the simulator.
Put it in/system/bin so that it is in the default path. After the simulator is started:
ADB push gdbserver/system/bin
If you are prompted that the file system is not writable, Run "ADB remount", because it is mounted in read-only mode by default.

2. Compile the program.

By default, the android compilation system uses the "-G" option to compile the program, that is, the debugging information has been generated. However, when the final file is generated, it passes through strip, removing
All debugging information. So we finally need to use the files before strip to the debugging target. Use make showcommands to view detailed command information. The following is the output of a specific command:
Target executable: libomstts (Out/target/product/generic/obj/executables/libomstts_intermediates/linked/libomstts)
Prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/ARM-Eabi-G ++-nostdlib
-Bdynamic-wl,-T, build/CORE/armelf. x
-Wl,-dynamic-linker,/system/bin/linker-wl, -- GC-Sections
-Wl,-Z, nocopyreloc-o
Out/target/product/generic/obj/executables/libomstts_intermediates/linked/libomstts
-Lout/target/product/generic/obj/lib
-Wl,-rpath-link = out/target/product/generic/obj/lib-llog-lcutils
-Lutils-landroid_runtime-lnativehelper-lstdc ++-LZ-LC-lstdc ++-LM
Out/target/product/generic/obj/lib/crtbegin_dynamic.o
Out/target/product/generic/obj/executables/libomstts_intermediates/src/TTS. o

Out/target/product/generic/obj/executables/libomstts_intermediates/src/ttswrapper. o
Out/target/product/generic/obj/lib/crtend_android.o
Target non-prelinked: libomstts (Out/target/product/generic/symbols/system/bin/libomstts)
Out/host/linux-x86/bin/ACP-FPT
Out/target/product/generic/obj/executables/libomstts_intermediates/linked/libomstts
Out/target/product/generic/symbols/system/bin/libomstts
Target Strip: libomstts (Out/target/product/generic/obj/executables/libomstts_intermediates/libomstts)
Out/host/linux-x86/bin/soslim -- strip -- shady -- quiet
Out/target/product/generic/symbols/system/bin/libomstts -- OUTFILE
Out/target/product/generic/obj/executables/libomstts_intermediates/libomstts
Install: Out/target/product/generic/system/bin/libomstts
Out/host/linux-x86/bin/ACP-FPT
Out/target/product/generic/obj/executables/libomstts_intermediates/libomstts
Out/target/product/generic/system/bin/libomstts

The generated executable file is libomstts. You can see that the target file for the first link is "Out/target/product/generic/obj ".
/Executables/libomstts_intermediates/linked/libomstts ", and then copy it to" Out/Target
/Product/generic/symbols/system/bin/libomstts ". The Strip file is" Out/target ".
/Product/generic/obj/executables/libomstts_intermediates/libomstts "and" out
/Target/product/generic/system/bin/libomstts ". Only the first two files can be used for debugging.
Put the debugging information to the executable file on the simulator. I used "Out/target/product/generic/symbols/system/bin/libomstts ":
ADB push out/target/product/generic/symbols/system/bin/libomstts/system/bin

3. Start the debugger
Start gdbserver on the simulator:
ADB Shell
After entering the simulator Console
Gdbserver 10.0.2.2: 1234/system/bin/libomstts
10.0.2.2 is the default IP address of the simulator. The gdbserver listens to port 1234 on the simulator. If the startup is successful, the following information is displayed:
Process/system/bin/libomstts created; pid = 1025
Listening on port 1234
To enable GDB to connect to the simulator to gdbserver, Data Forwarding is required:
Telnet local host 5554
Trying 127.0.0.1...
Connected to localhost.
Escape Character is '^]'.
Android Console: Type 'help' for a list of commands
OK
Redir add TCP: 1234: 1234
OK
Exit
The above Telnet localhost 5554, redir add
TCP: 1234: 1234, exit is a self-input command, and others are output information. 5554 is the listening port of the simulator console.
Localhost: 1234 data is forwarded to port 1234 of the simulator.
Start GDB on the local machine:
Arm-Eabi-GDB out/target/product/generic/symbols/system/bin/libomstts
Arm-Eabi-GDB is included in the toolchain provided by Android. Note that the executable files following it are earlier than strip.
After GDB is started, run the following command in GDB to connect to gdbserver:
Target remote localhost: 1234
After successfully connecting to the gdbserver, you can use all the gdb Debugging commands.

Currently, the gdbserver cannot debug the dynamic link library. It can only be compiled into an executable file for debugging.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.