gdb笔记 ---《Linux.C编程一站式学习》

单步执行和跟踪函数调用

函数调试实例

#include <stdio.h>

int add_range(int low, int high)
{
int i, sum;
for (i = low; i <= high; i++)
{
sum = sum + i;
}
return sum;
} int main(void)
{
int result[100];
result[0] = add_range(1, 10);
result[1] = add_range(1, 100);
printf("result[0] = %d\n result[1] = %d\n", result[0], result[1]);
return 0;
}

arrange_range函数从low加到high,程序打印的结果为:

                        <1>result[0] = 55

                        <2>result[1] = 5105

打印<1>结果正确,但<2>结果出错。<1>和<2>都是运行同一段代码,假如代码是错的,<1>却是正确的。

在阅读理解代码后还是找不出错误,可以使用gdb进行源码级的调试:

gdb进行源码级调试

编译时要加上-g的选项,生成可让gdb进行源码级调试的可执行文件。

-g选项作用:在可执行文件中加入源代码的信息,比如可执行文件中第几条机器指令对应源代码的第几行,在调试的时候确保gdb能找到源代码

没加-g选项:

$ gcc -g main.c -o main
$ gdb main
GNU gdb (Ubuntu 7.11-1-0ubuntu1~16.5) 7.11.1 //版本号
Copyright (C) 2016 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 configured " for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources onlice at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "aprops word" to search for command for commands related to "word"...//查找命令
"/home/lyl/Desktop/main.c": not in executable format: File format not recongnized //不可执行的格式:文件格式不可识别
(gdb)

gdb的help命令

(gdb) help
List of classes of commands: aliases -- Aliases of other commands //命令的别名
breakpoints -- Making program stop at certain points //断点:使程序停止在你需要停止的某些点上
data -- Examining data //检查数据
files -- Specifying and examining files //指定和检查文件
internals -- Maintenance commands //维护命令?翻译不知道是否正确
obscure -- Obscure features //提供编译,记录,监控等功能
running -- Running the program
stack -- Examing the stack //检查堆栈
status -- status inquireis //状态查询?
support -- Support facilities //支持的设备?
tracepoints -- Tracing of program execution whithout stopping the program //当程序没有停止的时候跟踪程序
user-defined -- 用户自定义命令 Type "help" followed by a class name for a list of commands i that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unmbiguous.

进一步查看某一类别有哪些命令

help files    #help + 类别

list命令:从第一行开始列出源代码

(gdb) list 1
1 #include <stdio.h>
2
3 int add_range(int low, int high)
4 {
5 int i, sum;
6 for(i = low; i <= high; i++)
7 sum = sum + i;
8 return sum;
9 }
10

gdb笔记 ---《Linux.C编程一站式学习》的更多相关文章

  1. GDB笔记

    GDB是在Linux命令行下对C/C++的程序进行调试常用的一个命令,现将平时记录在本子上的笔记整理如下: 一.断点 断点类型有breakpoints, watchpoints, catchpoint ...

  2. c语言,gdb

    Get gdb call stack http://blog.csdn.net/zoufeiyy/article/details/1490241 Debugging with GDB - Examin ...

  3. GDB调试基础

    GDB调试基础 https://lesca.me/archives/gdb-basic-knowledge.html GDB笔记(二):条件断点.命令列表.监视点 https://lesca.me/a ...

  4. GDB调试笔记

    参考资料:GDB调试精粹及使用实例 # 调试实例 #include <iostream> #include <cstring> using namespace std; ][] ...

  5. ubuntu: qemu+gdb 调试linux kernel 学习笔记

    声明: 本笔记内容并非本人原创,90%来自网络资料的整合.同时,由于自己是刚刚接触qemu & gdbserver remote debug,本文也就算不得教程,仅供有缘人参考而已. ---- ...

  6. 用gdb调试程序笔记: 以段错误(Segmental fault)为例

    用gdb调试程序笔记: 以段错误(Segmental fault)为例[转] 1.背景介绍2.程序中常见的bug分类3.程序调试器(如gdb)有什么用4.段错误(Segmental fault)介绍5 ...

  7. 《软件调试的艺术》学习笔记——GDB使用技巧摘要

    <软件调试的艺术>学习笔记——GDB使用技巧摘要 <软件调试的艺术>,因为名是The Art of Debugging with GDB, DDD, and Eclipse. ...

  8. 手把手教你使用eclipse+qemu+gdb来单步调试ARM内核【学习笔记】

    平台信息:linux4.0 平台:qemu 作者:庄泽彬 说明:笨叔叔的Linux视频的笔记 一.编译linux源码 export CROSS_COMPILE=arm-linux-gnueabi- e ...

  9. gdb使用笔记

    相关编译选项: 1.-g 开启gdb 2.-o0,o2  o0表示不优化, 3. -funsigned-char -fdata-sections 会使compiler为每个function和data ...

随机推荐

  1. jquery radio选择器 语法

    jquery radio选择器 语法 作用::radio 选择器选取类型为 radio 的 <input> 元素.大理石平台价格表 语法:$(":radio") jqu ...

  2. Java中截取字符串中小数点前面的字符

    通过下标获取 String number = "2563.2154"; int index = number.indexOf("."); String intN ...

  3. UVa 1595 Symmetry (set && math)

    题意:给出n个在直角坐标系上的点,问你能不能找出一条竖轴(即垂直于x的轴)使得所有的点根据这条轴对称,能则输出YES,否则输出NO 分析:首先需要找到对称轴的值,将所有n个点的x轴的值加起来然后去除以 ...

  4. Android Studio基本使用

    1.    创建Android项目 1)    Application name:应用名称,launcher界面显示的 2)    Company Domain:公司域名(sprd.com) 3)   ...

  5. Spring boot之热部署

    springboot热部署 1.springloaded(热部署) 2.devtools(热部署) 一.springloaded 问题的提出: 在编写代码的时候,你会发现我们只是简单把打印信息改变了, ...

  6. eclipse中把选中的代码全部变成大写或者小写的快捷键

    Ctrl+shift+x是把选中的变成大写 Ctrl+shift+y是把选中的变成小写

  7. python3笔记十三:python数据类型-Set集合

    一:学习内容 集合概念 集合创建 集合添加 集合插入 集合删除 集合访问 集合操作:并集.交集 二:集合概念 1.set:类似dict,是一组key的集合,不存储value 2.本质:无序和无重复元素 ...

  8. 【Spark机器学习速成宝典】模型篇08支持向量机【SVM】(Python版)

    目录 什么是支持向量机(SVM) 线性可分数据集的分类 线性可分数据集的分类(对偶形式) 线性近似可分数据集的分类 线性近似可分数据集的分类(对偶形式) 非线性数据集的分类 SMO算法 合页损失函数 ...

  9. leetcode-easy-array-136. Single Number

    mycode   75.80% class Solution(object): def singleNumber(self, nums): """ :type nums: ...

  10. CentOS 6.4 yum快速搭建Zabbix 2.2版本(中文)

    1.安装zabbix官方epel包 rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zabbix-release-2.2-1.el6. ...