gdb调试入门(下)

GDB调试主要包括:

1、查看运行时数据

2、程序错误

3、gdb调试逻辑错误

4、gdb调试段错误

5、core文件调试

 

一、查看运行时数据

1、print 查看变量值

2、ptype 变量: 查看数据类型

3、print array(数组名):  查看数组

4、print *array@len(p *arr2@10 查看动态内存10个元素内容):  查看动态内存(malloc分配的内存),静态数组也可以这么查看

5、print  i=5 : 改变运行时数据

 

二、程序错误

1、编译错误:编写程序时没有符合语言规范导致编译错误

2、运行时错误:编译器检查不出这种错误,但在运行时可能导致崩溃

3、逻辑错误:程序逻辑出错

 

三、gdb调试段错误

段错误是由于访问非法地址而产生的错误,访问系统数据区,尤其是往系统保护的内存地址写数据,最常见就是给一个指针以0地址,或者内存越界访问到不属于你的内存区等。

例如:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 void segfault()
  5 {
  6         int *p =NULL;
  7         *p=100;
  8 }
  9 
 10 int main(void)
 11 {
 12         segfault();

      }
[root@tlinux shell]# gcc -g seg.c -o seg
[root@tlinux shell]# gdb seg
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>;...
Reading symbols from /shell/seg...done.
(gdb) r
Starting program: /shell/seg 

Program received signal SIGSEGV, Segmentation fault.
0x00000000004004dd in segfault () at seg.c:7
7        *p=100;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.3.x86_64
(gdb) bt
#0  0x00000000004004dd in segfault () at seg.c:7
#1  0x00000000004004f3 in main () at seg.c:12
(gdb) 

 

四、core文件调试

详见https://www.cnblogs.com/wsw-seu/p/10589187.html

posted on 2019-05-07 22:28  wsw_seu  阅读(248)  评论(0编辑  收藏  举报

导航