How to examine source file and data is gdb
Filed under: Linux Debugging Interview Questions, Linux Programming Interview Questions
Examine source code in gdb
list linenum : Print ten lines centered around linenum in current
source file.
list function : Print ten lines centered around beginning of
function (or method).
list : Print ten more lines.
Below are the command used to investigate data in the gdb
print expression : Print value of expression. Expression is any valid C expression, can include function calls and
arithmetic expressions, all evaluated within current stack frame.
set variable = expression : Assign value of variable to expression. You can set any variable in the current scope. Variables
which begin with $ can be used as temporary variables local to gdb.
display expression : Print value of expression each time the program stops. This can be useful to watch the change in a
variable as you step through code.
undisplay : Cancels previous display requests.
How to investigate stack in gdb and what are the various gdb stack related debugging options
Filed under: Linux Debugging Interview Questions, Linux Programming Interview Questions
Below are the some of the gdb stack command to investigate the gdb stacktrace. backtrace command show all the stack frame and quick a stacknum starting from 0 first function to framenumber to every frame.
backtrace : Show stack frames, useful to find the calling sequence that produced a crash.
frame framenumber : Start examining the frame with framenumber. This does not change the execution context, but allows
to examine variables for a different frame.
down : Select and print stack frame called by this one. (The metaphor here is that the stack grows down with each function call.)
up : Select and print stack frame that called this one.
info args : Show the argument variables of current stack frame.
info locals : Show the local variables of current stack frame. Select and print stack frame that called this one.
How to set breakpoints in gdb ? What are the different option in gdb breakpoint ?
Filed under: Linux Debugging Interview Questions, Linux Programming Interview Questions
Breakpoint are used to stop the program at certain line number or certain function . Each breakpoint is assigned a certain number “breaknum” from which various information about the breakpoint in gdb can be performed. We can set conditional breakpoint or breakpoint on a particular line on of a particular file if multiple file are involved in debugging. Below are the various option in setting breakpoint in gdb
info break : Prints a list of all breakpoints with numbers and status.
break function : Place a breakpoint at start of the specified function
break linenumber : Prints a breakpoint at line, relative to current source file.
break filename:linenumber : Place a breakpoint at the specified line within the specified source file.
You can also specify an if clause to create a conditional breakpoint:
break fn if expression : Stop at the breakpoint, only if expression evaluates to true. Expression is any valid C expression,
evaluated within current stack frame when hitting the breakpoint.
disable breaknum : Display breakpoint (with break num)
enable breaknum : Disable/enable breakpoint identified by breaknum
delete breaknum : Delete the breakpoint identified by breaknum
commands breaknum : Specify commands to be executed when breaknum is reached. The commands can be any list of C
statements or gdb commands. This can be useful to fix code on-the-fly in the debugger without recompiling .
cont : Continue a program that has been stopped.
For example, the commands…
break binky.c:120
break DoGoofyStuff
set a breakpoint on line 120 of the file binky.c and another on the first line of the function
DoGoofyStuff. When control reaches these locations, the program will stop and give
you a chance to look around in the debugger
How to start gdb and what are the various running command in gdb
Filed under: Linux Debugging Interview Questions, Linux Programming Interview Questions
GDB Interview Questions
Compile the program with -g options .
Then type gdb programname
Running the Program
run: Reset the program, run (or rerun) from the beginning. You can supply command-line arguments the same way you can supply commandline arguments to your executable from the shell.
step : Run next line of source and return to debugger. If a subroutine call is encountered, follow into that subroutine.
step count : Run count lines of source.
next : Similar to step, but doesn’t step into subroutines.
finish : Run until the current function/method returns.
return : Make selected stack frame return to its caller.
jump address Continue program at specified line or address.
