For programming, the simplest and most convenient program to remove is to print the number of commands/letters-if your program production line does not match the production period, the calculation process is printed. However, this is not convenient. For example, a certain circle may be released within 100th bytes and less than times, so the debugging information provided by China and India may be too confusing. At this time, you need a tool that allows you to write your program and stop it to view the changes in the program. If you find that the problem is not in this place, you can also call it too many rows. You can also manually modify the value of the variable to see where the next change is if the problem is solved, solve several problems with one breath. These areDebugger.
GDB is a debugger in command column mode. If your program uses C, Objective C, C ++, Fortran, Pascal, Ada ,... for example, if the compiler used by the compiler is from GNU, you can use GDB to remove the compiler. In the following example, we use the optimized version of the selected sorting program to divide the program.
First, usegcc -Wall -o ssort ssort_bug.c
When begin, the specified row returns begin ssort. Then click "OK "../ssort 12.3 -17 6.5
The result shows segmentation fault. Well, this is like the "please refer to the programming environment" that appears in Win32. It is the least relevant warning message.
Finallygdb ssort
In GDB. Run the following command in the Command column of GDB:run 12.3 -17 6.5
Returns the same number of command columns to the ssort program. The result is as follows:
Program received signal SIGSEGV, Segmentation fault. 0x4004ca01 in __strtod_internal () from /lib/libc.so.6
Where did we go? Lowerwhere
, The following response is displayed:
#0 0x4004ca01 in __strtod_internal () from /lib/libc.so.6 #1 0x40042d93 in atof () from /lib/libc.so.6 #2 0x080483a9 in main () #3 0x4002f80c in __libc_start_main () from /lib/libc.so.6
These are the names of function numbers, indicating that _ libc_start_main is halfway through the main line, while main is halfway through the main line. atof is being called, and atof is halfway through the line, calling _ strtod_internal is in progress. Let's take a look at the original program:l
It seems that we are not familiar with things.
It seems that only main () is our own program; others are system letters, and there is no original program. Up to two rows:up 2
, Press the arrow twice to repeat the previousl
The command is called and printed again. This fast-paced feature is provided by GNU Readline and is worth learning.
Strange, is there any original response ...? Yes. During debugging, compiler must inform compiler of the information required for debugging to be submitted together. Don't jump out of GDB. open another window and try again:gcc -Wall -g -o ssort ssort_bug.c
Here-G is the meaning of debug. Then, in the original GDB window:run
Run it again. This time, you don't have to give data again. It will remember to use the data given last time.
At this time, it will ask if you really want to refresh the line. This is because the program is being debugged to half, and you may have spent a lot of time in the middle of the project, and it is difficult to find it here. If such a heavy run, all the previous tasks have to be completed. However, our orders are very simple, so we press "Y" generously ". Please note that it has found that your program has been re-installed, so remind you:
'ssort' has changed; re-reading symbols.
When I call where again this time, the output response seems to be a little more.up 2
Then, the question is printed. We are still downl
Print the context program together. It seems clearer than the context.
If you accidentally press enter again (see it now !), It prints ten columns down. In GDB, in order to facilitate you to re-execute the same command, it may take many times to press enter to re-execute the previous command.
Okay. Next timewhere
Remind yourself where the division is. This time, please pay special attention to the printed "#2... at ssort_bug.c: 12" We recommend using the following code:l ssort_bug.c:12
. You can also guess it without checking the operator: This indicates "12th columns of ssort_bug.c in this case ". Because the original scripts of a large program may contain many. c-producer, so it is the most secure method to specify the complete parameter column with the gdb program. However, we only have one legal case, so we onlyl 12
Or.
Okay. Where exactly is our program going? Print the data to see it:p i
It seems that there is no problem; sop argv[i]
What about it? It's a little strange... 0x0 is a null index, that is, null. Print each element in the argv example column. Have you found the cause?
Return to the original record and repeat the entire circle to check the condition. Repeat the condition and repeat the row under GDB (in this case, you do not need to open GDB ). This program can upload rows, but the printed results are not correct-there is no sorting at all. But what exactly is the cause? The program has been completed, and where tells us there is nothing to watch. Let's stop the program before it starts to relay the line:b main
This means thatBreak Point. Of courseb ssort_bug.c:7
Or. Instant usei b
Check that the break points have been set.d b 1
Divide the unique break point and thenb ssort_bug.c:7
, And then usei b
Refer query.
Okay. Repeat the line once. The program stops at the main queue, with 7th columns. Lowern
Call it a step by step. Then press enter until the end of the column.
However, it is not necessary to make a simple and step-by-step procedure.p
Command to print the value of suspicious changes. Please repeat the line once, and print it out every time you calculate min in the circle.
Waiting for renewal...
The following information is provided:
If you want to divide a. Out Into tokens, run the following command:gdb a.out
After entering GDB, the following common commands are available:
- Basic commands
- Quit: End
- Help: Help)
- Run: indicates the number of arguments in the Command column that can be added to the program)
- List: print the Program Article (you can add a column or a letter name)
- Print: print the formula Value
- Running commands
- Limit or function name in the break column: set a limit point
- Info break: Check that we have set the Centers
- Disp formula: This formula is displayed every time a transaction occurs.
- Info disp: Check that we have set the representation
- Next: Program program compute (column numbers of rows to be added)
- Step: compile a Program program line, but if you encounter a call to send data, you need to jump in to the function number step by step, do not take the entire call as a step.
- Cont: continue until the next link or program ends.
- Special variations in formula (such as print and disp values:
- $: Previous Formula
- $: The calculation formula two times ago
- $7: Seventh Formula
- $7: The seventh Formula
- Commands related to heap hosts:
- Where: displays the zookeeper called by the current sub-program.
- Up: Move to the upper limit
- Down: continue
- Other commands:
- [Cr]: repeat an action
In fact, most commands do not need to be completely completed as long as they are not obfuscated. For exampleinfo break
You can simply createi b
That's all.