`
clouisandy
  • 浏览: 19775 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

gdb debug c

 
阅读更多
用gdb可以debug c程序,简单操作如下:
step-0:准本工作
拿下面的程序为例子
#include <stdio.h>
#include <ctype.h>

main()
{
	int c;

	while((c=getchar())!=EOF){
		if(isspace(c) && c!='\n'){
			printf("is a space!\n");
		}else if(isdigit(c)){
			printf("is a number!\n");
		}else if(isalpha(c)){
			printf("is a letter!\n");
		}
	}
	
}




step-1:编译
gcc -g test.c -o test
注:-g是调试参数,-o指定目标名称,这样会生成test.exe

step-2:进入gdb
gdb test

注:gdb <filename>

此时命令行显示:
GNU gdb 5.2.1
Copyright 2002 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 "i686-pc-mingw32"...
(gdb)


step-3:打断点
(gdb) list
1       #include <stdio.h>
2       #include <ctype.h>
3
4       main()
5       {
6               int c;
7
8               while((c=getchar())!=EOF){
9                       if(isspace(c) && c!='\n'){
10                              printf("is a space!\n");

注:运行list可以查看源码,一次10行
找到要打断点的行,比如第8行,可以输入
(gdb) break 8

此时显示
Breakpoint 1 at 0x4012ba: file test.c, line 8.


step-4:debug
现在可以运行程序了,命令是:
(gdb) run

会弹出一个新的cmd,是运行的界面,此时gdb的界面会显示:
Starting program: E:\forc\6/test.exe

Breakpoint 1, main () at test.c:8
8               while((c=getchar())!=EOF){


可以看到断点信息,此时可以单步一下:
(gdb) next

下面在运行的界面输入一个字符,比如说'a',回车
单步操作,可以在gdb界面看到如下信息:
8               while((c=getchar())!=EOF){
(gdb) next
9                       if(isspace(c) && c!='\n'){
(gdb) next
11                      }else if(isdigit(c)){
(gdb) next
13                      }else if(isalpha(c)){
(gdb) next
14                              printf("is a letter!\n");
(gdb) next


可以看到每一步的操作,但是没有打印出变量c的值,可以通过下面的命令来查看
(gdb) watch c
Hardware watchpoint 2: c

这样每次循环都可以看到c的值,如下:
Old value = 2
New value = 97



更多详细用法可以参考:
http://www.huihoo.org/gnu/linux/gdb.html
分享到:
评论

相关推荐

    Linux GDB Debug -- Ebook in .doc --

    A e-book for C/C++ developer under UNIX/Linux system, that means if you are an GNU/GCC/GDB user, then this book is a nice one for you as primer book.

    Debugging With GDB中文版

    This is the Tenth Edition, of Debugging with GDB: the GNU Source-Level Debugger for GDB (GDB) Version 8.1.90.20180814-git. Copyright © 1988-2018 Free Software Foundation, Inc. Permission is granted...

    Eclipse C C++ GDB 编程及调试方法大全 Codeblocks编程指南

    Eclipse 本人整理的目前最全的资料:内涵Eclipse中文教程pdf,Codeblocks中文教程pdf Eclipse C/C++调试方法、文档、问题说明 CDT配置 GDb调试工具。另有Eclipse C、C++(CDT)开发文档pdf。绝对值!

    debugging with gdb中文带书签特别版.pdf

    debugging with gdb中文版,学习gdb必备

    Linux GDB调试文档

    使用GDB调试,英文版。对于从事 Linux C/C++ 后台开发的读者来说,GDB 调试是需要熟练掌握的一项技能

    gdb.exe下载,安装MinGW-w64缺少gdb32.exe

    安装MinGW-w64 下载有的要翻墙 这了里单独下载 放到bin里就行了, , 特别是vscode运行c/c++ debug模式 "D:\\Mingw-w64\\mingw64\\bin\\gdb.exe

    linux c gdb 调试工具

    linux C语言 程序开发 调试工具 linux下的编程少不了要debug,调试程序用

    gdb 培训材料 ppt

    自己根据搜索资料著作的一个一个GDB的培训的PPT,通过这个PPT可以掌握所有的使用gdb的调试技巧。另外,还添加了一些自己在工作中经常使用的一些命令的组合。

    动态分等级打印.Linux 程序开发打印 Debug 信息的使用技巧--C语言中几种输出调试信息的方法_精品文库_IThao12

    [摘要:Linux顺序开辟挨印Debug疑息的应用技能若何清楚明晰天挨印出顺序疑息,能够快速判别顺序运转环境,定位顺序出题目的中央。Linux 程序开发打印 D

    vscode-cpp-remote-debug:用于在VS Code中使用gdb和gdbserver测试C ++代码的远程调试的项目

    用于在VS Code中使用gdb和gdbserver测试C ++代码的远程调试的项目 先决条件 通过ssh可以访问的远程主机 gdbserver应该安装在远程主机上 远程主机上不需要root权限,普通用户权限就足够了 安装在本地计算机上的Visual...

    HP Eighteenth Edition, for GDB September 2008

    You can use GDB to debug programs written in C, C++ and Fortran. For more infor- mation, refer to the Section 9.4 [Supported languages], page 83. For more information on supported languages, refer to ...

    gdb手册整理双文档

    两个文档gdb应用的整理, 一个是基本的, 一个调试多进程的

    Debugging with GDB --2002年5.11

    s GDB and C s GDB features for C++ r q q file:///C|/gdb.html (3 of 352)19. 1. 2004 20:32:03 Debugging with GDB Modula-2 s Operators s Built-in functions and procedures s Constants s Modula-2 defaults ...

    gdb:可携带的 gdb 叉

    Apportable gdb fork 的主要区别在于添加了 Objective C 调试支持。 构建 gdb 的生产版本 git clone :apportable/gdb.git mkdir gdb_build cd gdb_build CFLAGS="-g -O2 -Wno-unused-value -Wno-unused-function...

    Debugging.with.gdb.中文版.pdf

    The GNU Debugger allows you to see what is going on ... GDB supports C, C++, Java, Fortran and Assembly among other languages; it is also designed to work closely with the GNU Compiler Collection (GCC).

    Debugging with GDB

    gdb英文文档,使用gdb可以在linux上面调试c、c++代码

    GDB调试和实践基本介绍.doc

    详细讲解C语言在预处理,编译, 链接, 汇编几个阶段的工作流程, 通过在Makefile中添加相关参数来进行Debug GDB调试, 讲解常用的调试指令和操作方法。

    C编译: 使用gdb调试

    它是一款UNIX平台的调试器(debugger),可用于为C, C++, Objective-C, Java, Fortran等程序debug。  在gdb中,你可以通过设置断点(break point)来控制程序运行的进度,并查看断点时的变量和函数调用状况,从而...

    浅析C语言调试器GDB和LLDB的使用方法

    调试器的使用 编译输出带调试信息的程序 调试信息包含:指令地址、对应源代码及行号 指令完成后,回调 LINUX使用GDB ...以上所述是小编给大家介绍的C语言调试器GDB和LLDB的使用方法,希望对大家有所帮助!

Global site tag (gtag.js) - Google Analytics