zoukankan      html  css  js  c++  java
  • GCC,GDB学习

    1.GCC学习

    没有深入学习编译器连接器原理,就是单纯学习Linux下GCC编译C代码用法,主要参考GCC中文手册还有GCC官方tutorial,主要是明白GCC工作机制,练习几个程序慢慢熟练

    2.GDB学习

    ** 简单入门,主要目的是能会调试现在写的比较小的程序段,参考网上其他大佬的文章,和GDB中文手册练习**


    break -- 在指定的行或函数处设置断点,缩写为 b
    info breakpoints -- 打印未删除的所有断点,观察点和捕获点的列表,缩写为 i b
    disable -- 禁用断点,缩写为 dis
    enable -- 启用断点
    clear -- 清除指定行或函数处的断点
    delete -- 删除断点,缩写为 dt
    break -- 设置临时断点,参数同 break,但在程序第一次停住后会被自动删除
    watch -- 为表达式(或变量)设置观察点,当表达式(或变量)的值有变化时,暂停程序执行
    step -- 单步跟踪,如果有函数调用,会进入该函数,缩写为 s
    reverse-step -- 反向单步跟踪,如果有函数调用,会进入该函数
    next -- 单步跟踪,如果有函数调用,不会进入该函数,缩写为 n
    reverse-next -- 反向单步跟踪,如果有函数调用,不会进入该函数
    return -- 使选定的栈帧返回到其调用者
    finish -- 执行直到选择的栈帧返回,缩写为 fin
    until -- 执行直到达到当前栈帧中当前行后的某一行(用于跳过循环、递归函数调用),缩写为 u
    continue -- 恢复程序执行,缩写为 c
    print -- 打印表达式 EXP 的值,缩写为 p
    x -- 查看内存
    display -- 每次程序停止时打印表达式 EXP 的值(自动显示)
    info display -- 打印早先设置为自动显示的表达式列表
    disable display -- 禁用自动显示
    enable display -- 启用自动显示
    undisplay -- 删除自动显示项
    help -- 打印命令列表(带参数时查找命令的帮助),缩写为 h
    attach -- 挂接到已在运行的进程来调试
    run -- 启动被调试的程序,缩写为 r
    backtrace -- 查看程序调用栈的信息,缩写为 bt
    ptype -- 打印类型 TYPE 的定义


    转载:

    一、初始化

    输入gdb进入gdb调试环境。或者直接输入gdb + progfile来加载文件。
    注意该文件是使用gcc(或g++)编译得到的。为了使 gdb 正常工作, 必须
    使你的程序在编译时包含调试信息,编译时必须使用-g参数来。
    或者进入gdb环境后,通过命令file + progfile来加载需要调试的可
    执行文件文件。

    查看源代码:list [函数名][行数]

    设置程序运行参数:set args

    二、暂停程序

    gdb可以使用几种方式来暂停程序:断点,观察点,捕捉点,信号,线
    程停止。当程序被暂停后,可以使用continue、next、step来继续执行程序。
    continue 执行到下一暂停点或程序结束。
    next 执行一行源代码但不进入函数内部。
    step 执行一行源代码而且进入函数内部。

    1、设置断点:

    a、break + [源代码行号][源代码函数名][内存地址]
    b、break ... if condition ...可以是上述任一参数,condition
    条件。例如在循环体中可以设置break ... if i = 100 来设置循环次数。

    2、设置观察点:

    a、watch + [变量][表达式] 当变量或表达式值改变时即停住程序。
    b、rwatch + [变量][表达式] 当变量或表达式被读时,停住程序。
    c、awatch + [变量][表达式] 当变量或表达式被读或被写时,停住程序。

    3、设置捕捉点:

    catch + event 当event发生时,停住程序。event可以是下面的内容:
    1)、throw 一个C++抛出的异常。(throw为关键字)
    2)、catch 一个C++捕捉到的异常。(catch为关键字)
    3)、exec 调用系统调用exec时。(exec为关键字,目前此功能只在HP-UX下有用)
    4)、fork 调用系统调用fork时。(fork为关键字,目前此功能只在HP-UX下有用)
    5)、vfork 调用系统调用vfork时。(vfork为关键字,目前此功能只在HP-UX下有用)
    6)、load 或 load 载入共享库(动态链接库)时。(load为关键字,
    目前此功能只在HP-UX下有用)
    7)、unload 或 unload 卸载共享库(动态链接库)时。(unload为关
    键字,目前此功能只在HP-UX下有用)

    4、捕捉信号:

    handle + [argu] + signals
    signals:是Linux/Unix定义的信号,SIGINT表示中断字符信号,也就是
    Ctrl+C的信号,SIGBUS表示硬件故障的信号;SIGCHLD表示子进程状态改
    变信号; SIGKILL表示终止程序运行的信号,等等。
    argu:
    nostop 当被调试的程序收到信号时,GDB不会停住程序的运行,但
    会打出消息告诉你收到这种信号。
    stop 当被调试的程序收到信号时,GDB会停住你的程序。
    print 当被调试的程序收到信号时,GDB会显示出一条信息。
    noprint 当被调试的程序收到信号时,GDB不会告诉你收到信号的信息。
    pass or noignore 当被调试的程序收到信号时,GDB不处理信号。
    这表示,GDB会把这个信号交给被调试程序会处理。
    nopass or ignore 当被调试的程序收到信号时,GDB不会让被调
    试程序来处理这个信号。

    5、线程中断:

    break [linespec] thread [threadno] [if ...]
    linespec 断点设置所在的源代码的行号。如: test.c:12表示文件为
    test.c中的第12行设置一个断点。
    threadno 线程的ID。是GDB分配的,通过输入info threads来查看正在
    运行中程序的线程信息。
    if ... 设置中断条件。

    三、查看信息

    1、查看数据

    print variable 查看变量
    print *array@len 查看数组(array是数组指针,len是需要数据长度)
    可以通过添加参数来设置输出格式:
    /x 按十六进制格式显示变量。
    /d 按十进制格式显示变量。
    /u 按十六进制格式显示无符号整型。
    /o 按八进制格式显示变量。
    /t 按二进制格式显示变量。
    /a 按十六进制格式显示变量。
    /c 按字符格式显示变量。
    /f 按浮点数格式显示变量。

    2、查看内存

    examine /n f u + 内存地址(指针变量)
    n 表示显示内存长度
    f 表示输出格式(见上)
    u 表示字节数制定(b 单字节;h 双字节;w 四字节;g 八字节;默认为四字节)
    如:
    x /10cw pFilePath (pFilePath为一个字符串指针,指针占4字节)
    x 为examine命令的简写。

    3、查看栈信息

    backtrace [-n][n]
    n 表示只打印栈顶上n层的栈信息。
    -n 表示只打印栈底上n层的栈信息。
    不加参数,表示打印所有栈信息。

    4、info

    info address -- Describe where symbol SYM is stored
    info all-registers -- List of all registers and their contents
    info args -- Argument variables of current stack frame
    info auxv -- Display the inferior's auxiliary vector
    info breakpoints -- Status of user-settable breakpoints
    info catch -- Exceptions that can be caught in the current stack frame
    info checkpoints -- IDs of currently known forks/checkpoints
    info classes -- All Objective-C classes
    info common -- Print out the values contained in a Fortran COMMON block
    info copying -- Conditions for redistributing copies of GDB
    info dcache -- Print information on the dcache performance
    info display -- Expressions to display when program stops
    info extensions -- All filename extensions associated with a source language
    info files -- Names of targets and files being debugged
    info float -- Print the status of the floating point unit
    info forks -- IDs of currently known forks/checkpoints
    info frame -- All about selected stack frame
    info functions -- All function names
    info handle -- What debugger does when program gets various signals
    info line -- Core addresses of the code for a source line
    info linkmap -- Display the inferior's linkmap
    info locals -- Local variables of current stack frame
    info macro -- Show the definition of MACRO
    info mem -- Memory region attributes
    info proc -- Show /proc process information about any running process
    info program -- Execution status of the program
    info registers -- List of integer registers and their contents
    info scope -- List the variables local to a scope
    info selectors -- All Objective-C selectors
    info set -- Show all GDB settings
    info sharedlibrary -- Status of loaded shared object libraries
    info signals -- What debugger does when program gets various signals
    info source -- Information about the current source file
    info sources -- Source files in the program
    info stack -- Backtrace of the stack
    info symbol -- Describe what symbol is at location ADDR
    info target -- Names of targets and files being debugged
    info terminal -- Print inferior's saved terminal status
    info threads -- IDs of currently known threads
    info tracepoints -- Status of tracepoints
    info types -- All type names
    info variables -- All global and static variable names
    info vector -- Print the status of the vector unit
    info warranty -- Various kinds of warranty you do not have
    info watchpoints -- Synonym for ``info breakpoints''
    info win -- List of all displayed windows

    基本gdb命令:

    命令 简写 功能
    file 装入想要调试的可执行文件.
    kill k 终止正在调试的程序.
    list l 列出产生执行文件的源代码的一部分.
    next n 执行一行源代码但不进入函数内部.
    step s 执行一行源代码而且进入函数内部.
    continue c 继续执行程序,直至下一中断或者程序结束。
    run r 执行当前被调试的程序.
    quit q 终止 gdb.
    watch 使你能监视一个变量的值而不管它何时被改变.
    catch 设置捕捉点.
    thread t 查看当前运行程序的线程信息.
    break b 在代码里设置断点, 这将使程序执行到这里时被挂起.
    make 使你能不退出 gdb 就可以重新产生可执行文件.
    shell 使你能不离开 gdb 就执行 UNIX shell 命令.
    print p 打印数据内容。
    examine x 打印内存内容。
    backtrace bt 查看函数调用栈的所有信息。

    参考目录:
    1.http://blog.jobbole.com/107759/
    2.GDB完全中文手册
    3.https://blog.csdn.net/nancygreen/article/details/16962467
    4.Debugging with GDB
    5.Linux中国

  • 相关阅读:
    对Spring Boot 及Mybatis简单应用
    云时代架构读后感(十)
    云时代架构读后感(九)
    云时代架构读后感(八)
    云时代架构读后感(七)
    云时代架构读后感(六)
    关于mysql,sqlserverl,与oracle数据库连接总结
    云时代架构读后感(五)
    javaweb实现添加课程
    javaweb入门(使用SQLserver2008 R2数据库)
  • 原文地址:https://www.cnblogs.com/mark2018/p/9850230.html
Copyright © 2011-2022 走看看