Debugging tool under Linux-GDB

GDB

1.What is GDB

GDB is a debugging tool provided by the GNU software system community. Together with GCC, it forms a complete development environment. GDB is the standard development environment for Linux and many Unix-like systems.

Generally speaking, GDB can mainly provide help in the following four aspects:

  • Start the program and run it as you like according to customized requirements;
  • The debugged program can be stopped at the specified breakpoint (The breakpoint can also be a conditional expression);
  • When the program is stopped, you can check the status of variables in the current program;
  • You can modify the program to correct the impact of one BUG to test other BUGs;

2. Preparation

Usually, when compiling for debugging, we need to turn off the optimization option -O and turn on the debugging option -g.

In addition, -Wall will choose to turn on all warning without affecting the program behavior as much as possible. It can also find many problems and avoid unnecessary BUGs.

gcc -g -Wall program.c -o program

The -g option is used to add source code information to the executable file, such as which machine instruction in the executable file corresponds to which line of source code. But it does not add the entire The source file is embedded in the executable file, so you must ensure that GDB can find the source filewhen debugging.

3.GDB commands – start, exit, view code

Test using the following files:

main.c

#include <stdio.h>
#include <stdlib.h>

#include "head.h"

int main(int argc,char* argv[]){<!-- -->

    int a , b;
    if(argc < 3){<!-- -->
        a = 10, b = 20;
    }
    else{<!-- -->
        a = atoi(argv[1]);
        b = atoi(argv[2]);
    }

    printf("a = %d , b = %d\\
",a,b);
    printf("a + b = %d\\
",add(a,b));
    printf("a - b = %d\\
",sub(a,b));
    printf("a * b = %d\\
",mul(a,b));
    printf("a / b = %.2lf\\
",divide(a,b));
    printf("xixixixixixi\\
");

    printf("hello makefile\\
");


    for(int i = 0;i < 20;i + + ){<!-- -->
        printf("%d ",i + 1);
    }
    printf("\\
");

    return 0;
}

head.h

#include <stdio.h>

int add(int a,int b);

int sub(int a,int b);

int mul(int a,int b);

double divide(int a,int b);

add.c

#include "head.h"

int add(int a,int b){<!-- -->
    return a + b;
}

sub.c

#include "head.h"

int sub(int a,int b){<!-- -->
    return a - b;
}

mul.c

#include "head.h"

int mul(int a,int b){<!-- -->
    return a * b;
}

div.c

#include "head.h"

double divide(int a,int b){<!-- -->
    return a * 1.0 / b;
}

Makefile

src=$(wildcard *.c)
objs=$(patsubst %.c,%.o,$(src))

target=app

$(target):$(src)
$(CC) $(src) -o $(target) -g


.PHONY:clean
clean:
rm $(objs) -f

1. Start and exit

  • gdb executable file;
  • quit / q;

First use make to generate an executable program app with debugging information, and then use gdb to start and exit.

2. Set parameters/get parameters for the program

  • set args 10 20 ...;
  • show args;

3.GDB usage help

  • help;

4. View the current file code

  • list / l (displayed from default position);
  • list / l line number (displayed starting from the specified line, with the specified line number in the middle);
  • list/l function name (displayed from the specified function);

The default is to display from the starting position.

Show line 20.


Shows the main function.

5. Set the number of displayed lines

  • show list / listsize;
  • set list / listsize number of lines;

Only 10 lines are displayed by default.

Modify it to 20 lines and look at the code again.

6. View the code of non-current files

  • list / l file name: line number;
  • list / l file name: function name;

Look at line 5 of the add.c file.

Check out the sub.c file for the sub() function.

4.GDB command – breakpoint operation

1. Set breakpoints

  • break / b line number;
  • break / b function name;
  • break / b file name:line number;
  • break/b filename:function;

By default, a breakpoint is set on line 10 of the file.

The main function sets breakpoints.

In the add,c file, set a breakpoint on line 3.

In the sub.c file, the sub() function sets a breakpoint.

2. View breakpoints

  • info/i break/b;

View the breakpoint information set in the first step.

3. Delete breakpoints

  • delete / del / d breakpoint number;

Delete breakpoint number 3, which is line 3 of add.c.

4. Setting breakpoints is invalid

  • disable / dis breakpoint number ;

Set breakpoint 1 to invalid.

5. Setting breakpoints takes effect

  • enable / ena breakpoint number;

Setting breakpoint No. 1 takes effect.

6. Set conditional breakpoints (generally used in loop bodies)

  • break / b line number if i==5;

Line 28 of main.c is a loop. When i ==10, set a breakpoint on line 28.

It should be noted that after exiting gdb, all the breakpoints you set before will be gone.

5.GDB command – debugging command

1. Run the GDB program

  • start (the program stops at the first line);
  • run (stop when a breakpoint is encountered);

Set a breakpoint on line 20 of main.c.

start

run

2. Continue running and stop when the next breakpoint is encountered

  • continue/c;

Set another breakpoint on line 24 of main.c.

run starts the program and reaches line 20.


Then execute the continue / c command and you will reach the next breakpoint, which is line 24.

If there is no breakpoint later, and then execute the continue / c command, the program will be executed to the end and completed.

3. Execute one line of code downwards (without entering the function body)

  • next/n;

4. Variable operation

  • print variable name (print the value of the variable);
  • ptype variable name (print variable data type);

5. Downward single-step debugging

  • step / s (will enter the function body);
  • finish (call out the function body);

6. Automatic variable operation

  • display num (automatically prints the value of the specified variable num);
  • info / i display (display which automatic variables are available);
  • undisplay number (unspecified automatic variable);

7. Other operations

  • set var variable name=variable value (define variable);
  • until (break out of the loop);