• 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

  • 相关阅读:
    判断输入的是否是汉字(中文,不包括中文符号)
    第XX行将截断字符串或二进制数据。语句已终止
    Joomla学习之旅开始啦
    bean加载与注入之重新理解 L
    Excelsior JET 7.6 MP5 发布
    Squid Analyzer 5.1 发布,Squid日志统计
    flashrd 1.3 发布,OpenBSD 安装器
    Oracle 宣布 MySQL 5.6 正式版发布
    Spring Hateoas 0.4 发布
    MacPorts 2.1.3 发布,Mac 软件包管理
  • 原文地址:https://www.cnblogs.com/wsw-seu/p/10828634.html
Copyright © 2020-2023  润新知