In addition to the wrong process: GDB

xiaoxiao2021-03-06  111

For program designers, the easiest and convenient defect program is the print command / function - if your program execution result is not in conformance, the intermediate calculation process is printed all the way. But this is not convenient. For example, a recoil may be mistaken in the first million seventy-three times, and the Debug information in this is more dizziness. At this time you need a tool, let you execute your program while you will stop going to observe changes in some variables. If the problem is not here, you can also call it to continue. Up to half, you can also manually modify the value of the variable, see if this is resolved, the next error is, a few problems have been solved. These are the work of Debugger.

GDB is a command column mode of Debugger. If you write, you can use C, Objective C, C, C , Fortran, Pascal, Ada, ... and so on, and the compiler is from the GNU, you can get GDB to get the wrong. Let's take the wrong demonstration version of the sort program to practice the error.

First, as usual, use gcc -wall -o ssort ssort_bug.c to compile the execution file SSORT. Then execute ./ssort 12.3 -17 6.5 Results Segmentation Fault appears. Very good, this is the most no-topile error message as the "Please contact the program designer" on the top of Win32.

So the lower GDB SSORT entered GDB to start unlock. Tap under the command column of GDB: RUN 12.3 -17 6.5 Feed the same command column parameters as the SSORT. The result appears like this:

Program received Signal SigSegv, Segmentation Fault.

0x4004ca01 in __strtod_internal () from /Lib/libc.so.6

What did we do? Under WHER, there is a message like this:

# 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 the function, indicating that __libc_start_main executes half, is calling main, while the main is executed, the atof is being called, and the ATOF is executed, and the __strtod_internal, the result is hanging here. So, print a little original code: l It seems that there is nothing we are familiar with.

It seems that only main () is our own program; others are system functions, no wonder there is no original code. Up two floors: Up 2, press the arrow twice, call the previously knocked L command to print once. The function of this quick key is provided by GNU Readline, and there are some simple and convenient quick keys worth learning.

Strange, still there is no original code ...? Yes, when compiling the program, you must tell Compiler to compile the information you need to use. You don't have to jump out of GDB, please open another window, recompile: gcc -wall -g -o ssort ssort_bug.c The -g is what you want Debug. Then under the original GDB window: RUN ran again. This time you don't have to give the parameters, it will remember to use the parameters given last time.

At this time, it will ask if you really want to re-execute. This is because the program is debug to half, you may have already spent a lot of hard work, interrupted and implement a few times, it is hard to find here, if you run, everything goes to the soup. However, our situation is simple, so press "Y" in generous. Please note that it is found that your program has been recompiled, so remind you:

'sorts' has change; re-reading symbols.

This time I played WHER, the imprinted message seems to be more. After Up 2, it was found that it was printed on the question. We still printed with the programs of the context and looks more clearly. If you accidentally press ENTER key (try it right away!), It will then print ten columns. In GDB, in order to facilitate you to repeat the same instruction, there are many times, pressing Enter, indicating that the last instruction is repeated.

It doesn't matter, then the next time where WHER reminds yourself where you are. Please pay special attention to its printed "# 2 ... at ssort_bug.c: 12" We still draw a gourd, down l ssort_bug.c: 12. It is not necessary to check the manual, also guess: this means "SSORT_BUG.C file 12". " Because the original code of the large-scale may contain many .c files, it is the most insurance that is the most insurance with the GDB communication program. However, we have only one file now, so it is actually playing only L 12.

Ok, our program is wrong? Print the variables to see it: P i seems to have no problem; then p argv [i]? It is a bit strange ... 0x0 is an empty indicator, that is, null. Please print each element of the Argv array. Did you find a reason?

After returning to the original code, the reciprocating ring is terminated, recompiled and re-executed under the GDB (the same, do not need to leave GDB). This program can be implemented, but the result is not correct - there is no sorting at all. But where is wrong? The program has been implemented, and where tells us that there is no thing. We will stop before starting to start executing: b main This sentence means that set a Break Point interrupt point at the entrance to the subscript main. Of course, B ssort_bug.c: 7 can also be. Immediately use I B to check out those Break Points, with D B 1 to delete the unique break point, then lower B ssort_bug.c: 7, and finally check with I B.

Ok, re-executed once, the program stops in the threshold of Main, column 7. The next N calls it to perform one step. Then press ENTER, pay attention to it is executing that column until it ends.

Of course, the step-by-step execution program is not enough, and it is also necessary to lower the P instruction in the process, and the value of the suspicious variable is printed. Please re-execute it, and then the MIN is calculated every time, it is printed.

to be continued...

The following is old information

Suppose you want to deactivate a.out, you can follow the instruction: GDB A.out has the following common instructions after entering GDB:

Basic directive

Quit: End Help: Help (Canada Directive Name) Run: Performing Code (can be fed) List: Column Printer This article (Canada number or function name) Print: Printing the calculation value Interrupt command

BREAK number or function name: Setting Interrupt Point Info Break: See those interrupt point DISP operations: Each interrupt is displayed in this operational INFO DISP: See those display next: Execute a row code (The number of columns that can be performed) Step: Perform a row code, but if you encounter a function call, you want to jump into the function to go step by step, don't make a whole function call as a step. Cont: Do it until An interrupt point or a program ends for special variables in an arithmetic (such as PRINT and DISP):

$: The previous arithmetic $$: Two-time arithmetic $ 7: Seventh computation $$ 7: Countdown seventh arithmetic and stack-related instructions:

WHERE: Shows the status of the current subscriber layer call: Top level down: Top Next layer Other instructions:

[CR]: Repeat the last action fact that most of the instructions are not confused, they don't have to finish completely, for example

Info Break can be easily hit

I B is fine.

转载请注明原文地址:https://www.9cbs.com/read-101301.html

New Post(0)