The use of gdb debugging tools
In order to find out what is wrong with the original program, as a result of modifying the source code and outputting the correct program, for rookies who are just learning linux, it is more appropriate to use gdb debugging error information, if you are an expert in c language, you do not have to look at the following, because this is only suitable for rookie debugging dozens of lines of code using tools.
Let me give you an example of how to use gdb to debug and find the wrong information.
# include
# include
# include
Int display1 (char * string)
Int display2 (char * string)
Int main ()
{
Char string [] = "hallo world"
Display1 (string)
Display2 (string)
}
Int display1 (char * string)
{
Printf ("The original string is% s\ n", string)
}
Int diaplay2 (char * string1)
{
Char * string2
Int size,i
Size = (char *) malloc (size+1)
For (I = 0; I < size; iTunes +)
{
String2 [size-I] = string [I]
}
String2 [size+1] =''
Printf ("the string after is% s\ n", string2)
Free (string2)
}
When the code is input, compile using gcc-g test.c test and then. / a.out finds that the result is not what we want, there should be a line of hello world and another line of flashback output of hallo world, but the second line is empty, at this time we can use gdb debugging to check the error.
First of all, on the basis of the above operation commands, enter gcc test on the terminal panel and enter the gcc debug mode. Then we can use the command l to view the source code, and then set the breakpoint b + the number of lines 1 and the number of lines 2 on the source code. After setting the two breakpoints, we use the info b command to view the breakpoint information. Then, when we use r to run the code, we can use the step-by-step command n to run the code step by step and view the running information. If this does not find out the error information, we can check the information on the variable. Use p + variable name, and you can see the running information of the variable at this time. In the above program, When we check the information on string2 [0], we find that string2 [0] does not have a value for him, so string2 [0] will always be NULL, so we will not output the result. After finding an error, exit gdb, use the command Q, and re-edit test.c to string2 [size-I-1] = string [I]. Recompile gcc-g test.c test. / a.out and finally take a look at the run result and find that it is the result you want. Well, the simple gdb debugging is over. Goodbye!