All notes
Gdb

# Breakpoints

break test.c:1	# Set breaks
info break [n]	# Print break pionts.
delete 1	# Delete the first breakpoint.
watch gvar	# Set a watchpoint on a variable when it is written to.

# Save all current breakpoints to a file.
save breakpoints filename
# Read the saved breakpoint definitions.
source filename

# list current line.
list *$pc info args/locals # Show the arguments and local variables for the current frame. info registers info types REGEXP # Show information about the current source file: info source # Print the names of all source files in your program: info sources # Print the names and data types of all defined functions: info functions info functions REGEXP # Look up information for a type Point by name, in detail. ptype Point # Just print the type info. whatis Point # looks up the value of x' in the scope of the file foo.c'. p 'foo.c'::x set case-sensitive on/off/auto show case-sensitive # SourceWare. p var # Print variable content. p/x var # Print var in hex format. p/t var # Print var in binary format. ‘t’ stands for “two”. p/d # Print as integer in signed decimal. p/u # Print as integer in unsigned decimal. p/o # Print as integer in octal. p/c # Print as char. p/f # Print as float. p/s var # Print as string. p/r # Print as raw. display var # Display the var every time you stop. # x, examing memory. # SourceWare. x addr set$foo = 5
set env DEBUG 1 # Set environment variables for process before launching.

finish	# Step out current frame.
step	# Do a source-level step in the currently selected thread.
next	# Do a source-level step over in the currently selected thread.
stepi	# Do an instruction-level step in currently selected thread.
nexti	# Do an instruction-level step over in currently selected thread.

# Evaluate expression, which may include functions.
print EXPR
call EXPR
# You can cancel execution of a function call with the value "EXPRESSION" returned.
return EXPRESSION
# Give a signal. SIGNAL could be, for example, SIGINT.
signal SIGNAL

bt	# Show backtrack.
bt 5	# Backtrace the first five frames of the current thread.
up/down	# Select the upper/lower stack frame from the current frame.

# Save binary memory data to a file starting at 0x1000 and ending at 0x2000.
dump memory /tmp/mem.bin 0x1000 0x2000

# Attach GDB to a process
attach pid

gdb --args executablename arg1 arg2 arg3

gdb exeName coreFile


# Core dump

Stackoverflow.  

# In BASH:
ulimit -c unlimited

# In tcsh
limit coredumpsize unlimited

 

"The reason why core dumps are not generated by default is that the limit is not set and/or set to 0, which prevents the core from being dumped. By setting a limit of unlimited, we guarantee that core dumps can always be generated."

• For help on dump core: man core.

#### Cent OS

/proc/sys/kernel/core_pattern is used to specify a core dumpfile pattern name.

If the first character of the pattern is a '|', the kernel will treat the rest of the pattern as a command to run. The core dump will be written to the standard input of that program instead of to a file.

• Change the content of the core_pattern to: core.%e.%p, then it will create core dump file under working directory with name like "core.exeName.pidNumber". NOTE: you have to use echo instead of vim, see below.
• Easwy. /proc文件系统的内容是kernel映射出来的内存内容，并不在真实文件系统上，所以root权限也不能vim编辑，只能echo进去。
• 可以在core_pattern模板中使用变量还很多，见下面的列表：
%% 单个%字符
%p pid
%u uid
%g gid
%s signal number
%t UNIX time for core dump (由1970年1月1日计起的秒数)
%h hostname
%e executable

如果在上述文件名中包含目录分隔符”/“，那么所生成的core文件将会被放到指定的目录中。
• Wordpress. To make it permanent, modify in "/etc/sysctl.conf":
kernel.core_pattern=/tmp/cores/core.%e.%p.%h.%t


# FAQ

## GDB find no symbol

The culprit turned out to be a mismatch between the compiler and debugger! I had recently switched to GCC 4.8, a newer version. However, I was still using the old version of GDB. In v4.8, GCC produces debugging information in the DWARF4 format, as explained in its release notes. However, this older version of GDB could only understand DWARF3 or versions older than that. It was failing silently when local variables were requested.

The solution was simple: request GCC to produce debugging information in the older format by specifying -gdwarf-3. Once I did this, GDB could print local variables and they appeared in Eclipse CDT too.


GNU. As noted in the release notes for GCC 4.8 (i.e. current trunk):

DWARF4 is now the default when generating DWARF debug information. When -g is used on a platform that uses DWARF debugging information, GCC will now default to -gdwarf-4 -fno-debug-types-section. GDB 7.5, Valgrind 3.8.0 and elfutils 0.154 debug information consumers support DWARF4 by default. Before GCC 4.8 the default version used was DWARF2. To make GCC 4.8 generate an older DWARF version use -g together with -gdwarf-2 or -gdwarf-3. The default for Darwin and VxWorks is still -gdwarf-2 -gstrict-dwarf.