In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what GDB is in the Linux system, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
GDB is the most commonly used program debugger on the Linux platform. Linux GDB debugging is a skill that must be mastered by CumberCraft + programmers.
1. Introduction to GDB: GDB (GNU symbolic debugger) is a GNU Project debugger that allows you to see what another program is doing during "execution"-or what it was doing when it crashed.
GDB can do four main things (and other things that support them) to help you catch errors in your behavior:
Start your program and specify everything that may affect its behavior.
Causes the program to stop under the specified conditions.
Check what happens when the program stops.
Change the contents of the program so that you can try to correct the impact of one error, and then move on to learn another.
These programs may be executed on the same computer as GDB (native), on another computer (remote), or on an emulator. GDB can run on most popular UNIX and Microsoft Windows variants as well as Mac OS X.
2. GDB basic debugging command 2. 1 prepares to add debugging information to the executable file through the-g option of gcc.
If you build a gcc-g hello.c-o hello1 using Makefile, you typically specify the-g option in CFLAGS.
CFLAGS: =-Wall-O2-G1 note that after adding optimization options to the GCC compiler, the actual execution order may be different from the source code order due to optimization, so using the debugger to trace the runtime can sometimes go to strange places, causing confusion.
Launch starts with the command gdb program name.
Set breakpoints can set breakpoints on function names, line numbers, and so on. The program is automatically suspended when it reaches the breakpoint. At this point, you can view the value of the variable at that time, display the stack frame, reset the breakpoint or rerun, and so on. The breakpoint command break can be abbreviated to b and the command is break. The breakpoint can be set by the function name, the line number within the current file, the file name before the line number, the offset from the pause position, or the address. Format:
Format description break sets breakpoints for specified functions in the currently executing file break sets breakpoints for specific lines in the currently executing file break file name: line number sets breakpoints for specified lines in the specified file The most commonly used breakpoint setting method break file name: function name sets breakpoint to the specified function of the specified file break current instruction line + /-offset sets breakpoint break specified address sets breakpoint set breakpoint can be confirmed by info break.
Running starts with the run parameter command, where the parameters are the parameters of the executable program. If a breakpoint is set, it executes to the location where the breakpoint is set and then pauses. It can be abbreviated to r.
The backtrace command, abbreviated as bt, can display stack frames when execution is suspended when a breakpoint or exception is encountered. In addition, backtrace has aliases such as where and info stack.
Format description bt displays all stack frames bt displays only the first N stack frames bt only shows the last N stack frames bt full displays not only backtrace, but also the local variable bt full
After displays the stack frame, you can see where the program stops and the calling path of the program.
The print command displays variables, which can be abbreviated to p. format: print variable.
2.7display register info registers can display registers, abbreviated as info reg. adds $before the register name to display the contents of the register, such as p $eax. The p / format $register can specify the display format of the register, such as pchart c $eax. The available formats are as follows:
Format description x display as hexadecimal number d display as decimal number u display as unsigned decimal number o display as octal number t display as binary number an address c display as character (ASCII) f floating point decimal s display as string I display as machine language, details are only available in x commands that display memory
The program pointer can be written as or eip, using p $pc to display the contents of the program pointer. The program pointer points to the address of the run point of the current program. The x command displays the contents of memory in the format: X /. For example, XUnix I $ps displays assembly instructions. When generally uses the x command, the format is x /. Here ADDR is the address you want to display, N is the number of repeats, F is the previous display format, and U represents the following units:
Unit description b byte h halfword (2 bytes) w word (4 bytes) (default) g double word (8 bytes) such as the command x\ 10i $pc displays 10 instructions starting from the address referred to by pc. disassembly command disassemble, abbreviated as disas. Format:
Disassemble . Disassemble the current entire function.
Disassemble program counters. The entire function of the function where the disassembler counter resides.
Disassemble start address end address. Disassemble the part from the start address to the end address.
The meaning of step execution is executed one line at a time according to the source code. The command that executes one line in the source code is next, abbreviated as n. If you encounter a function call during execution and want to execute inside the function, use the step command, abbreviated as p. if you want to execute assembly instructions one by one, you can use the nexti and stepi commands, respectively.
2.9 continue to run use the continue command to continue running the program, abbreviated as c. The program pauses again after it encounters a breakpoint. Use the continue command to specify the number of times breakpoints are ignored.
2.10 in monitoring large software or programs that use pointers heavily, it is difficult to find out where variables have been changed. To find out where variables have been changed, you can use the watch command (Watch, watchpoint). The format is as follows:
Watch . Pause when the expression changes.
Awatch . Pauses when the expression is accessed and changed.
Rwatch . Pauses running when the expression is accessed.
should be aware that setting a watch point may slow down the running speed.
2.11 remove breakpoints and monitoring points delete command deletes breakpoints and monitoring points, abbreviated as d. The format is delete, which means to delete the breakpoint or monitoring point indicated by the number, which can be viewed with the command info b. The clear command deletes a defined breakpoint. The available commands include: clear clear clear file name: line number clear file name: function name disable command disables breakpoints. The command format is as follows: disable: disable all breakpoints. disable: disables the specified breakpoint. disable display: disables the automatic display defined by the display command. disable mem: disables the area of memory defined by the mem command. The enable command enables breakpoints. The format of the command is as follows: enable enable enable once: enables the specified breakpoint only once. enable delete enable display enable mem
Other breakpoints hardware breakpoints (hbreak) are suitable for programs in areas of memory that cannot be modified, such as ROM space, and cannot be used in some architectures. temporary breakpoints (tbreak) and temporary hardware breakpoints (thbreak) are paused when running there, where the breakpoint is deleted, making it easy to use when it only needs to be stopped once.
2.13 change the value of the variable in format: set variable. For example, the command set variable options = 0 changes the value of the variable options to 0.
2.14 generate kernel dump files use generate-core-file to generate kernel dump files for debugging processes. View the running history when the dump file was generated through the kernel dump file and debug object. The gcore command generates kernel dump files directly from the command line. Use gcore pid on the command line, where pid is the process number. This command does not have to stop the running program to get the kernel dump file, and is useful when you need to analyze the cause of the problem separately on other machines, or when analyzing problems that occur at the customer site.
3. The biggest advantage of kernel dump kernel dump (core dump) is that it can save the state when a problem occurs. As long as there is a problem with the executable file and kernel dump of the program, you can know the current state of the process.
Enable kernel dump most Linux distributions turn off kernel dump by default. You can use the ulimit command to check whether the current kernel dump function is effective.
The $ulimit-c 012 -c option indicates the size limit of the kernel dump file, and 0 indicates that the kernel dump is invalid. Open the kernel dump to execute the command:
The upper limit of $ulimit-c unlimited or $ulimit-c 123 unlimited means that there is no limit on the size of the kernel dump file, and all the memory of the process can be dumped to the kernel dump file in the event of a problem. Alternatively, set the upper limit of the kernel dump file and specify the upper limit size in the parameters, in Kb. When an exception occurs in the program, a kernel dump file is generated in the current directory. For example, the program a.out generates the dump file core and starts GDB in the following ways:
$gdb-c core. / a.out1 uses GDB's list command to view nearby source code. Command usage
Format description list displays the source code around line linenum of the program list displays the source code of the function named function list displays the source code after the current line list-displays the source code set listsize settings before the current line displays the number of lines of the source code at a time show listsize views the settings list of the current listsize, displays the source code list from the first line to the last line Display the source code from the current line to the last line list + displays the source code 3.2 the full path to where the kernel dump dump is saved in a dedicated directory can be set through the sysctl variable kernel.core_pattern. The settings in the file / etc/sysctl.conf are as follows:
$cat / etc/sysctl.conf kernel.core_pattern = / var/core/%t-%e-%p-%c.core kernel.core_uses_pid = 0$ sysctl-p1234 in addition, you can modify the settings under / proc/sys/kernel. / proc/sys/kernel/core_uses_pid can control whether pid is added as an extension to the filename of the resulting core file, and if so, the file content is 1, otherwise it is 0. proc/sys/kernel/core_pattern can set the location or file name of the formatted core file, which can be modified as follows:
The format characters that can be set in $echo "/ corefile/core-%e-%p-%t" > core_pattern1 kernel.core_pattern are as follows:
Format character description%% character itself% p process dumped process ID (PID)% u real user of dumped process ID (real UID)% g Real group ID (real GID)% s triggered dump signal number% t dump time (seconds from 0:00 on 1970-1-1)% h hostname (same as nodename returned by uname (2))% e executable filename% c The upper limit of the size of the dump file (available after kernel version 2.6.24) 3.3.Use the user-mode helper to automatically compress the kernel dump file modification / etc/sysctl.conf by using the kernel.core_pattern variable.
$cat / etc/sysctl.conf kernel.core_pattern= | usr/local/sbin/core_helper% t% e% p% c kernel.core_uses_pid= 0$ sysctl-p1234 core_helper:
$cat usr/local/sbin/core_helper #! / bin/sh execgzip-> / var/core/$1-$2-$3-$4.core.gz1234 so that when a kernel dump occurs, a compressed kernel dump file is generated under / var/core.
3.4 enable kernel dump function for the whole system. Kernel dump function for all users can be enabled in / etc/profile file. Kernel dump is prohibited by default:
Ulimit-S-c 0 > / dev/null 2 > & 11 modifies it to
Ulimit-S-c unlimited > / dev/null 2 > & 11 the next step is to make the kernel dump function of the daemon started through the init script effective. Add a line of command to the / etc/sysconfig/init file.
DAEMON_COREFILE_LIMIT='unlimited'1 finally adds the following settings to / etc/sysctl.conf.
Fs.suid_dumpable=11 this setting allows programs that are SUID to be dumped by the kernel. By rebooting, you can enable kernel dumps for the entire system. did not find the / etc/sysconfig folder in the Ubuntu, Debian, and portable embedded Linux systems I used. The file / etc/security/limits.conf is modified, as prompted by the contents of the file, to make the unlimited permanent.
3.4 use kernel dump mask to exclude shared memory if multiprocess programs use a large amount of shared memory, all the shared memory of all processes will be dumped during kernel dump, which will put great pressure on the disk, and the dump process will also increase the load on the system. Because the contents of the shared memory are the same, you only need to dump the shared memory in a process. is set through / proc//coredump_filter. Coredump_filter uses a bit mask to represent the memory type. As follows:
Bit mask memory type bit 0 anonymous private memory bit 1 anonymous shared memory bit 2file-backed dedicated slave 3file-backed shared memory bit 4ELF file mapping (available after kernel version 2.6.24) to skip all shared memory segments, change the value to 1.
4, GDB debugging skills 4.1attach to the process to debug a process that has already been started, or to debug a process that is stuck in a dead loop and cannot return to the console, you can use the attach command. Format: attach, execute this command to attach to a process whose ID is pid. After attach, you can use the normal gdb command. When gdb is separated from the process, the detach command is used, and the debugged process is released from the control of gdb. The process continues to run after being detach. process information can be displayed with the info proc command. After starting the child process, the Guardian process automatically shuts down the main process. If the monitoring mode is not set, gdb will prompt you to disconnect from the process. Therefore, the monitoring object must be set and the command must be set to set follow-fork-mode child/parent.
4.2 conditional breakpoint break if, this command tests the given condition and pauses if it is a frame. if the breakpoint already exists, the condition command adds a trigger condition to the breakpoint, and the condition command removes the trigger condition for the specified numbered breakpoint.
Execute ignore repeatedly: ignore the specified number of times at the specified breakpoint, monitor point, or capture point. continue: do not pause until the power is cut off until the specified number of times is reached. s/stepi/n/nexti: executes the corresponding command for the specified number of times. finish: pauses after the execution of the current function. until: pauses after the execution of code blocks such as the current function, or, in the case of a loop, after the execution of the loop, which is often used to jump out of the loop. until: execution stops at the specified address.
The commands command defines the commands that are executed automatically after the breakpoint terminal. The format is as follows:
(gdb) commands... The history of end12344.5 values values displayed by the print command are recorded in the internal value history. These values are referenced by $, and the last 10 values in history can be displayed using the show value command.
Variable description the last value of $value history $n value history nth value $$value history penultimate value $n value history penultimate value $_ x command displayed last address $_ x command displayed last address value $_ eexitcode program return code $bpnum last set breakpoint number can also define variables at will, starting with $ It consists of English letters and numbers. For example:
(gdb) set $iTun0 (gdb) p $I $I = 01234.6 Command History show history saves the command history to a file, and the default command history file is located in. / .gdb _ history.
set history expansion show history expansion
can use csh style! Characters.
set history filename show history filename
saves the command history to a file. You can change the default file name through the environment variable GDBHISTFILE.
set history save show history save
enables the ability to save command history to file and restore.
set history size show history size
sets the number of commands saved to the command history. The default value is 256.
4.7 initialization file (.gdbinit) the initialization file in the Linux environment is .gdbinit. If a .gdbinit file exists, GDB runs it as a command file before starting it. The initialization file and the command file are run in the following order.
1. $HOME/.gdbinit.
2. Run the command line option.
3 、. / .gdbinit .
4. The command file given through the-x option.
The syntax of the initialization file is the same as the command file. The define command allows you to customize the command, the document command adds a description to the custom command, and the GDB runtime uses help to view the defined commands. Examples are as follows:
In addition to initializing files, define li Xamp 10i $pc end document li list machine instruction end123456 can also write settings in files that you can read when you run GDB. The command is source, for example:
(gdb) source gdbcalc (gdb) p $log10 (10000.0) $1 = 4123 where the gdbcalc file is as follows:
#! / usr/bin/gdb-x file / usr/bin/gdb start set $e = 2.71828284590452354 set $pi = 3.14159265358979323846 set $fabs = (double (*) (double)) fabs set $sqrt = (double (*) (double)) sqrt set $cbrt = (double (*) (double)) cbrt set $exp = (double (*) (double)) exp set $exp2 = (double (*) (double)) exp2 set $exp10 = (double (*) (double)) exp10 set $log = (double (*) (double)) Log set $log2 = (double (*) (double)) log2 set $log10 = (double (*) (double)) log10 set $pow = (double (*) (double Double)) pow set $sin = (double (*) (double)) sin set $cos = (double (*) (double)) cos set $tan = (double (*) (double)) tan set $asin = (double (*) (double)) asin set $acos = (double (*) (double) acos set $atan = (double (*) (double)) atan set $atan2 = (double (*) (double)) Double)) atan set $sinh = (double (*) (double)) sinh set $cosh = (double (*) (double)) cosh set $tanh = (double (*) (double)) tanh set $asinh = (double (*) (double)) asinh set $acosh = (double (*) (double) acosh set $atanh = (double (*) (double)) atanh223456789101112131415161718192021222324252627285, summary of common GDB commands and abbreviations are as follows:
Command abbreviated form description backtracebt, where display backtracebreak setting breakpoint
Continuec, cont continue to run deleted delete breakpoint finish run to the end of the function
Info breakpoints displays breakpoint information
Nextn executes the next line printp display expression runr runtime steps executes one line at a time, including the internal x of the function to display the memory content
Untilu executes to the specified line directorydir insert directory disabledis disable breakpoint downdo Select stack frame to display edite edit file or function framef Select stack frame to display forward-searchfo forward search generate-core-filegcore generation kernel dump helph display help list infoi display information listl display function or line nextini execute the next line (in assembly code) print-objectpo display target information sharedlibraryshare load shared library The symbol setpisi executes the next line to display details
The above is all the content of the article "what is GDB in Linux system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.