zoukankan      html  css  js  c++  java
  • GDB调试

    一 GDB调试Core_dump

     1 设置core文件大小为无限制:

    ulimit -c unlimited

       2 使用gcc -g 编译程序(-g 编译器将符号表(对应于程序的变量和代码行的内存地址列表)保存在生成的可执行文件中):

    gcc -g ./test.c

     2 运行程序 ./a.out,产生段错误,生成core文件。

       3 使用gdb查看core文件 :

      gdb ./a.out ./core

      输出如下:

    GNU gdb (Ubuntu 7.9-1ubuntu1) 7.9
    Copyright (C) 2015 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 ./a.out...done.
    [New LWP 3115]
    Core was generated by `./a.out'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  0x000000000040051b in do_it () at ./test.c:11
    11        *p = 'a';

      如果只想显示错误所在行,使用where:

    (gdb) where
    #0  0x000000000040051b in do_it () at ./test.c:11
    #1  0x0000000000400504 in main () at ./test.c:4

    二 GDB插入断点

      1)使用gcc -g 编译源代码得到可执行文件 b

     gcc btest.c -g -o b.out

      2)进入gdb模式,file b 加载可执行文件b

      (gdb) file b.out

      Reading symbols from b.out

      ...done.

      3)run 执行

    (gdb) r
    Starting program: /home/lijinxu/UNIX/Gdb/b.out 
    n = 1, nGlobalVar = 88 /ntempFunction is called, a = 1, b = 2 /nn = 3[Inferior 1 (process 3240) exited normally]

      4) 在主函数入口处 插入断点 然后run

    (gdb) b main
    Breakpoint 2 at 0x40056d: file btest.c, line 14.
    (gdb) r
    Starting program: /home/lijinxu/UNIX/Gdb/b.out 
    Breakpoint 2, main () at btest.c:14
    14        n = 1;

      上面信息表示下面将要执行的语句是 第14行: n=1;

      5)step 执行下一条:

    (gdb) s
    15        n++;

      6)print 查看变量的值 :

    (gdb) p n
    $1 = 1

      横插一脚:在vi中显示行号:

    set nu  #全部显示行号
    :nu       #显示当前行号

      7)按照函数名和行号分别插入断点:

    (gdb) break 26
    Breakpoint 3 at 0x4005da: file btest.c, line 26.
    (gdb) b tempFunction 
    Breakpoint 4 at 0x400544: file btest.c, line 7.

      8)continue命令继续执行:

    (gdb) c
    Continuing.
    n = 1, nGlobalVar = 88 
    
    Breakpoint 4, tempFunction (a=1, b=2) at btest.c:7
    7        printf("tempFunction is called, a = %d, b = %d 
    ", a, b);

      9)

    三 查看汇编代码

    (gdb) display /i $pc

      以后再进行到断点处就会显示相应的汇编代码了。

    四 删除断点

      d

    五 查看寄存器

      information register

    (gdb) b *main
    Breakpoint 1 at 0x400565: file btest.c, line 12.
    (gdb) r
    Starting program: /home/lijinxu/UNIX/Gdb/b.out 
    
    Breakpoint 1, main () at btest.c:12
    12    {
    (gdb) i r
    rax            0x400565    4195685
    rbx            0x0    0
    rcx            0x0    0
    rdx            0x7fffffffdf18    140737488346904
    rsi            0x7fffffffdf08    140737488346888
    rdi            0x1    1
    rbp            0x4005f0    0x4005f0 <__libc_csu_init>
    rsp            0x7fffffffde28    0x7fffffffde28
    r8             0x7ffff7dd4dd0    140737351863760
    r9             0x7ffff7de99d0    140737351948752
    r10            0x833    2099
    r11            0x7ffff7a2f950    140737348041040
    r12            0x400440    4195392
    r13            0x7fffffffdf00    140737488346880
    r14            0x0    0
    r15            0x0    0
    rip            0x400565    0x400565 <main>
    eflags         0x246    [ PF ZF IF ]
    cs             0x33    51
    ss             0x2b    43
    ds             0x0    0
    es             0x0    0
    fs             0x0    0

      显示指定寄存器:

    (gdb) i r eax

    六 退出

      q

  • 相关阅读:
    linux如何查看端口或服务被占用情况
    linux网络查看及配置相关命令
    linux查看程序运行相关命令
    shell脚本编写一个用真实用户去访问的vsftpd服务器
    shell脚本监控CPU和内存利用率
    小白的个人技能树(基于自动化软件测试开发实习和软件开发实习)
    MySQL 8.0.12 基于Windows 安装教程(超级详细)
    C语言 0x7fffffff是多少(也就是INT_MAX,首位是 0,其余都是1,f代表1111)
    数通知识点
    数据结构之算法基础
  • 原文地址:https://www.cnblogs.com/luntai/p/6553705.html
Copyright © 2011-2022 走看看