1. 程式人生 > >gdb除錯Android native程式碼

gdb除錯Android native程式碼

除錯環境:

    Ubuntu 16.04,win10,android 7.1

    其中,win10主機通過USB與被測試機連線,Ubuntu16.04上有android 7.1 SDK程式碼及編譯環境,通過本地網路與被測試機連線。

第一部分:

程式碼示例:

test.cpp:

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>

int main() {
  printf("main start...\n");
  int x=5;
  printf("x=%d\n",x);
  while(true)
 {
    printf("%d \n",x);
    x++;
    sleep(1);
 }
  return 0;
}

Android.mk 

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.cpp
LOCAL_CFLAGS := -g
LOCAL_STRIP_MODULE :=false
LOCAL_CPPFLAGS := -g -O0
include $(BUILD_EXECUTABLE)

以上程式碼可以放到system/core/test目錄,然後編譯生成test。這邊要注意的是,我們需要使用:

out/target/product/***/symbols/system/bin/test 這個bin應用,而非 

out/target/product/***/system/bin/test  目錄下的test。然後將test push到被測試機的/system/bin目錄:

D:\callen\Downloads {git}
{lamb} adb root

D:\callen\Downloads {git}
{lamb} adb remount
remount succeeded

D:\callen\Downloads {git}
{lamb} adb push test /system/bin
test: 1 file pushed. 1.6 MB/s (20560 bytes in 0.012s)

第二部分:

這邊通過android開啟wifi,PC端通過網路連線。

首先,配置連線:

Win10:

1). 配置埠:

adb tcpip 5555 

2). 獲取IP:

 adb shell ifconfig |grep  "inet addr" 

D:\callen\Downloads {git}
{lamb} adb tcpip 5555
//獲取被測試機IP
D:\callen\Downloads {git}
{lamb} adb shell ifconfig |grep  "inet addr"
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet addr:172.16.37.216  Bcast:172.16.37.255  Mask:255.255.255.0

Ubuntu:

連線被測試機:

adb connect 172.16.37.216 

[email protected]:~/N$ adb connect 172.16.37.216
connected to 172.16.37.216:5555

第三部分:

1.android啟動gdbserver 來執行被除錯應用:

rk3399_mid:/ # gdbserver64 :22335 /system/bin/test
Process /system/bin/test created; pid = 1421
Listening on port 22335

配置在埠22335監聽,test的程序號是1421

2.Ubuntu端:

當前路徑位於SDK原始碼根目錄。

通過gdbclient方式(需要source 編譯環境):

Usage: gdbclient <pid|processname> [port number]

[email protected]:~/N$ gdbclient 1421 22335
It looks like gdbserver is already attached to 3592 (process is traced), trying to connect to it using local port=22335
GNU gdb (GDB) 7.11
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from out/target/product/rk3399_mid/symbols/system/bin/test...done.
__dl__start () at bionic/linker/arch/arm64/begin.S:32
32        mov x0, sp

連線上後,Win端的輸出如下:

rk3399_mid:/ # gdbserver64 :22335 /system/bin/test
Process /system/bin/test created; pid = 1421
Listening on port 22335
Remote debugging from host 127.0.0.1

執行dir關聯下程式碼:

(gdb) dir system/core/test
Source directories searched: /home/cai/N/system/core/test:$cdir:$cwd

列印堆疊:

(gdb) bt
#0  __dl__start () at bionic/linker/arch/arm64/begin.S:32
#1  0x0000000000000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

檢視當前斷點情況:

(gdb) info b
No breakpoints or watchpoints.

新增斷點:

(gdb) info b
No breakpoints or watchpoints.
(gdb) b system/core/test/test.cpp:7
Breakpoint 1 at 0x5555555690: file system/core/dd/test.cpp, line 7.
(gdb) b system/core/test/test.cpp:13
Breakpoint 2 at 0x55555556d4: file system/core/dd/test.cpp, line 13.

檢視斷點:

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000005555555690 in main() at system/core/test/test.cpp:7
2       breakpoint     keep y   0x00000055555556d4 in main() at system/core/test/test.cpp:13

繼續執行當前應用:

(gdb) c
Continuing.

Breakpoint 1, main () at system/core/test/test.cpp:7
7         printf("main start...\n");

可見已經在原始碼的第一個斷點停住了。

輸入c繼續執行,在第二個斷點暫停:

(gdb) c
Continuing.

Breakpoint 2, main () at system/core/test/test.cpp:13
13          x++;

列印當前斷點變數值:

(gdb) p x
$1 = 5