What is GDB?
-
It is a powerful Unix developed by the GNU organization. ProgramDebugging tool, which can be used to debug C and C ++ on Android Code.
-
It can mainly do four things:
- Start your program as you like.
- When a breakpoint is set, the program stops running at the breakpoint. (The breakpoint can be an expression)
- After the program is stopped, you can view what happens in the program.
- Dynamically change the execution environment of the program.
GDB remote debugging schematic
-
As shown in, we need to use gdbserver to attach to the process we want to debug. GDB uses adbd to communicate with the gdbserver on the mobile phone through socket.
Remote debugging practices
-
- Start gdbserver on your mobile phone, attach the process you want to debug, and specify the port for listening to Debugging commands (this port is the port on your mobile phone)
$ ADB shell # ps # view the PID of the process to be debugged # gdbserver: 1234 -- attach 96 #: 1234 is the port number, 96 is the process ID
-
- Use ADB for port ing to direct the port on the PC to the port listened by gdbserver on the mobile phone
$ ADB forward TCP: 1234 TCP: 1234 # port ing: maps port 1234 of the PC to port 1234 of the mobile phone
-
- Start GDB and send messages to the specified PC port to start debugging.
Android _ project_root/prebuilt/linux-x86/arm-eabi-4.4.3/$. /ARM-Eabi-GDB # Use the gdb client in the project to connect to gdbserver. The GDB type must be set to the mobile phone platform. The version must be consistent with that of gdbserver.
<GDB> target remote: 1234 # connect to the local port 1234. The port has been mapped to port 1234 of the mobile phone. <GDB> file xxx/out/target/XX... XX/symbols/system/bin/mediaserver # load the executable program. Select the program under symbols. If the debugging process is an app process, select app_process. <GDB> set solib-search-path xxx/out/target/XX... XX/symbols/system/lib # Load all dynamic connection libraries. Select symbols. <GDB> BT # view the call stack <GDB> List xxx/XXX .. xxx/xxx. CPP # view related files <GDB> B 100 # Break a breakpoint in line 100th of the file <GDB> C # continue to execute the program
Use gdbclient to simplify the debugging process
-
- go to the source code project directory, configure the environment, and run envsetup In the build folder. SH and choosecombo xx commands, then you can use gdbclient.
- the ADB shell enters the mobile phone and enables gdbserver as before.
- in the PC source code root directory, run gdbclient-e mediaserver-P 1234 # debug mediaserver from Port 1234