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 

cai@ubuntu:~/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源码根目录。

A:通过gdbclient方式(需要source 编译环境):

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

cai@ubuntu:~/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

B:如果使用gdb来直接调试:

进入prebuilts/gdb/linux-x86/bin目录,执行:gdb,然后输入:

target remote 172.16.37.216:22335

格式:target remote 被调试机IP:端口

cai@ubuntu:~/N/prebuilts/gdb/linux-x86/bin$ gdb 
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 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".
(gdb) target remote 172.16.37.216:22335
Remote debugging using 172.16.37.216:22335

 

连接上后,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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值