There are three basic methods for setting GDB breakpoints: Break, catch, and watch.
Break is the simplest. Here we will talk about break settings.
I. breakpoint settings
When we use GDB as an executable program, the program needs to be adjustable and the mode option-G must be added during compilation. However, if we download other open-source projects, generally, debugging options are not added. Therefore, you must modify the MAKEFILE file and add the debugging options.
For example, I want to debug the executable project of a project.
GDB Project
We can set a breakpoint at this time: for example
B Main indicates that a breakpoint is set at the main function entry.
B 12 indicates that a breakpoint is set on 12 rows.
Here, the breakpoint is set in the main program file of this project, but we know that a project has many files. If you want to set breakpoints in other files, this will not work, you need to add other file names. Assume that this project has a source file called uploadmgr. c. Set a breakpoint in this file as follows:
B uploadmgr. C: Upload indicates that the upload function in the uploadmgr. c file has a breakpoint.
B uploadmgr. C: 12 indicates that a breakpoint is set on the 12 rows of the uploadmgr. c file.
Ii. breakpoint viewing
Info break can be used to view the current breakpoint information, for example:
(GDB) info break
Num type disp ENB address what
1 breakpoint keep y 0xb76b1650 in uploader_create_dir_config (apr_pool_t *, char *) at Maid: 202
Breakpoint already hit 4 times
2 breakpoint keep y 0xb76b1650 in uploader_create_dir_config (apr_pool_t *, char *) at Maid: 202
Breakpoint already hit 4 times
3. breakpoint clearing
Clear clears the breakpoint of the specified row or function in the current running file. If no parameter exists, clears all breakpoints in the line that the selected frame
Is executing in. (Not quite clear)
Delete 1 indicates deleting the first breakpoint (use info break to view the breakpoint number)
Delete, without parameters, deletes all breakpoints.
4. Tips
1. GDB has a friendly debugging interface, that is, layout debugging, which allows you to view the source code while debugging, as shown below:
However, the layout method sometimes has a bug, but it can be used basically. You can use Ctrl + X and CTRL + a (to use it before, press Ctrl + X and then Ctrl +) to switch. Layout may cause a screen display on the console. use Ctrl + L to clear the screen.
2. rbreak can be used to set breakpoints using regular expressions. This is useful, for example, if you want to set breakpoints for all functions in a file:
Rbreak XXX. C :*
That's all, but there is a requirement that your program must be in the debugging status, that is, it will be effective after running. Otherwise, no response is returned. (* Whether or not to add a direct viewpoint)
3. When we want to print a complex variable, We can first print the changed data structure with Pt, and then print the values of interest. For example:
(GDB) Pt progress
Type = struct requestreader: postprogress {
Apr_uint32_t ID;
Apr_uint64_t total_size;
Apr_uint64_t read_size;
Apr_time_t end_time;
Bool is_succeeded;
Apr_size_t item_id;
}*
Of course, you can also enter P and press the tab key to automatically prompt, for example:
(GDB) P progress->
End_time is_succeeded read_size
Id item_id total_size
(GDB) P progress->
However, the type is missing.
4. when we promise a char * type variable (long_var), if the variable is long, you can directly P long_var. Generally, GDB will not print it all, and it will display... three ellipsis. We can use printf "% s" and long_var to print all the content.
5. When we enter a function, we need to print the stack frame to view it. If you add the full parameter, more information will be provided.
BT full