zoukankan      html  css  js  c++  java
  • 02.Linux下C语言程序的调试

    你可以用 gcc -S test.c 命令生成test.c对应的汇编语言文件,是at&t格式的。

    xumh@ubuntu:~/cpp$ gcc --o gdb gdb.c   //参数-g是为了产生debug信息
    xumh@ubuntu:
    ~/cpp$ ls
    gdb  gdb.c  myfirst  test1.c  test1.s
    xumh@ubuntu:
    ~/cpp$ gdb gdb //用GDB命令调试刚才生成的gdb程序
    GNU gdb 
    6.8-debian
    Copyright (C) 
    2008 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"
    (gdb) list
    1       //测试gdb实现对c语言源程序的调试
    2
    3       #include <stdio.h>
    4       int main(int argc, char **argv)
    5       {
    6               int sum = 0;
    7               int i=0;
    8               for(; i<10; i++)
    9                       sum += i;
    10              printf("the value is:%d.\n",sum);
    (gdb) 
    break 9//还可以对function中断
    Breakpoint 1 at 0x4004b7: file gdb.c, line 9.
    (gdb) run
          //执行程序
    Starting program: 
    /home/xumh/cpp/gdb

    Breakpoint 
    1, main (argc=1, argv=0x7fff42873018) at gdb.c:9
    9                       sum += i;
    (gdb) watch sum  
    //观察变量
    Hardware watchpoint 
    2: sum
    (gdb) watch i
    Hardware watchpoint 
    3: i
    (gdb) next   
    //单步执行
    8               for(; i<10; i++)
    (gdb) next
    Hardware watchpoint 
    3: i

    Old value 
    = 0
    New value 
    = 1
    0x00000000004004c1 in main (argc=1, argv=0x7fff42873018) at gdb.c:8
    8               for(; i<10; i++)
    (gdb) next

    Breakpoint 
    1, main (argc=1, argv=0x7fff42873018) at gdb.c:9
    9                       sum += i;
    (gdb) next
    Hardware watchpoint 
    2: sum

    Old value 
    = 0
    New value 
    = 1

    main (argc
    =1, argv=0x7fff42873018) at gdb.c:8
    8               for(; i<10; i++)
    (gdb) next
    Hardware watchpoint 
    3: i

    Old value 
    = 1
    New value 
    = 2
    0x00000000004004c1 in main (argc=1, argv=0x7fff42873018) at gdb.c:8
    8               for(; i<10; i++)
    (gdb) next

    Breakpoint 
    1, main (argc=1, argv=0x7fff42873018) at gdb.c:9
    9                       sum += i;
    (gdb) next
    Hardware watchpoint 
    2: sum

    Old value 
    = 1
    New value 
    = 3
    main (argc
    =1, argv=0x7fff42873018) at gdb.c:8
    8               for(; i<10; i++)
    (gdb) next
    Hardware watchpoint 
    3: i

    Old value 
    = 2
    New value 
    = 3
    0x00000000004004c1 in main (argc=1, argv=0x7fff42873018) at gdb.c:8
    8               for(; i<10; i++)
    (gdb) next

    Breakpoint 
    1, main (argc=1, argv=0x7fff42873018) at gdb.c:9
    9                       sum += i;
    (gdb) next
    Hardware watchpoint 
    2: sum

    Old value 
    = 3
    New value 
    = 6
    main (argc
    =1, argv=0x7fff42873018) at gdb.c:8
    8               for(; i<10; i++)

    (gdb)

  • 相关阅读:
    【原创】交换机多端口抓包配置教程
    【转】常用的C语言宏定义
    【转】C语言中可变参数的函数(三个点,“...”)
    【转】SIP响应状态码
    【转】交换机常用功能——端口监控应用举例
    【转】网线水晶头的接法
    【转】三层交换机vlan接口配置
    【转】各种常见的交换机接口及连接方法介绍
    【转】Internet多媒体会议工具包的使用示例
    【转】SDP
  • 原文地址:https://www.cnblogs.com/flaaash/p/1327285.html
Copyright © 2011-2022 走看看