zoukankan      html  css  js  c++  java
  • 每天学点GDB 1

    在Linux环境下进行C或是C++编程,调试工具首选GDB。

    GDB的功能很多,一下子全弄明白似乎不太可能。那么就从最简单的使用说起吧。一谈起简单,Helloworld就成了最佳的选择了。

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3
    4 int main(int argc, char** argv) {
    5     printf("hello,world\n");
    6     return 0;
    7 }

    编译链接

    gcc -o hello -g helloworld.c
    

     好了,现在可执行目标文件hello已经生成,接下来的事情就是用gdb将其载入并运行。

    gdb ./hello
    

    接下来的会看到如下的信息。

    gdb hello
    GNU gdb (GDB) 7.5.1
    Copyright (C) 2012 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-unknown-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /tmp/hello...done.
    (gdb)
    

    显示源代码

    (gdb)list 1,10

    1	#include <stdlib.h>
    2	#include <stdio.h>
    3
    4	int main(int argc, char** argv) {
    5	  printf("hello,world\n");
    6	  return 0;
    7	}
    

    设置断点有多种方法,如通过函数名,通过指定源码行数,下面的例子通过指定文件中的行数来设置断点

    (gdb)break 7

    运行程序

    (gdb)run

    当检查完相关的变量后,继续运行程序

    (gdb)continue

    最终,退出gdb

    (gdb)quit

    ok,重新回到bash shell.

    至此一个简单的调试例子完整的过了一遍,是的,平淡的如白开水一般。

  • 相关阅读:
    如何进入docker 使用root用户的方式
    阿里云服务搭建nginx并配置
    阿里云容器部署Redis集群
    Redis运维利器 -- RedisManager
    远程连接-使用SSH密钥对远程登录阿里云云服务器
    第1课:SQL注入原理深度解析
    数据库设计三大范式
    linux指令大全(归类整理)
    linux目录结构
    linux-创建/使用快照/克隆(类似windows中备份还原)
  • 原文地址:https://www.cnblogs.com/hseagle/p/GDB.html
Copyright © 2011-2022 走看看