GDB debugging in Linux

Source: Internet
Author: User
GDB debugging in Linux is a powerful program debugging tool in UNIX released by the GNU open-source organization. In general, GDB mainly helps you complete the following four functions: start your program and run the program as needed according to your custom requirements. 2. run the program to be debugged... GDB debugging in Linux is a powerful program debugging tool in UNIX released by the GNU open-source organization. In general, GDB mainly helps you complete the following four functions: to start your program, you can run program 2 as per your custom requirements: the program to be debugged can be stopped at the breakpoint of the modulation you specify (the breakpoint can be a conditional expression) 3: When the program is stopped, check what happened in your program 4: dynamically changing the execution environment of your program www.2cto.com using the print command can check the value of each variable. (Gdb) print p (p is the variable name) 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 content: data structure of function calling (gdb) print find_entry () in the program and other complex objects (gdb) print * table_start = {e = reference = '\ 000', location = 0x0, next = 0x0} historical components (gdb) print (a historical variable, values that can be directly referenced later) therefore, to display the 10 elements after h, you can use h @ 10: (gdb) printh @ 10 = (-1,345, 23, -98,345, 0, 10) the whatis command can display the type of a variable (gdb) whatis p type = int * www.2cto.com break command (can be abbreviated as B) this command can be used to set breakpoints in a program to be debugged. The command has the following four forms: breakline-number so that the program stops just before the given line is executed. Breakfunction-name causes the program to stop before entering the specified function. Breakline-or-function if condition if the condition is true, the program stops when it reaches the specified row or function. Breakroutine-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 breakif command, as shown in the following figure: (gdb) break line-or-function if expr finish the current function to end the execution. the returned value is a program example source program: tst. c 1 # include 2 3int func (int n) 4 {5int sum = 0, I; 6for (I = 0; I <= n; I ++) 7 {8sum + = I; 9} 10 return sum; 11} 12 13 14 main () 15 {16int I; 17 long result = 0; 18for (I = 1; I <= 100; I ++) 19 {20 result + = I; 21} 22 23 printf ("result [1-100] = % d \ n", result ); 24 printf ("result [1-250] = % d \ n", func (250); 25} compile and generate the execution File: (in Linux) hchen/test> gcc-g tst. c-o tst debug with GDB: hchen/test> gdb tst <---------- start GDB GNUgdb 5.1.1 Copyright20 02 Free Software Foundation, Inc. GDBis free software, covered by the GNU General Public License, and youare welcometo change it and/or distribute copies of it under certain conditions. type "show copying" to see the conditions. thereis absolutely no warranty for GDB. type "show warranty" fordetails. thisGDB was configured as "i386-suse-linux "... (gdb) l <-------------------- l command is equivalent to list, the original code is obtained from the first line. 1 # include 2 3int func (int n) 4 {5int sum = 0, I; 6for (I = 0; I <= n; I ++) 7 {8sum + = I; 9} 10 return sum; the entire process: first write the source code file and then compile and generate. c executable file command gcc-g tst. c-o tst then enables GDB to debug the gdbtst www.2cto.com (gdb) l Command, which is equivalent to the list command. the Source Code is obtained from the first line. (Gdb) break 16 sets the breakpoint, and breakfunc <-------------------- sets the breakpoint at the entry of function func () in the source code line 16. (Gdb) infobreak: view the breakpoint information (r) running program. run command is short for (gdb) n <------------------- single statement execution, and next command is short. Gdb) c <--------------------- to continue running the program, the continue command is abbreviated. (Gdb) p sum gdb) bt <--------------------- view the function stack. (Gdb) finish <--------------------- exit the function. (Gdb) c <--------------------- continue to run. Gdb) q <--------------------- exit gdb.

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.