1.GDB 调试器简介
GDB 全称 “GNU symbolic debugger”,是 Linux 下常用的程序调试器。GDB 已经迭代了诸多个版本,支持调试多种编程语言编写的程序,包括 C、C++、Go、Objective-C、Rust 等。
GDB 吉祥物.png2.如何 dump 内存
步骤 1:获取目标线程 id 即 Pid 的全部内存地址
# 获取目标线程 id
$ps -ef |grep java |grep filebeat
root 129063 128947 0 Nov07 ? 00:37:10 java -Xms128m -Xmx128m -XX:NewRatio=1 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:G1ReservePercent=15 -XX:InitiatingHeapOccupancyPercent=75 -XX:MaxTenuringThreshold=6 -XX:+ExplicitGCInvokesConcurrent -XX:+AlwaysPreTouch -XX:AutoBoxCacheMax=20000 ...省略
# 获取目标线程 id 即 Pid 的全部内存地址
#cat /proc/{Pid}/smaps或者pmap -x {Pid}
$pmap -x 129063
129063: java -Xms128m -Xmx128m -XX:NewRatio=1 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:G1ReservePercent=15 -XX:InitiatingHeapOccupancyPercent=75 -XX:MaxTenuringThreshold=6 -XX:+ExplicitGCInvokesConcurrent -XX:+AlwaysPreTouch -XX:AutoBoxCacheMax=20000 ...省略
Address Kbytes RSS Dirty Mode Mapping
00000000f8000000 137856 137856 137856 rw--- [ anon ]
00000001006a0000 1041792 0 0 ----- [ anon ]
0000561c3036b000 4 0 0 r---- java
0000561c3036c000 4 0 0 r-x-- java
0000561c3036d000 4 0 0 r---- java
0000561c3036e000 4 4 4 r---- java
0000561c3036f000 4 4 4 rw--- java
0000561c307c5000 132 36 36 rw--- [ anon ]
...省略
步骤 2:获取 Pid 中的指定内存地址的详情
# 获取目标线程 id
$ps -ef |grep java |grep filebeat
root 129063 128947 0 Nov07 ? 00:37:10 java -Xms128m -Xmx128m -XX:NewRatio=1 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:G1ReservePercent=15 -XX:InitiatingHeapOccupancyPercent=75 -XX:MaxTenuringThreshold=6 -XX:+ExplicitGCInvokesConcurrent -XX:+AlwaysPreTouch -XX:AutoBoxCacheMax=20000 ...省略
# 进入 gdb dump
$gdb dump
GNU gdb (GDB) EulerOS 8.3.1-12.eulerosv2r9
Copyright (C) 2019 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-openEuler-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"...
dump: No such file or directory.
# 绑定目标线程 id
(gdb) attach 129063
Attaching to process 129063
[New LWP 129066]
[New LWP 129067]
[New LWP 129068]
[New LWP 129069]
[New LWP 129070]
[New LWP 129071]
[New LWP 129080]
[New LWP 129081]
[New LWP 129082]
[New LWP 129083]
[New LWP 129084]
[New LWP 129085]
[New LWP 129086]
...省略
# dump 目标内存地址的详情
(gdb) dump memory /tmp/dump_memory_filebeat.bin 0x0000561c3036e000 0x0000561c3036f000
网友评论