Debug the game server with gdb and the game server with gdb
Preface The importance of gdb
Generally speaking, gdb is used for debugging. "Command", which is almost equivalent to complicated words for users. Despite the fact, gdb is required for actual development and debugging. Currently, most Linux systems exist on servers. We generally use Terminal to operate these systems. That is to say, these Linux systems do not have a graphical interface. Debugging is generally divided into two parts: Debugging during development and debugging during runtime. When our program is deployed on Linux, We need to forget the damn graphic debugger.
Purpose of writing this article
The server of the company's game crashed yesterday. I forgot the gdb command-_-during debugging -_-! (Of course, I finally found this Bug ). Therefore, write this blog post to deepen your memory and share your experience.
Basic commands
Note: There are far fewer gdb commands
1. attach: Use gdb to debug a running process.
Gdb <program> PID or gdb attach PID
2. br: set breakpoints
Br filename: line_num
Br namespace: classname: func_name
3. n: skip step s: single step in
4. finish: run the retun function to return the result.
5. list: list 10 lines of code after the current location; list line_number: list 10 lines of code after line_number
6. bt (backtrace): List call stacks (where is the same type, and experience tells me that when you want to list the stack information and find it ineffective, it is best to try both commands)
7. info locals: list the local variables of the current function
8. p var _: print the variable value
9. info breakpoints: list all breakpoints
10. delete breakpoints: delete all breakpoints; delete breakpoints id: delete breakpoints numbered with IDs; disable/enable breakpoints id: disable/enable breakpoints
11. break... if... conditional interruption
Next I will mainly talk about runtime debugging.
Test code
# Include <stdio. h> void Crash () {int * a; * a = 1; printf ("% d \ n", * a);} void EndlessLoop () {int I = 1; int j = 0; while (I) {++ j ;}int main () {Crash (); // Crash EndlessLoop (); // endless loop return 0 ;}
Debugging crash
Http://blog.csdn.net/yitouhan/article/details/17175113 this is an article I wrote earlier about preventing crashes.
Here the core file is used:
When a program crashes, it generally generates a core file under the specified directory. The core file is only a memory image (with debugging information added) and is mainly used for debugging.
The core file name is generally core. PID, that is, core.3745, etc.
I usually set Linux core support in/etc/security/limits. conf (Centos). This requires restarting the system and printing core files permanently.
Add the following command
* Soft core unlimited
* Hard core unlimited
It means that both software and hardware print core files and are unlimited (unrestricted ). Replace unlimited with the specified size.
Note: There are other settings that you can search and query on the Internet.
At this point, the test server crashes. The core.1234 file is found in my working directory (the core file is output to the working directory by default ).
Input gdb test core.1234 to start gdb debugging.
Enter where to view stack information, for example:
Do not tell me where the error is still not found ?!
Debug endless loop
When we find an endless loop, do not stop the process. Assume that the process ID is 1234
Input command gdb attach 1234
You will find that gdb will break the point in the dead loop, maybe not very clear, you can always enter n. Pay attention to the row number, and you will find that this is where an endless loop occurs.
Enter where to view the stack information, as shown in.
Do not tell me where the error is still not found ?!
Semi-infinite loop
Semi-endless loops (this is a term I used myself and I don't know if other tutorials are used or not) are errors during running, A Bug that loops through millions, tens of millions, or even hundreds of millions of times.
Although this Bug is less harmful to crashes and loops, debugging is much harder. If you have better debugging experience on such bugs, I hope you can share it with us!