我是靠谱客的博主 端庄衬衫,最近开发中收集的这篇文章主要介绍IOT开发的学习-linux#6 调试工具-gdb整理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

gcc编译,gdb调试的一些整理。

步骤1:gcc编译生成实行文件

        $ gcc test.c -o testnodebug

        #用ls查看实行文件已经生成

        $ ls

        Desktop    Downloads      Music     Public   test.c       Videos

        Documents  examples.desktop  Pictures  Templates  testnodebug

步骤2:gdb调试

        $ gdb testnodebug

        #出现以下错误:

        reading symbols from testnodebug...(no debugging symbols found)...done.

        #原因是没有找到调试需要的信息。gcc编译默认是生成release版,如果要debug,需要加参数 -g 让编译器生成额外的信息用于调试

        #常用的还有-Wall 可以输出所有的警告信息

步骤3:gcc编译生成可调试实行文件

        gcc -g -Wall test.c -o testdebug

步骤4:gdb进入调试

        $ gdb testdebug

        #显示如下:表示已经进入可调式状态。

        GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git

        Copyright (C) 2018 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 testdebug...done.

        (gdb)

        #Reading symbols from testdebug...done.前显示的是gdb版本信息,不想显示可以加参数 - q,如下:

        $ gdb -q testdebug 只显示以下信息

        Reading symbols from testdebug...done.

        (gdb)

步骤5:gdb调试 输入list/l就会显示默认10行代码,按回车就能继续显示。

        (gdb) list

         4 {

        5 printf("hello world1!n");

        6 printf("hello world2!n");

        7 printf("hello world3!n");

        8 }

        9

        10 void change(int* no1,int* no2)

        11 {

        12     int tmp=(*no1);

        13     (*no1)=(*no2);

        (gdb)

        14     (*no2)=tmp;

        15 }

        16

        17 int main()

        18 {

        19     int x=1;

        20     int y=2;

        21     change(&x,&y);

        22     printf("x=%d y=%dn",x,y);

        23     print();

        (gdb)

步骤6:执行程序

1.输入:start 从程序第一步开始执行,输入next/n 下一步,step/s单步执行

        gdb) start

       Temporary breakpoint 1 at 0x55555555475a: file test.c, line 18.

        Starting program: /home/user1/testdebug

        Temporary breakpoint 1, main () at test.c:18

        18 {

        (gdb) n

        19     int x=1;

        (gdb) n

        20     int y=2;

        (gdb)  

2.输入:run/r 直接运行程序到结束或者断点处

        (gdb) run

        Starting program: /home/user1/testdebug

        x=2 y=1

        hello world1!

        hello world2!

        hello world3!

3.设置断点:break/b line(行数)或函数名或条件表达式,输入 c 继续执行到下一个断点

        (gdb) break 20

        Breakpoint 1 at 0x770: file test.c, line 20.

        (gdb) run

        Starting program: /home/user1/testdebug

        Breakpoint 1, main () at test.c:20

        20     int y=2;

        (gdb) break change

        Breakpoint 2 at 0x555555554731: file test.c, line 12.

        (gdb) c

        Continuing.

        Breakpoint 2, change (no1=0x7fffffffe970, no2=0x7fffffffe974) at test.c:12

        12     int tmp=(*no1);

        (gdb)

4.info 查看断点

        (gdb) info break

        Num     Type           Disp Enb Address            What

        1       breakpoint     keep y   0x0000555555554770 in main at test.c:20

        breakpoint already hit 1 time

        2       breakpoint     keep y   0x0000555555554731 in change at test.c:12

        breakpoint already hit 1 time

        (gdb)

5.删除断点:

        clear 行号 : 删除这行的断点

        clear 函数名 : 删除该函数的断点

        (gdb) clear 20

        Deleted breakpoint 1

        (gdb) info break

        Num     Type           Disp Enb Address            What

        2       breakpoint     keep y   0x0000555555554731 in change at test.c:12

        breakpoint already hit 1 time

        (gdb) clear change

        Deleted breakpoint 2

        (gdb) info break

        No breakpoints or watchpoints.

        (gdb)

        # delete 断点号n:删除第n个断点

        # disable 断点号n:暂停第n个断点

        #enable断点号n:开启第n个断点

        #clear 行号n: 清除第n行的断点

        #delete breakpoints或clear:清除所有断点

6.print/p +变量 打印变量值

       (gdb) print x

        $1 = 1

         (gdb)

7.quit/q 退出调试

        (gdb) q

        A debugging session is active.

        Inferior 1 [process 3098] will be killed.

        quit anyway? (y or n) y

8.set 参数

        (gdb) set x=3

        (gdb) p x

        $2 = 3

        (gdb)

其他的一些常用方法:

9.finish    退出当前函数   

10.until+行数 执行到指定函数

11 whatis+变量 查看类型

        (gdb) whatis x

        type = int

12.call+函数(参数)调用单个函数

13. where 查看当前运行的位置

        (gdb) where

        #0  0x0000555555554735 in change (no1=0x4, no2=0x5) at test.c:12

        #1  <function called from gdb>

        #2  main () at test.c:22

        (gdb)

最后

以上就是端庄衬衫为你收集整理的IOT开发的学习-linux#6 调试工具-gdb整理的全部内容,希望文章能够帮你解决IOT开发的学习-linux#6 调试工具-gdb整理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部