gdb调试Android native代码

2023-05-16

调试环境:

    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

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

gdb调试Android native代码 的相关文章

  • 图钉的 OnClickListener

    在这里我使用了谷歌地图和叠加层 我使用了图钉图像来指向 GeoPoint 我想设置一个OnClickListener图钉事件 当用户触摸 pin 时 我想吐槽一条消息 下面是代码 import java util List import c
  • 如何获取 Android 应用程序的内部版本号?

    我需要弄清楚如何获取或创建我的 Android 应用程序的内部版本号 我需要在用户界面中显示内部版本号 我必须做点什么吗AndroidManifest xml 如果您使用 Gradle 插件 Android Studio 从版本 0 7 0
  • Firebase FCM 通知图像不会显示

    我在我的项目中使用 FCM 当尝试使用 firebase 撰写通知 功能测试传入通知时 我将标题 正文和图像 URL 添加到消息中 它显示了它应该是什么样子 丰富的通知与图像 但发送给我的通知是正常的 没有任何图像 这是 firebase
  • 在 Android 中解析日期时出现意外的副作用

    在各种Android项目中 我使用以下静态函数来解析日期 例如1900 12 31 当然 这个函数应该是确定性的 但事实证明并非如此 为什么 通常 它解析日期2010 10 30 例如 到正确的Date持有该值的实例 但我注意到 当我有一个
  • 在android中的操作栏中标题左侧添加图标

    我想在操作栏中标题的左侧添加一个可点击的图标 按钮 怎么做 以下是我向操作栏添加搜索和设置图标的代码 它们出现在右侧 但我想在标题左侧添加一个新图标 怎么做 XML menu menu
  • 如何在android中直接从.zip文件读取文件而不解压它

    过去几个月我一直在研究 android 现在我的问题是读取放在 sdcard 上的 zip 文件 我已经成功完成了将 zip 文件下载到 SD 卡上的编码 我已将 img zip 文件下载到 SD 卡上 此 img zip 包含 5 个图像
  • Android 中未找到 PhoneGap 类错误

    我的 PhoneGap Android 应用程序遇到一些问题 到目前为止我明白了 我已经把一切都做好了 这是我所做的 在 Eclipse 中创建项目后 我在 libs 文件夹中添加了 cordova 2 2 0 jar 然后我编辑了Andr
  • 如何使用 Kotlin 在 ListAdapter 中使用 Filterable?

    我会用一个SearchView过滤我的RecyclerView 在 stackoverflow 和其他网站上我发现只是使用的示例Filterable与 Java 和RecyclerView Adapter当我使用时ListAdapter 所
  • WebView ssl 错误

    对不起我的英语不好 我需要加载 url https 我有一些问题 当我尝试加载页面时 webView 给我错误 primary error 3 certificate Issued to CN my site com Issued by C
  • Genymotion Google Nexus 6P 7.0.0 与 Open_Gapps 手臂 7.0.0

    With Genymotion 2 8 0 我已经安装了谷歌 Nexus 6P 7 0 0API 级别为 24 的设备 启动设备后 我刷新了 ARM 虚拟转换器并重新启动了设备 然后 我从下载 open gapps用于 Playstore
  • 如何通过代码改变Android SlidingDrawer的方向?

    当我从横向模式更改为纵向模式时 我无法找到设置 SlidingDrawer 方向的方法 反之亦然 最初我将 xml 的方向设置为垂直 当手机处于横向模式时 我需要将方向更改为水平 因此我将手柄放在左侧 有人有什么想法吗 我认为按照标准这是不
  • Kotlin 1.6.0 的 proguard / R8 删除了数据类元数据

    我有一个包含一些数据类的包 我尝试使用 Kotlin 反射在运行时访问构造函数clazz primaryConstructor 一切都按预期工作 但是当我启用 R8 时 数据类元数据被删除 例如当我检查是否KClass isData它返回
  • 如何从 Java 类调用 Kotlin 类

    我需要将意图从 java 活动传递到 Kotlin 活动 Java活动ProfileActivity class Intent selectGameIntent new Intent ProfileActivity this kotlin
  • MaterialDatePicker 错误:要在应用程序主题中设置materialCalendarFullscreenTheme 属性

    我做了什么 Added implementation com google android material material 1 1 0 在依赖关系中 Set Theme MaterialComponents Light Bridge作为
  • OkHttp javax.net.ssl.SSLPeerUnverifiedException:主机名domain.com未验证

    我几天来一直在努力让它发挥作用 我正在尝试通过以下方式连接到我的服务器https带有自签名证书 我认为现在没有任何页面或示例是我未读过的 我做了什么 按照本教程创建了 bks 密钥库 http blog crazybob org 2010
  • 使用 jenkins.Creating .apk 文件生成 android 版本

    我正在使用 Jenkins 在 mac 上持续集成 android 应用程序 但是我无法使用 Jenkins 生成 apk 文件 就像我们在 iOS 应用程序中创建 ipa 一样 创建用于在 mac 上分发的 apk 文件的配置是什么 您可
  • java.net.ServerSocket.accept () 在 Android 上不返回

    我正在尝试找到一种方法来远程登录到未root的机器人 我有INTERNET权限处于活动状态 我的设备与我的设备连接在同一网络上Mac OS X通过 WiFi 我可以 ping 通我打开的端口 在最初的实验中 我让它在有根测试设备上工作 但我
  • 在 Android 中使用 SQL (JDBC) 数据库

    在旧的 Java 应用程序中 我使用以下代码连接到 SQL 数据库并将其用于某些查询 private Connection dbConnection null System setProperty derby system home C C
  • 如何指定使用Glide for Android加载图片的重试次数?

    我正在为我的 Android 应用程序使用 glide 库 我想告诉它在放弃并显示错误占位符图像之前重试获取图像 X 次 可能使用指数退避 知道如何做到这一点吗 顺便说一句 我正在使用 Volley 集成 使用您自己的资源解码器 我仅加载本
  • Android Studio同时为同一个项目构建两个应用程序

    我正在使用 Android Studio v0 5 9 制作一个应用程序 它有一个图书馆项目作为依赖 但是 每次我运行该项目时 都会将两个具有相同名称和图标的 APK 部署到我的设备上 第一个 apk app 包含我的主模块 而第二个是库项

随机推荐