Tuesday, May 15, 2012

GDB V/S BUGS - III

Finally, the last post for gdb!
In this section, we will go through the terms like backtrace, core dump and stack overflow.
Starting with the 'backtrace' command, as the name suggests, it is used while running the prog, to know about the previous function calls made, i.e we can get to know, from where the control has come to the current function.It gives us the list of stack frames(function calls ,yet to be returned and stored on the the stack in stack frames sequentially), each with a number.This number is used to get info about any stack frame.'Info' command is used for further info on these frames.
e.g. 'info args 4' lists the list of arguments with their values, passed to the function call, stored on the stack frame #4.
similarly,'info locals #' gives a detailed info about the local variables used in the respective call.If you want to get the whole info in one go, with some extra info,like:
• the address of the frame
• the address of the next frame down (called by this frame)
• the address of the next frame up (caller of this frame)
• the program counter saved in it (the address of execution in the caller
frame)
• which registers were saved in the frame

the command is 'info frame #'.These all commands can help you figure out, specially in the recursive programs, the control flow of the program, the sequence of calls with their details, which can greatly help you to chase the bugs.
Using these, you can also find where your program stopped working i.e the details of the program when ?? 
The command 'where' is an additional alias for backtrace.

The next significant term is stack overflow:
A stack overflow occurs when too much memory is used on the call stack. The call stack contains a limited amount of memory, often determined at the start of the program.When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a 'buffer overflow'), the stack is said to overflow, typically resulting in a program crash.
This is also one of the main reasons of Segfault.
Following is an example which causes stack overflow(recursion without a base case)
 int main()
 {                                    
    main();
    return 0;
 }

lets come to the last term of the session now, core dump:
A core dump is a file that consists of the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed), including the processor registers, which may include the program counter and stack pointer, memory management information, and other processor and operating system flags and information. These dumps traditionally get created as "core".Core dumps are often used to assist in diagnosing and debugging errors in computer programs.Gdb itself can use these dumps to debug the programs, using the 'working history' of the program stored in these files.

So, this marks the end of this post and gdb as well!
Any queries, you can post here, or mail me at yash.girdhar@gmail.com.
adios :)

No comments: