Sometimes running program in Unix will fail without any debugging info or warnings because of the laziness of programmer.. So if need to find the right place where the program stops and start debugging right from there, set the coredump file size to some desired size by first checking ..:

udump -a | grep core

[root@os-t1 ~]# ulimit -a | grep core

core file size          (blocks, -c)

[root@os-t1 ~]#

Use the following command to set the limit of coredump file size:

ulimit -c unlimited

[root@os-t1 ~]# ulimit -c unlimited

[root@os-t1 ~]# ulimit -a | grep core

core file size          (blocks, -c) unlimited

[root@os-t1 ~]#

Make sure the program is generated by gcc/g++ using flags of -O0 and -g. After running the program that will fail will generate the Core.[0-9]+ file after exit, this file contains the stack/mem/register infos.

use gdb to restore the environment when the bug appears and program exits:

gdb program core.[0-9]+

gdb will point to the line will the program exits, use bt to view the stack infos, use up to get to the upper entry of the stack. Easy to find the problem in the program we write.

normal gdb debugging:

gdb set watch points, uses watch, rwatch, awatch, see this passage. note that if rerun the program by r, all the set watchpoints are outdated, unlike the normal breakpoints. use inf b to see all the watchpoints/breakpoints.

rwatch/awatch uses hardware detection, so only powerpc/x86 based machine are able to set these points, and in a limited number. use watch expr mask 0xff00 to watch some bits.

gdb expressions:

print construct gdb arrays at given len: p *lpstr@len

print certain var in function foo scope: p foo::var

catch points: catch signal ~

gdb handles exceptions: handle ~ keywords

condition stop: if expr true then stop, condition breakpointNB expr, the expr is evaluated when gdb running to the script, rather than right at the command is given. if condition breakpointNB is typed again, the condition is removed

another condition is to ignore the breakpoint for a given times, using ignore breakpointNB count to do it. only apply to bp, wp, cp.. [not tracepoints.. then .. what is tp?..

breakpoint & commands to execute:

.. if some breakpoints are activated.. some commands are able to be defined to run .. e.g. print values, enable other breakpoints..

format is:

commands [bp list]

command lists..

..

end

the last end ends the command list. if need to delete all defined commands for bps, using command \nend

the command list after resuming the program will be ignored.

     break foo if x>
commands
silent
printf "x is %d\n",x
cont
end

silent is to remove the message which says the bp is reached.. and can only be put at the first line of the command snippet.

this technique can also be used to pass the vars some desired value when debugging, and resume debugging for the rest codes.. like ..

     break
commands
silent
set x = y +
cont
end

Dynamic print:

adding printf expression while debugging (other than a breakpoint) can use:

dprintf location,template,expression[,expression...]

where location is line number, template is "%d" like, expr is vars. before using it, some configurations should be set:

set dprintf-style gdb/call/agent
set dprintf-function printf/fprintf/printk..
set dprintf-channel channel/stdout/stderr/pfile..

the 2nd command can be used only when using style-call. the 3rd command can be used in fpf as the first argument as the dest channel, e.g.

          (gdb) set dprintf-style call
(gdb) set dprintf-function fprintf
(gdb) set dprintf-channel mylog
(gdb) dprintf ,"at line 25, glob=%d\n",glob
Dprintf at 0x123456: file main.c, line .
(gdb) info break
dprintf keep y 0x00123456 in main at main.c:
call (void) fprintf (mylog,"at line 25, glob=%d\n",glob)
continue
(gdb)

using save breakpoints file to save all the bps, for later loading

loading settings in gdb, always use source file.

adding breakpoints in a ambiguous way..

     (gdb) b String::after
[] cancel
[] all
[] file:String.cc; line number:
[] file:String.cc; line number:
[] file:String.cc; line number:
[] file:String.cc; line number:
[] file:String.cc; line number:
[] file:String.cc; line number:
>
Breakpoint at 0xb26c: file String.cc, line .
Breakpoint at 0xb344: file String.cc, line .
Breakpoint at 0xafcc: file String.cc, line .
Multiple breakpoints were set.
Use the "delete" command to delete unwanted
breakpoints.
(gdb)

set multiple-symbols ask/all, if ask, the above is done, if all, automatically choose [1]all.

tui usages:

enter tui: c-x c-a  || c-x s || tui enable/disable

cls: c-l

commands:

c d f n q r s u v w

c/f/n/q/r/s: continue/fin/nex/quit/run/step

d/u/v/w: down/up/view/where

lay src/asm/split/regs

GDB free chars:-----

etyiopaghjkl;0-9'zxbm,./?`~-=_+

to break at conditions:

# This breaks when char* s satisfies strcmp(s, "hello")== at lineNo.
break lineNo if strcmp(s, "hello")==
# This breaks when i==
break lineNo if i==
# Anything legal in the context at lineNo can be put after if..
break lineNo if selfdefinedfunction()==true

gdb also has a condition syntax.. to quick add conditions..

# This is the same as the above line..
break lineNo
# # A break point numbered N has been set
condition N strcmp(s,"hello")==

set breakpoints for a certain thread.. check current thread use command thr

# First checkout all existing threads
info thread
# then break
b lineNo/funcname (if ...) thread id

gdb may set convenient vars: some more gdb-defined c-vars can be viewed here. (Some not available till recent version of gdb ..)

set $foo = *object_ptr
# Using a convenience variable for the first time creates it, but its value is void until you assign a new value. You can alter the value with another assignment at any time.
show convenience
init-if-undefined $variable = expression
set $i =
print bar[$i++]->contents

gdb can also debug several programs/processes at a time, as inferiors..further info be viewed here.

gdb can run process in background mode, by continue& ..

debug one/several threads while others keep running, other than stop together with the ones being debugged, by: (default off)

# If using the CLI, pagination breaks non-stop.
set pagination off # Finally, turn it on!
set non-stop on show non-stop # To revert this setting
set non-stop off

use gdbserver to enable remote debugging: (why?) sometimes need local gdbinit? or ?

# target:
gdbserver ip:port prog
# host:
gdb
> target remote ip:port
> continue

---------

Sometimes need to debug into python third-party libraries.. and without source code.. If with source code, it is easy to debug python with pdb, however if without source code, we only can debug in byte-code form with installed python-debuginfo (centos). By:

gdb python pid

GDB: advanced usages的更多相关文章

  1. Docker container network configuration

    http://xmodulo.com/networking-between-docker-containers.html How to set up networking between Docker ...

  2. ASP.NET(支持 ASP.NET MVC)性能优化包 - combres 介绍

    项目地址:https://github.com/buunguyen/combres Overview Combres (previously hosted in CodePlex) helps you ...

  3. How to Build a Search Page with Elasticsearch and .NET

    Although SQL Server's Full-Text search is good for searching text that is within a database, there a ...

  4. Spring 4 MVC+Apache Tiles 3 Example

    In this post we will integrate Apache Tiles 3 with Spring MVC 4, using annotation-based configuratio ...

  5. notes: the architecture of GDB

    1. gdb structure at the largest scale,GDB can be said to have two sides to it:1. The "symbol si ...

  6. emacs semantic,speedbar,gdb汇总

    在emacs 里使用gdb emacs speedbar功能介绍 semantic功能介绍 上面3篇文章精华,都汇总到下面的.emacs文件里了. ;;启动semantic功能 (semantic-m ...

  7. linux应用调试技术之GDB和GDBServer

    1.调试原理 GDB调试是应用程序在开发板上运行,然后在PC机上对开发板上得应用程序进行调试,PC机运行GDB,开发板上运行GDBServer.在应用程序调试的时候,pc机上的gdb向开发板上的GDB ...

  8. 新手如何在gdb中存活

    网络上已经有很多gdb调试的文章了,为什么我还要写这篇文章呢,因为本文是写给gdb新手的,目的就是通过一个简单的例子来让新手很快上手.一旦上手入门了,其他的问题就可以自己去搜索搞定了.右边是gdb的L ...

  9. GDB 多线程调试:只停止断点的线程,其他线程任然执行; 或只运行某些线程 其他线程中断

    多线程调试之痛 调试器(如VS2008和老版GDB)往往只支持all-stop模式,调试多线程程序时,如果某个线程断在一个断点上,你的调试器会让整个程序freeze,直到你continue这个线程,程 ...

随机推荐

  1. ap143 led修改

    以前的硬件版本只使用了一个eth口,新的板子增加了一个eth口,并且增加了响应的通信时使用的灯. led修改涉及到一下的内容: (1)led 对应的gpio口(这个需要根据原理图来定义) 需要修改的源 ...

  2. String、StringBuffer和StringBuilder区别及性能分析

    1.性能比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuilder)的原因 S ...

  3. NOIP2010-普及组初赛C语言解析

    第十六届全国青少年信息学奥林匹克联赛初赛试题 一.单项选择题 (共20题,每题1.5分,共计30分.每题有且仅有一个正确选项.) 1.2E+03表示(  D  ). A.2.03          B ...

  4. 【转载】__name__ == "__main__": 你认识我么?

    <笨方法学Python>的习题50中,脚本ex50.py最后一行代码就是 if __name__ == "__main__": 那么,这句代码到底有什么用呢? 莫急莫急 ...

  5. java上传并下载以及解压zip文件有时会报文件被损坏错误分析以及解决

    情景描述: 1.将本地数据备份成zip文件: 2.将备份的zip文件通过sftp上传到文件服务器: 3.将文件服务器上的zip文件下载到运行服务器: 4.将下载的zip文件解压到本地(文件大小超过50 ...

  6. rebase

    /BASE (Base Address) https://msdn.microsoft.com/en-us/library/f7f5138s.aspx Need for Rebasing a DLL( ...

  7. Python--变量作用域

    变量作用域: 一般在函数体外定义的变量成为全局变量,在函数内部定义的变量称为局部变量. 全局变量所有作用域都可读,局部变量只能在本函数可读 函数在读取变量时,优先读取函数本身自有的局部变量,再去读全局 ...

  8. 老司机的奇怪noip模拟T1-guanyu

    1. 关羽(guanyu.cpp/c/pas )[问题描述]xpp 每天研究天文学研究哲学,对于人生又有一些我们完全无法理解的思考.在某天无聊学术之后, xpp 打开了 http://web.sang ...

  9. 第七十四节,css边框与背景

    css边框与背景 学习要点: 1.声明边框 2.边框样式 3.圆角边框  本章主要探讨HTML5中CSS边框和背景,通过边框和背景的样式设置,给元素增加更丰富的外观. 声明边框 边框的声明有三个属性设 ...

  10. [JS思路]运动框架思路

    匀速运动的思路一: 1.先清除动画,再加载动画 2.方向dir有正值和负值,可以通过 目标值 > 当前值  往右移动,即正数 目标值 < 当前值  往右移动,即负数 来进行判断:dir = ...