GDB introduction

The GNU Debugger (GDB) is a powerful command line debugging tool. Generally, developing under Windows, command line debugging is rarely handled, and debuggers and compilers are mostly integrated into the IDE. MinGW (a collection of windows-specific header files and GNU toolset import libraries) contains both GCC and GDB tools:

Run the GDB -v command to view the GDB version.

However, the GDB tool is one of the must-know tools for developing under Linux. Xiaobian recently also switched to Linux, naturally also want to master some of the basic tools and knowledge will know. Xiaobian is also used to learn where, this note we first share the use of GDB:

Examples demonstrate the use of GDB

Old readers all know that one of the characteristics of this public article is more examples, operability is strong, step by step with the article should be able to learn something. Similarly, this note will be analyzed by examples.

Example code gdb_test.c:

// Public id: Linux mainland
// author: ZhengN
#include <stdio.h>

// Test function 1
void test0(void)
{
	int i = - 1;

	if (i = 0)
		printf("i = %d\n", i);
	else if (i = 1)
		printf("i = %d\n", i);
	else
		printf("i = %d\n", i);
}

// Test function 2
void test1(void)
{
	int a[10] = {0.1.2.3.4.5.6.7.8.9};
	int *p = &a[1];
	int *p1 = (int*)(&a + 1);

	printf("p[6] = %d\n", p[6]);  
	printf("*(p1 - 1) = %d\n", *(p1 - 1));		
}

/ / the main function
int main(int argc, char *argv[])
{
    test0();
    test1();
    
    return 0;
}
Copy the code

There are two test functions in this sample code, which are actually two classic fallible interview pen questions. And you can think about what the answer is. Below we use GDB to debug and analyze step by step, do experiments under Windows, Linux operation is similar.

In general, we use the following command to compile:

gcc gdb_test.c -o gdb_test.exe
Copy the code

This compiles gdb_test.exe without debugging information. To debug using GDB, you must compile an executable with debugging information such as line numbers. To generate debugging information, add a -g parameter.

In addition, we should not use optimization when compiling. If optimization is used, the compiler will make some optimizations to the program, which may change the order of statements and optimize some variables, which may cause the program execution flow and the source flow to not match. Further, we can turn on all warnings with the -wall argument, so our compile command becomes:

gcc -g -Wall gdb_test.c -o gdb_test.exe
Copy the code

1. GDB common commands

Here is a rough list of some commonly used commands:

2. Demo debugging analysis

To compile the executable program gdb_test.exe with debugging information, use the build command above. There are two ways to start debugging.

One way is to enter the GDB command to enter the GDB environment, and then type file+ executable to load the debug file, that is:

Another way is to debug the program directly by entering the GDB + executable, that is:

(1) Debug test function 1

Did you think about test function 1 up here? Let’s step through and see what the results look like:

Test1 = test1;

② Run to breakpoint:

③ Step down:

Obviously, by stepping through this sentence we get the result of the test function 1, which is the output I = 1. Did you get that right? If you don’t pay attention to this, you can make a mistake. If you use the = sign instead of the == sign, this little trap may confuse some beginners of C.

The general form of an if statement is:

if (expression)
	statement
Copy the code

To be clear, statement is executed if expression is true (non-0). In this case, if (I = 0) is equivalent to

i = 0;
if (i)
Copy the code

If expression is false, statement will not be executed.

Similarly, if (I = 1) is equivalent to

i = 1;
if (i)
Copy the code

If expression is true, statement is executed.

When you find that the process of executing your code is abnormal, you might as well debug and debug it step by step to see whether the program goes according to the process you designed and whether our execution logic design is wrong.

(2) Debug test function 2

Test function 2 is also a classic interview question. Can’t see the results at a glance? That’s okay. Let’s debug it together. Following the above process, we exit the GDB environment with the quit command and re-enter debugging test2.

Test2 = test2;

② Run to breakpoint:

A [1] = a[1];

So, before the array is initialized, the values in the entire array space are random values. The value of a local variable before initialization is irregular, so you might as well initialize a certain value when defining a local variable to prevent errors.

③ Step down:

Int p; int a; int a;

Because line 23 has not yet been executed, the address p points to is not the address of the element A [1].

The value of the pointer variable p and the address starting with the value of the pointer variable p and ending with the value of the pointer variable P are the values of the space:

So far, we have clearly obtained the value of p[6] through debugging.

To continue, let’s look at the value of &a[0], &a, (&a+1), and p1:

&a is of type (int (*) [10]), which is a pointer to an integer array of 10 elements, so (&a+1) is set back by 10 * sizeof(int). Further, use other output information:

& has a value of a0x61fee0
&a+1The value of0x61ff08
Copy the code

Subtract the two values to get 40, which is exactly the number of bytes in the entire array.

P1 is an integer pointer, so p1-1 refers to the address of a[9] offset by sizeof(int) by a byte (0x61FF04), so *(p1-1) is the value of a[9]. Finally, let’s look at the values in the next 40 addresses of &a:

The above is the example of this demonstration, only to use a small part of the GDB command, there are more commands we can practice using their own, the basic will, do not understand the place encountered when it is too late to check.

Maybe it’s a little messy, but HOPEFULLY it’s helpful. In short, for some uncertain knowledge points or the execution of the program is not consistent with expectations, you may wish to debug, step by step to see if the data is abnormal.