|簡體中文

比思論壇

 找回密碼
 按這成為會員
搜索



查看: 628|回復: 1
打印 上一主題 下一主題

linux程序开发的GDB调试

[複製鏈接]

24

主題

0

好友

354

積分

中學生

Rank: 3Rank: 3

  • TA的每日心情
    慵懶
    2024-2-29 13:55
  • 簽到天數: 136 天

    [LV.7]常住居民III

    推廣值
    0
    貢獻值
    0
    金錢
    137
    威望
    354
    主題
    24
    樓主
    發表於 2013-6-14 14:56:56
    无论多么优秀的程序员,必须经常面对的一个问题就是调试。当程序编译完成后,他可能无法正常运行;或许程序会彻底崩溃;或许只是不能正常地运行某些功能;或许它的输出会被挂起;或许不会提示要求正常的输入。无论在何种情况下,跟踪这些问题,特别是在大的工程中,将是开发中最困难的部分,本章将介绍使用gdb(GNU debugger)调试程序的方法,该程序是一个调试器,是用来帮助程序员寻找程序中的错误的软件。
    gdbGNU开发组织发布的一个强大的UNIX/Linux下的程序调试工具。或许,有人比较习惯图形界面方式的,像VCBCBIDE环境,但是在UNIX/Linux平台下做软件,gdb这个调试工具有比VCBCB的图形化调试器更强大的功能。所谓寸有所长,尺有所短就是这个道理。
    一般来说,gdb主要帮忙用户完成下面4个方面的功能:
           启动程序,可以按照用户自定义的要求随心所欲的运行程序。
           可让被调试的程序在用户所指定的调试的断点处停住 (断点可以是条件表达式)
           当程序停住时,可以检查此时程序中所发生的事。
           动态地改变程序的执行环境。
    从上面来看,gdb和一般的调试工具区别不大,基本上也是完成这些功能,不过在细节上,会发现gdb这个调试工具的强大。大家可能习惯了图形化的调试工具,但有时候,命令行的调试工具却有着图形化工具所不能完成的功能。下面通过实例4-4进行说明。
    实例4-1 test.c                           


    #include <stdio.h>
    int func(int n)
    {
         int sum=0,i;
         for(i=0; i<n; i++)
         {
            sum+=i;
           }
         return sum;
    }
    main()
    {
         int i;
         long result = 0;
         for(i=1; i<=100; i++)
          {
            result += i;
          }
         printf("result[1-100] = %d \n", result );
         printf("result[1-250] = %d \n", func(250) );
    }
    编译生成执行文件(Linux)
    [david@david david]$ gcc –g test.c -o test
    使用gdb调试:
    [david@DAVID david]$ gdb test <---------- 启动gdb
    GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
    Copyright 2003 Free Software Foundation, Inc.
    gdb is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for gdb. Type "show warranty" for details.
    This gdb was configured as "i386-redhat-Linux-gnu"...
    (gdb)
    键入 l命令相当于list命令,从第一行开始列出源码:
    [david@DAVID david]$ gdb test
    GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
    Copyright 2003 Free Software Foundation, Inc.
    gdb is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for gdb. Type "show warranty" for details.
    This gdb was configured as "i386-redhat-Linux-gnu"...
    (gdb) l
    7                    {
    8                           sum+=i;
    9                    }
    10                  return sum;
    11           }
    12
    13              main()
    14           {
    15                   int i;
    16                   long result = 0;
    (gdb)
    17                   for(i=1; i<=100; i++)
    18                   {
    19                           result += i;
    20                   }
    21                     printf("result[1-100] = %d \n", result );
    22                 printf("result[1-250] = %d \n", func(250) );
    23           }
    (gdb) break 16 <--------------------
    设置断点,在源程序第16行处。
    Breakpoint 1 at 0x804836a: file test.c, line 16.
    (gdb) break func <--------------------
    设置断点,在函数func()入口处。
    Breakpoint 2 at 0x804832e: file test.c, line 5.
    (gdb) info break <--------------------
    查看断点信息。
    Num Type           Disp Enb Address    What
    1   breakpoint     keep y   0x0804836a in main at test.c:16
    2   breakpoint     keep y   0x0804832e in func at test.c:5
    (gdb) r <---------------------
    运行程序,run命令简写
    Starting program: /home/david/test

    Breakpoint 1, main () at test.c:16 <----------
    在断点处停住。
    16                   long result = 0;
    (gdb) n <---------------------
    单条语句执行,next命令简写。
    17                   for(i=1; i<=100; i++)
    (gdb) n
    19                           result += i;
    (gdb) n
    17                   for(i=1; i<=100; i++)
    (gdb) n
    19                           result += i;
    (gdb) n
    17                   for(i=1; i<=100; i++)
    (gdb) c     <---------------------
    继续运行程序,continue命令简写。
    Continuing.
    result[1-100] = 5050  <----------
    程序输出。

    Breakpoint 2, func (n=250) at test.c:5
    5                   int sum=0,i;
    (gdb) n
    6                    for(i=0; i<n; i++)
    (gdb) p I    <---------------------
    打印变量i的值,print命令简写。
    $1 = 1107620064
    (gdb) n
    8                           sum+=i;
    (gdb) n
    6                    for(i=0; i<n; i++)
    (gdb) p sum
    $2 = 0
    (gdb) bt     <---------------------
    查看函数堆栈。
    #0 func (n=250) at test.c:6
    #1 0x080483b2 in main () at test.c:22
    #2 0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
    (gdb) finish <---------------------
    退出函数。
    Run till exit from #0 func (n=250) at test.c:6
    0x080483b2 in main () at test.c:22
    22   printf("result[1-250] = %d \n", func(250) );
    Value returned is $3 = 31125
    (gdb) c <---------------------
    继续运行。
    Continuing.
    result[1-250] = 31125

    Program exited with code 027. <--------
    程序退出,调试结束。
    (gdb) q     <--------------------- 退出gdb
    [david@DAVID david]$
    有了以上的感性认识,下面来系统地学习一下gdb
    重要聲明:本論壇是以即時上載留言的方式運作,比思論壇對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,讀者及用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,讀者及用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本論壇受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者及用戶發現有留言出現問題,請聯絡我們比思論壇有權刪除任何留言及拒絕任何人士上載留言 (刪除前或不會作事先警告及通知 ),同時亦有不刪除留言的權利,如有任何爭議,管理員擁有最終的詮釋權。用戶切勿撰寫粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。

    手機版| 廣告聯繫

    GMT+8, 2024-5-31 00:59 , Processed in 0.017262 second(s), 24 queries , Gzip On.

    Powered by Discuz! X2.5

    © 2001-2012 Comsenz Inc.

    回頂部