Hi,
It may be out of track article for kernel, but it could be good if you have single file.
So GDB is a debugger utility in linux for debugging C/ C++ program.
Step 1 : So first how you will enable your program to do so :
In your compilation command, use -g option, for example :
gcc -g program.c -o program
Step 2 : Now to run the utility, just type gdb on the shell. It will give you something like (gdb) shell
Step 3: Load the file which you want to debug,
(gdb) file filename.c
Step 4 : Put break points at lines
(gdb) break filename.c : Line number
Put break points at function
(gdb) break func_name
Step 5 : type run. It will run till it reaches your first break point.
Step 6 : Now suppose you want to check the value of some variable use print
(gdb) print x
Step 7 : If you want to just go to next break point, just type
(gdb) continue
Step 8 : If you want to go step by step, use either next/step command.
(gdb) next
or
(gdb) step
Only difference is, next will treat any sub-routine call as a single instruction.
Now apart from break point there is one more concept as Watch point, it works on variable instead of function or line number. It will stop code execution when the value of variable on which watchpoint is set, has been changed. Syntax is
(gdb) watch variable_name
There are other command also
1. backtrace - produces a stack trace of the function calls that lead to a seg fault (should remind you of Java exceptions)
2. where - same as backtrace; you can think of this version as working even when you’re still in the middle of the program
3. finish - runs until the current function is finished
4. delete - deletes a specified breakpoint
5. info breakpoints - shows information about all declared
breakpoints
Also we can use condition in breakpoints, for example you want to break only when i < 1,so do like this :
(gdb) break program.c : 7 if i < 1.
It may be out of track article for kernel, but it could be good if you have single file.
So GDB is a debugger utility in linux for debugging C/ C++ program.
Step 1 : So first how you will enable your program to do so :
In your compilation command, use -g option, for example :
gcc -g program.c -o program
Step 2 : Now to run the utility, just type gdb on the shell. It will give you something like (gdb) shell
Step 3: Load the file which you want to debug,
(gdb) file filename.c
Step 4 : Put break points at lines
(gdb) break filename.c : Line number
Put break points at function
(gdb) break func_name
Step 5 : type run. It will run till it reaches your first break point.
Step 6 : Now suppose you want to check the value of some variable use print
(gdb) print x
Step 7 : If you want to just go to next break point, just type
(gdb) continue
Step 8 : If you want to go step by step, use either next/step command.
(gdb) next
or
(gdb) step
Only difference is, next will treat any sub-routine call as a single instruction.
Now apart from break point there is one more concept as Watch point, it works on variable instead of function or line number. It will stop code execution when the value of variable on which watchpoint is set, has been changed. Syntax is
(gdb) watch variable_name
There are other command also
1. backtrace - produces a stack trace of the function calls that lead to a seg fault (should remind you of Java exceptions)
2. where - same as backtrace; you can think of this version as working even when you’re still in the middle of the program
3. finish - runs until the current function is finished
4. delete - deletes a specified breakpoint
5. info breakpoints - shows information about all declared
breakpoints
Also we can use condition in breakpoints, for example you want to break only when i < 1,so do like this :
(gdb) break program.c : 7 if i < 1.
No comments:
Post a Comment