6-1 学习使用gdb
使用gdb命令进入gdb来调试
gdb ./helloworld #或者 gdb helloworld
操作过程
其中,p a表示打印a的值
表示十六进制, d表示整型, u表示无符号整型, f表示浮点型, s表示字符串。
b(byte)表示1个字节, h(half-word)表示2个字节, w(word)表示4个字节, g(giant)表示8个字节。
操作过程
6-2 段错误与内存转储
什么情况下会发生段错误?
使用gdb命令来定位段错误
实际操作
实际操作
ulimit -c unlimited #shell的路径在main.cpp路径下
./helloworld
gdb helloworld core.*
bt #使用bt查看函数调用栈
gdb的补充说明
如何生成带有调试信息的可执行文件?
如何判断一个程序带有调试信息?
实验操作,带有调试信息的helloworld
objdump -h helloworld
##生成下面的信息
0 .interp 0000001c 0000000000400238 0000000000400238 00000238 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
1 .note.ABI-tag 00000020 0000000000400254 0000000000400254 00000254 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
2 .note.gnu.build-id 00000024 0000000000400274 0000000000400274 00000274 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
3 .gnu.hash 00000030 0000000000400298 0000000000400298 00000298 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .dynsym 00000150 00000000004002c8 00000000004002c8 000002c8 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 .dynstr 00000155 0000000000400418 0000000000400418 00000418 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
6 .gnu.version 0000001c 000000000040056e 000000000040056e 0000056e 2**1
CONTENTS, ALLOC, LOAD, READONLY, DATA
7 .gnu.version_r 00000050 0000000000400590 0000000000400590 00000590 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
8 .rela.dyn 00000030 00000000004005e0 00000000004005e0 000005e0 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
9 .rela.plt 000000c0 0000000000400610 0000000000400610 00000610 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
10 .init 0000001a 00000000004006d0 00000000004006d0 000006d0 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
11 .plt 00000090 00000000004006f0 00000000004006f0 000006f0 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
12 .plt.got 00000008 0000000000400780 0000000000400780 00000780 2**3
CONTENTS, ALLOC, LOAD, READONLY, CODE
13 .text 000002b2 0000000000400790 0000000000400790 00000790 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
14 .fini 00000009 0000000000400a44 0000000000400a44 00000a44 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
15 .rodata 00000004 0000000000400a50 0000000000400a50 00000a50 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
16 .eh_frame_hdr 0000005c 0000000000400a54 0000000000400a54 00000a54 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
17 .eh_frame 00000194 0000000000400ab0 0000000000400ab0 00000ab0 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
18 .init_array 00000018 0000000000600df0 0000000000600df0 00000df0 2**3
CONTENTS, ALLOC, LOAD, DATA
19 .fini_array 00000008 0000000000600e08 0000000000600e08 00000e08 2**3
CONTENTS, ALLOC, LOAD, DATA
20 .jcr 00000008 0000000000600e10 0000000000600e10 00000e10 2**3
CONTENTS, ALLOC, LOAD, DATA
21 .dynamic 000001e0 0000000000600e18 0000000000600e18 00000e18 2**3
CONTENTS, ALLOC, LOAD, DATA
22 .got 00000008 0000000000600ff8 0000000000600ff8 00000ff8 2**3
CONTENTS, ALLOC, LOAD, DATA
23 .got.plt 00000058 0000000000601000 0000000000601000 00001000 2**3
CONTENTS, ALLOC, LOAD, DATA
24 .data 00000010 0000000000601058 0000000000601058 00001058 2**3
CONTENTS, ALLOC, LOAD, DATA
25 .bss 00000118 0000000000601080 0000000000601080 00001068 2**5
ALLOC
26 .comment 0000002d 0000000000000000 0000000000000000 00001068 2**0
CONTENTS, READONLY
27 .debug_aranges 00000060 0000000000000000 0000000000000000 00001095 2**0
CONTENTS, READONLY, DEBUGGING
28 .debug_info 000029d4 0000000000000000 0000000000000000 000010f5 2**0
CONTENTS, READONLY, DEBUGGING
29 .debug_abbrev 00000765 0000000000000000 0000000000000000 00003ac9 2**0
CONTENTS, READONLY, DEBUGGING
30 .debug_line 000004ad 0000000000000000 0000000000000000 0000422e 2**0
CONTENTS, READONLY, DEBUGGING
31 .debug_str 00000d46 0000000000000000 0000000000000000 000046db 2**0
实验操作,不带调试信息的helloworld
objdump -h helloworld
## 生成以下信息
0 .interp 0000001c 0000000000400238 0000000000400238 00000238 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
1 .note.ABI-tag 00000020 0000000000400254 0000000000400254 00000254 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
2 .note.gnu.build-id 00000024 0000000000400274 0000000000400274 00000274 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
3 .gnu.hash 00000030 0000000000400298 0000000000400298 00000298 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .dynsym 00000150 00000000004002c8 00000000004002c8 000002c8 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 .dynstr 00000155 0000000000400418 0000000000400418 00000418 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
6 .gnu.version 0000001c 000000000040056e 000000000040056e 0000056e 2**1
CONTENTS, ALLOC, LOAD, READONLY, DATA
7 .gnu.version_r 00000050 0000000000400590 0000000000400590 00000590 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
8 .rela.dyn 00000030 00000000004005e0 00000000004005e0 000005e0 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
9 .rela.plt 000000c0 0000000000400610 0000000000400610 00000610 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
10 .init 0000001a 00000000004006d0 00000000004006d0 000006d0 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
11 .plt 00000090 00000000004006f0 00000000004006f0 000006f0 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
12 .plt.got 00000008 0000000000400780 0000000000400780 00000780 2**3
CONTENTS, ALLOC, LOAD, READONLY, CODE
13 .text 000002b2 0000000000400790 0000000000400790 00000790 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
14 .fini 00000009 0000000000400a44 0000000000400a44 00000a44 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
15 .rodata 00000004 0000000000400a50 0000000000400a50 00000a50 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
16 .eh_frame_hdr 0000005c 0000000000400a54 0000000000400a54 00000a54 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
17 .eh_frame 00000194 0000000000400ab0 0000000000400ab0 00000ab0 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
18 .init_array 00000018 0000000000600df0 0000000000600df0 00000df0 2**3
CONTENTS, ALLOC, LOAD, DATA
19 .fini_array 00000008 0000000000600e08 0000000000600e08 00000e08 2**3
CONTENTS, ALLOC, LOAD, DATA
20 .jcr 00000008 0000000000600e10 0000000000600e10 00000e10 2**3
CONTENTS, ALLOC, LOAD, DATA
21 .dynamic 000001e0 0000000000600e18 0000000000600e18 00000e18 2**3
CONTENTS, ALLOC, LOAD, DATA
22 .got 00000008 0000000000600ff8 0000000000600ff8 00000ff8 2**3
CONTENTS, ALLOC, LOAD, DATA
23 .got.plt 00000058 0000000000601000 0000000000601000 00001000 2**3
CONTENTS, ALLOC, LOAD, DATA
24 .data 00000010 0000000000601058 0000000000601058 00001058 2**3
CONTENTS, ALLOC, LOAD, DATA
25 .bss 00000118 0000000000601080 0000000000601080 00001068 2**5
ALLOC
26 .comment 00000035 0000000000000000 0000000000000000 00001068 2**0
什么时候使用gdb,是么时候使用打印调试
gdb调试 -带有命令行参数参考博客
今天在实现http客户端编程的时候,需要使用命令行参数来输入host的ip地址。但是程序运行的过程中一直报段错误。没办法,只能用gdb进行调试。那么怎样在调试的时候输入我们需要的命令行参数呢。一般有两种方法。
进入gdb调试:
gcc -g -Wall httpTest.c -o test #生成了可调试文件
gdb test 进入调试
下面可以使用两种方法输入命令行参数
run 命令行参数
set args 命令行参数
如:我的程序中需要输入的时服务器端ip地址,可以通过以下两种方法输入
如果直接运行程序,run www.baidu.com
set args www.baidu.com,后面再继续进行调试
实验操作
这里演示调试darknet,我们使用gdb darknet运行gdb,然后添加调试的命令行参数set args detect cfg/yolov2.cfg yolov2.weights data/dog.jpg,最后run运行整个程序。
这里由于运行gdb darknet时没有加sudo,所以报错:"darknet" received signal SIGBRT, Aborted.,加上sudo即可正常调试。