[Learning Record] GDB debugging learning

I have known that print is a good method before, but I am really tired. It is quite troublesome to configure IDE debugging. After learning GDB, I find it quite convenient. Some notes are as follows:

Main reference link (this information is very good): https://www.yanbinghu.com/2019/04/20/41283.html

  • Generate core files for debugging:
    Core files are not generated by default. You need to cancel the core generation restrictions. In addition, after cancellation, the core saving path is the default. I searched for a long time and couldn't find it. However, you can modify it to specify the path, as follows /proc/sys/kernel/core_pattern:
ulimit -c unlimited		# 取消限制
echo "./core-%e-%p-%t" > /proc/sys/kernel/core_pattern	# 修改保存路径到当前路径 ./

Reference link: https://blog.csdn.net/jiemashizhen/article/details/125016646
%p - insert pid into filename Add pid
%u - insert current uid into filename Add current uid
%g - insert current gid into filename Add current gid
%s - insert signal that caused the coredump into the filename Add the signal that caused the core
%t - insert UNIX time that the coredump occurred into filename Add the unix time when the core file was generated
%h - insert hostname where the coredump happened into filename Add the host name
%e - insert coredumping executable name into filename Add the command name The
apport service of Ubuntu22.04 will automatically rewrite this file every time it is powered on. If you do not use apport, you can set the enable setting of the configuration file /etc/default/apport Set to 0 to close the apport. (Not tested yet)

# 如果报权限问题,采用下面这句  
sudo sh -c 'echo "./core-%e-%p-%t" > /proc/sys/kernel/core_pattern'

Avoid 'Permission denied' when 'sudo echo x >'
A: Example sudo echo a > 1.txt-bash: 1.txt: Permission denied
B: Analysis: bash refuses to do this, saying it has insufficient permissions. This is because of redirection The symbol ">" is also a bash command. sudo only allows the echo command to have root permissions, but does not allow the ">" command to also have root permissions, so bash will think that this command does not have permission to write information.
C: Solution. Three types: 1. Use the "sh -c" command, which allows bash to execute a string as a complete command, thus extending the scope of sudo's influence to the entire command. The specific usage is as follows:sudo sh -c "echo a > 1.txt"

  • To clear the screen in the gdb interface: call the shell's clear:shell clear

  • Common instructions:
    Insert image description here

Guess you like

Origin blog.csdn.net/tfb760/article/details/129696078