Simple practice of GDB program debugging

I have been using GCC/G++ for a long time, but I have never used GDB to debug the program. Sometimes the program is not very big, and there are usually errors. Just look at the compiler compilation results and know where the error is. Or use codeblocks to single-step debugging, Even going back to Windows to debug, it is always inconvenient, so it is necessary to take a look at GDB debugging methods and basic steps.

Here is a simple demo:

First create a buggy code such as the following:


This program is very easy, the purpose is to accept the user's input and output the user's input in response.

However, there is an error on line 17 of this program, which uses an uninitialized character pointer name, so a segmentation fault occurs after compilation and execution, such as the following:


The following uses GDB tools to find problems in the program, the process is as follows:

FIR:

Execute the gdb bug command to load the above executable:


Use the list command (or l) command to view the code:


Use the run command:


Output three lines of results:

1: A segmentation fault occurred

2: Prompt the wrong location


Use the where command to see where the program went wrong:



The above information shows that the gets function has an error. It can be seen from the code that the only possible error is the name variable, so the name variable is viewed through the print command:


It can be seen that the cause of the error is the use of an uninitialized variable name.

quit gdb.


A simple demonstration. For other detailed debugging methods, see maybe.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324795628&siteId=291194637