Summary of GDB debugging methods

gdb is a very good way to debug programs and track problems. The specific usage of gdb will be introduced in detail below.

1 [First introduce how to run programs with gdb]

When compiling a program with gdb, first add -g when compiling the program.

Take the above program as an example to compile

gcc gdb_test.c -o gdb_test -g

There are two running usages

  • One is to enter gdb + compiled file name after successful compilation
  • The second is to enter gdb in the command line, press Enter, and then enter file + compiled file

    You can choose one of these two methods.

    Then enter the run command to run the program, abbreviated r

2 [Break point in the program]

  • Use break for breakpoints, abbreviated as b
  • You can make a breakpoint on a line Example: breakpoint b 3 on the third line of the program
  • If there are multiple files, you can break a certain line of a file, for example: break point b gdb_test.c:3 for the third line of gdb_test.c 
  • You can make a breakpoint on a function Example: breakpoint b func for the func() function in this program
  • Similarly, you can also break the function of a file in multiple files, for example: b gdb_test.c:func
  • For example, break point b 20 on line 20 of the program
  • After the breakpoint is hit, a line of content is displayed, which is the number of breakpoints, in which file, and the number of lines.

    After hitting the breakpoint, run the program and enter the r command

    When the program runs to line 20, it will automatically stop. At this time, the program runs to line 19, and line 20 has not been run.

    We can first use the p command to print the value of i in the program. The following blog will talk about the usage of p

  • At this time, the value of i is 2.

    If we want the program to continue running, we can use the c command

  • If you want to quit the program, you can use the q command.
  • If we want to delete a breakpoint, there are two ways:

1 delete command abbreviation d

delete break deletes all breakpoints

delete break n delete a breakpoint n is the breakpoint number

2 clear command

clear line number deletes the breakpoint set on a line

The usage of these two delete breakpoints is that one is to delete according to the breakpoint number, and the other is to delete according to the line number.

  • disable break n disable a breakpoint n is the breakpoint number
  • enable break n enable a breakpoint n is the breakpoint number
  • Set breakpoints on lines 19 and 20, respectively, and run the program.
  • After the program stops at line 19, the next step is to disable the breakpoint on line 20
  • Then keep running the program and you will see that there is no stop at line 20. So it means that disabling the breakpoint on line 20 has taken effect.
  • View breakpoint information: info b View all breakpoint information
  • info bn View the information of the nth breakpoint





Guess you like

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