GDB debugging essence and examples

Source: Internet
Author: User
Tags signal handler
GDB debugging essence and use Example 1: List of file columns 1. list (gdb) listline1, line2 2: If you want to run the program to prepare for debugging, you can run the run command, which can be followed by any parameters sent to the program, including standard input and standard output specifiers (& lt; and & gt;) and Shell GDB debugging essence and use Example 1: column file list 1. list (gdb) list line1, line2 2: to run the program to prepare for debugging, run the run command, which can be followed by any parameters sent to the program, including standard input and standard output specifiers ( <和> ) And Shell wildcard (*,?. If you use the run command without parameters, gdb will use the parameter you gave the previous run command again, which is very useful. You can use the set args command to modify the parameters sent to the program, and use the show args command to view the list of its default parameters. The (gdb) set args-B-x (gdb) show args backtrace Command provides the backward tracing function for the stack. The Backtrace command generates a list that contains valid processes starting from the most recent process and parameters for calling these processes. 3. use the print command to check the values of each variable. (Gdb) print p (p is the variable name) the whatis command can display the type of a variable (gdb) whatis p type = int * print is a very powerful command of gdb, it can be used to display any valid expressions in the language to be debugged. In addition to variables in your program, expressions can also contain the following: l call the functions in the program (gdb) print find_entry () l data structure and other complex objects (gdb) print * table_start $8 = {e = reference = '\ 000', location = 0x0, next = 0x0} historical components of the l value (gdb) print $1 ($1 is a history variable and can directly reference the value of $1 in the future) an array provides a method for displaying the content of a storage block (array section or dynamically allocated storage area. Early debugging programs did not have a good way to replace arbitrary pointers with an array. Just like treating parameters, let's look at the 10 integers after the variable h in the memory. the syntax of a dynamic array is as follows: base @ length therefore, to display the 10 elements after h, use h @ 10: (gdb) print h @ 10 $13 = (-1,345, 23,-234,0, 98,345, 10) 4. the breakpoint (breakpoint) break command (which can be abbreviated as B) can be used to set breakpoints in a program to be debugged. The command has the following four forms: l break line-number causes the program to stop just before the specified row is executed. L break function-name causes the program to stop before entering the specified function. L break line-or-function if condition if the condition is true, the program stops when it reaches the specified row or function. L break routine-name sets a breakpoint at the entrance of the specified routine. if the program is composed of many original files, you can set a breakpoint in each original file, instead of setting a breakpoint in the current original file, the method is as follows: (gdb) break filename: line-number (gdb) break filename: function-name to set a condition breakpoint, you can use the break if command as follows: (gdb) break line-or-function if expr example: (gdb) break 46 if testsize = 100 to continue running from the breakpoint: countinue command 5. breakpoint management 1. display the current gdb breakpoint Information: (gdb) info break it will display all the breakpoint information in the following form: Num Type Disp Enb Address What 1 breakpoint keep y 0x0000 28bc in init_random at qsort2.c: 155 2 breakpoint keep y 0x0000291c in init_organ at qsort2.c: 168 (gdb) 2. delete a specified breakpoint: (gdb) delete breakpoint 1 This command will delete the breakpoint numbered 1. if no number parameter is specified, all breakpoints (gdb) will be deleted) delete breakpoint 3. disable the use of a breakpoint (gdb) disable breakpoint 1 This command will disable breakpoint 1, and the breakpoint information (Enb) domain will change to n 4. allow a breakpoint (gdb) enable breakpoint 1 This command will allow breakpoint 1, while the breakpoint information (Enb) field will change to y 5. clear all breakpoints (gdb) clean number on a code line in the original file. note: number indicates the row number of a code line in the original file. change Quantity check and value assignment l whatis: identifies array or variable type l ptype: is more powerful than whatis, it can provide a structure definition l set variable: assign the value to variable l print. in addition to displaying the value of a variable, it can also be used to assign values to variable l print. one-step execution l next does not enter one-step execution l step to enter the single-step execution if a function has been entered, and wants to exit the function and return to its calling function, run the command finish 8. function call l call name call and execution of a function (gdb) call gen_and_sork (1234,1, 0) (gdb) call printf ("abcd ") $1 = 4 l finish the execution of the current function, and display the returned value (if any) 9. the machine language tool has a set of special gdb variables that can be used to check and modify general registers of a computer. gdb provides the standard names of the four registers actually used in each computer: l $ pc: Cheng Sequence counter l $ fp: Frame Pointer (current stack frame) l $ sp: stack pointer l $ ps: Processor status 10. signal gdb can usually capture most of the signals sent to it. By capturing the signals, it can decide what to do for running processes. For example, sending an interrupt signal to gdb by CTRL-C usually terminates gdb. However, you may not want to interrupt gdb. the real purpose is to interrupt the program running in gdb. Therefore, gdb needs to capture the signal and stop the program running, in this way, you can perform some debugging operations. The Handle command can control signal processing. It has two parameters: one is the signal name and the other is what to do when the signal is received. Several possible parameters are as follows: when nostop receives a signal, do not send it to the program or stop the program. L stop stops program execution when the signal is received to allow program debugging; displays a message indicating that the received signal is received (except for messages prohibited) l print displays a message when receiving the signal l noprint does not display the message when receiving the signal (and implicitly does not stop running the program) l pass sends the signal to the program, this allows your program to process it, stop running, or take other actions. L nopass stops running the program, but do not send signals to the program. For example, assume that you intercept the SIGPIPE signal to prevent the program being debugged from receiving the signal. Once the signal arrives, the program is required to stop and notify you. To complete this task, you can use the following command: (gdb) handle SIGPIPE stop print. Note that UNIX signal names always use uppercase letters! You can use the signal number to replace the signal name. if your program wants to perform any signal processing operations, you need to be able to test its signal processing program, A simple method is required to send signals to the program, which is the task of the signal Command. The parameter of this command is a number or name, such as SIGINT. Assuming your program has set a dedicated SIGINT (keyboard input, or CTRL-C; Signal 2) signal handler to capture a cleanup action, to test the signal handler, you can set a breakpoint and run the following command: (gdb) signal 2 continuing with signal SIGINT (2) the program continues to run, but the signal is transmitted immediately and the processing program starts to run. 11. search text of the original file: this command displays the next line containing the text string in the current file. Reverse-search text: this command can display the first line containing text. 12. the UNIX interface shell command can start the UNIX shell, the CTRL-D exit the shell, return to gdb. 13. command history in order to allow the use of historical commands, you can use the set history expansion on command (gdb) set history expansion on summary: the commonly used gdb command backtrace shows the current position in the program and stack trace indicating how to reach the current position (synonym: where) when breakpoint sets a breakpoint cd in the program to change the current working directory clear to delete the breakpoint commands that just stopped hitting the breakpoint, list the commands to be executed. continue starts from the breakpoint and continues to execute delete to delete a breakpoint or a monitoring point. you can also use the display program with other commands to show variables and lower down the stack frame when the display program is stopped, this makes another function the frame info of the current function frame to select the next continue command to display the parameters related to the program. Information: jump another point in the source program to start running kill exception termination program list running under the gdb control list corresponding to the original file content of the program being executed next to execute the next source code line, therefore, execute a print function to display the value of the variable or expression pwd to display the current working directory pype to display a data structure (such as a structure or C ++ class) content quit exit gdb reverse-search in the source file reverse search regular expression run execute this program search regular expression set variable in the source file search variable value signal send a signal to the running process step: execute the next source code line, if necessary, enter the reverse command of the undisplay display command of the next function. do not display the expression until to end the current loop up to move the stack frame up, make another function the current function watch sets a monitoring point (that is, a data breakpoint) in the program whatis display variable or function Number type ************************************** * ************* GNU Debugger is called gdb, this program is an interactive tool that works in character mode. In the X Window System, there is a gdb front-end graphics tool called xxgdb. Gdb is a powerful debugging program that can complete the following debugging tasks: * setting breakpoints; * monitoring program variable values; * one-step execution of programs; * modifying variable values. Before you can use gdb to debug a program, you must use the-g option to compile the source file. You can define the CFLAGS variable in makefile: CFLAGS =-g. the following command is often used when running the gdb debugging program: gdb progname: enter help at the gdb prompt to classify the listed commands, the main categories are: * aliases: Command alias * breakpoints: breakpoint definition; * data: data viewing; * files: specify and view files; * internals: Maintenance Command; * running: program execution; * stack: Call stack view; * statu: status view; * tracepoints: Trace program execution. Enter help and the classification name of the command to obtain a detailed list of such commands. The common commands of gdb explain that break NUM sets breakpoints on the specified line. Bt displays all call stack frames. This command can be used to display the call sequence of a function. Clear deletes a breakpoint set on a specific source file or line. Its usage is clear FILENAME: NUM continue. This command is used when the program stops running due to processing signals or breakpoints. Display EXPR: the expression value is displayed after each program is stopped. An expression is composed of variables defined by the program. File FILE to load the specified executable file for debugging. Help NAME: displays the help information of a specified command. Info break displays the current breakpoint list, including the number of times the breakpoint is reached. Info files: displays detailed information about the file to be debugged. Info func shows all function names. Info local shows the local variable information in the function. Info prog displays the execution status of the program to be debugged. Info var displays all global and static variable names. Kill the program being debugged. List shows source code segments. Make runs the make tool without exiting gdb. Next executes a source code line forward without entering other functions. Print EXPR: the value of the expression EXPR. * ******************************** --------------- List a C with an error source code bugging. c code: ----------------- 1 # I nclude 2 3 static char buff [256]; 4 static char * string; 5 int main () 6 {7 printf ("Please input a string: "); 8 gets (string); 9 printf (" \ nYour string is: % s \ n ", string); 10} --------------- the above program is very simple, the purpose is to accept user input and print the user input. This program uses an uninitialized string address string. Therefore, after compilation and running, the Segment Fault error will occur: $ gcc-o bugging-g bugging. c $. /bugging Please input a string: asfd Segmentation fault (core dumped) to find problems in this program, we use gdb and follow the steps below: 1. run the gdb bugging command to load the executable bugging file. 2. run the loaded bugging command; 3. use the where command to view the errors in the program. 4. use the list command to view the code near the call to The gets function; 5. the only cause of the gets function error is the variable string. Use the print command to view the string value; 6. in gdb, we can directly modify the value of the variable. we only need to take a valid pointer value for the string. Therefore, we can set the breakpoint break 8; 7 in line 8th. the program is re-run to stop at row 8th. in this case, we can use the set variable command to modify the value of string; 8. then run the program. The correct program running result is displayed.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.