Difference between revisions of "Using gdb and valgrind"
m (left one) |
m (layout and links) |
||
Line 1: | Line 1: | ||
− | <center><h2>by | + | <center><h2>by ''Phoenix''</h2></center> |
− | The two programs, | + | The two programs, [http://www.gnu.org/software/gdb/gdb.html GDB] and [http://valgrind.kde.org Valgrind], are fabulous tools to find errors in your code. [http://www.gnu.org/software/gdb/gdb.html GDB] is the standard gnu debugger, very powerfull, virtually every debugger in [http://www.gnu.org GNU] is [http://www.gnu.org/software/gdb/gdb.html GDB]-based. So, you need to learn some [http://www.gnu.org/software/gdb/gdb.html GDB]. [http://valgrind.kde.org Valgrind] is another invaluable tool, that allows you to track memory usage, stopping the program whenever an invalid operation has been made. |
− | + | [http://valgrind.kde.org Valgrind] can be used together with [http://www.gnu.org/software/gdb/gdb.html GDB], so that you can actually debug the program that is beeing analysed. This is possible due to a [http://www.gnu.org/software/gdb/gdb.html GDB] feature that makes it possible to attach any running process to a [http://www.gnu.org/software/gdb/gdb.html GDB] session. | |
− | The first step is to setup | + | The first step is to setup '''Valgrind''', but first, some interesting links about [http://valgrind.kde.org Valgrind]: |
* Home page: http://valgrind.kde.org/ | * Home page: http://valgrind.kde.org/ | ||
Line 13: | Line 13: | ||
* Nice article explaining how to use: http://www.linux-mag.com/2003-05/compile_03.html | * Nice article explaining how to use: http://www.linux-mag.com/2003-05/compile_03.html | ||
− | == Running Valgrind == | + | == Running [http://valgrind.kde.org Valgrind] == |
---- | ---- | ||
Line 20: | Line 20: | ||
<code>valgrind -v --leak-check=yes --gdb-attach=yes --num-callers=10 --suppressions=amule.sup --gen-suppressions=yes ~/programs/amule/amule-dev-cvs/src/amule --enable-stdin</code> | <code>valgrind -v --leak-check=yes --gdb-attach=yes --num-callers=10 --suppressions=amule.sup --gen-suppressions=yes ~/programs/amule/amule-dev-cvs/src/amule --enable-stdin</code> | ||
− | This way, leak-check is enabled, and whenever a problem is detected, the user will be prompted for whether he wants to start gdb at the current position. This is a great moment to see the beast exactly at the moment of falilure. The parameter --num-callers, which defaults to 4, is the number of backtrace lines that valgrind shows you, 4 is too little. | + | This way, leak-check is enabled, and whenever a problem is detected, the user will be prompted for whether he wants to start [http://www.gnu.org/software/gdb/gdb.html GDB] at the current position. This is a great moment to see the beast exactly at the moment of falilure. The parameter ''--num-callers'', which defaults to 4, is the number of backtrace lines that [http://valgrind.kde.org Valgrind] shows you, 4 is too little. |
− | + | Finally, there is ''--gen-suppressions'' and ''--suppressions''. Suppressions are a way that [http://valgrind.kde.org Valgrind] provides us so that we don't have to see '''every''' single problem that the program has. Eventually, other libraries your program is using, like X itself, will show memory usage problems, and [http://valgrind.kde.org Valgrind] will show them. So, on the first few runs of [http://valgrind.kde.org Valgrind], we will have to build a suppression file, which is specified by ''--suppressions='' and we tell [http://valgrind.kde.org Valgrind] to generate suppressions on screen. ''--gen-suppressions=yes'' will do this. Suppressions are a few lines that you may copy/paste in the suppressions file, all you have to do is give each suppression a name and that's it. | |
− | When set up that way, each time valgrind finds something wrong, he will show you a few lines describing the problem and them he will ask you if you want him to generate the suppressions. If you say yes, the suppression appears on the screen, and you can copy/paste it in the suppressions file, so that next time you run valgring this error is not reported. | + | When set up that way, each time [http://valgrind.kde.org Valgrind] finds something wrong, he will show you a few lines describing the problem and them he will ask you if you want him to generate the suppressions. If you say yes, the suppression appears on the screen, and you can copy/paste it in the suppressions file, so that next time you run valgring this error is not reported. |
− | Now, after prompting the user for printing the suppression, valgrind will prompt the user again whether or not he should start gdb. If you say | + | Now, after prompting the user for printing the suppression, [http://valgrind.kde.org Valgrind] will prompt the user again whether or not he should start [http://www.gnu.org/software/gdb/gdb.html GDB]. If you say accept, [http://www.gnu.org/software/gdb/gdb.html GDB] will attach the running process and you will be debugging the program exactly at the point of the invalid operation. |
− | == A few gdb usefull commands == | + | == A few [http://www.gnu.org/software/gdb/gdb.html GDB] usefull commands == |
---- | ---- | ||
;'''''bt''''': backtrace, shows the stack frames (subroutine calls) that lead to the position you are now in code. | ;'''''bt''''': backtrace, shows the stack frames (subroutine calls) that lead to the position you are now in code. | ||
;'''''bt full''''': shows every frame with the values of the local variables, usually too much verbose, only do this if you want to send the output to another person to analyse. | ;'''''bt full''''': shows every frame with the values of the local variables, usually too much verbose, only do this if you want to send the output to another person to analyse. | ||
− | ;'''''up/down number''''': e.g.: ''up 5'': goes up 5 stack frames. By default, whenever you enter gdb, you will be placed in frame number 0. '''With the aid of the source code of the program''' you should be able to select the subroutine where you want to inspect the variables. | + | ;'''''up/down number''''': e.g.: ''up 5'': goes up 5 stack frames. By default, whenever you enter [http://www.gnu.org/software/gdb/gdb.html GDB], you will be placed in frame number 0. '''With the aid of the source code of the program''' you should be able to select the subroutine where you want to inspect the variables. |
;'''''frame number''''': goes straight to the selected frame. | ;'''''frame number''''': goes straight to the selected frame. | ||
;'''''info locals''''': prints the value of all local variables belonging to the current frame. | ;'''''info locals''''': prints the value of all local variables belonging to the current frame. | ||
Line 39: | Line 39: | ||
;'''''help/help command''''': Do I need to explain that? :) | ;'''''help/help command''''': Do I need to explain that? :) | ||
− | There are other commands, but usualy these described will suffice. It's worth mentioning that gdb can be run with the core file, i.e., that big file that is produced after a crash. To do that, | + | There are other commands, but usualy these described will suffice. It's worth mentioning that [http://www.gnu.org/software/gdb/gdb.html GDB] can be run with the core file, i.e., that big file that is produced after a crash. To do that, type |
− | ''gdb program_path core_file_path | + | ''gdb '''program_path''' '''core_file_path''''' |
− | Also there are | + | Also there are GUI front ends to [http://www.gnu.org/software/gdb/gdb.html GDB] that make it a little bit more friendly (like http://kgdb.sourceforge.net kdbg]). GUI front ends are great to inspect several different variables by clicking with the mouse. But learning to use [http://www.gnu.org/software/gdb/gdb.html GDB] from the console is by far the fastest way to do the job, not to mention that you can do it '''quickly''' in a remote terminal session. |
− | Also note that practice makes it perfect, so the only way to realy learn how to do this is by doing it. You will see that you will quickly learn how to move around | + | Also note that practice makes it perfect, so the only way to realy learn how to do this is by doing it. You will see that you will quickly learn how to move around [http://www.gnu.org/software/gdb/gdb.html GDB], in spite of his daunting console interface. Soon he will become your friend. :) |
Revision as of 07:27, 17 July 2004
by Phoenix
The two programs, GDB and Valgrind, are fabulous tools to find errors in your code. GDB is the standard gnu debugger, very powerfull, virtually every debugger in GNU is GDB-based. So, you need to learn some GDB. Valgrind is another invaluable tool, that allows you to track memory usage, stopping the program whenever an invalid operation has been made.
Valgrind can be used together with GDB, so that you can actually debug the program that is beeing analysed. This is possible due to a GDB feature that makes it possible to attach any running process to a GDB session.
The first step is to setup Valgrind, but first, some interesting links about Valgrind:
- Home page: http://valgrind.kde.org/
- Documentation: http://developer.kde.org/~sewardj/docs-2.0.0/manual.html
- How To: http://www.tldp.org/HOWTO/Valgrind-HOWTO/
- FAQ: http://valgrind.kde.org/faq.html
- Nice article explaining how to use: http://www.linux-mag.com/2003-05/compile_03.html
Running Valgrind
I personaly use the following call:
valgrind -v --leak-check=yes --gdb-attach=yes --num-callers=10 --suppressions=amule.sup --gen-suppressions=yes ~/programs/amule/amule-dev-cvs/src/amule --enable-stdin
This way, leak-check is enabled, and whenever a problem is detected, the user will be prompted for whether he wants to start GDB at the current position. This is a great moment to see the beast exactly at the moment of falilure. The parameter --num-callers, which defaults to 4, is the number of backtrace lines that Valgrind shows you, 4 is too little.
Finally, there is --gen-suppressions and --suppressions. Suppressions are a way that Valgrind provides us so that we don't have to see every single problem that the program has. Eventually, other libraries your program is using, like X itself, will show memory usage problems, and Valgrind will show them. So, on the first few runs of Valgrind, we will have to build a suppression file, which is specified by --suppressions= and we tell Valgrind to generate suppressions on screen. --gen-suppressions=yes will do this. Suppressions are a few lines that you may copy/paste in the suppressions file, all you have to do is give each suppression a name and that's it.
When set up that way, each time Valgrind finds something wrong, he will show you a few lines describing the problem and them he will ask you if you want him to generate the suppressions. If you say yes, the suppression appears on the screen, and you can copy/paste it in the suppressions file, so that next time you run valgring this error is not reported.
Now, after prompting the user for printing the suppression, Valgrind will prompt the user again whether or not he should start GDB. If you say accept, GDB will attach the running process and you will be debugging the program exactly at the point of the invalid operation.
A few GDB usefull commands
- bt
- backtrace, shows the stack frames (subroutine calls) that lead to the position you are now in code.
- bt full
- shows every frame with the values of the local variables, usually too much verbose, only do this if you want to send the output to another person to analyse.
- up/down number
- e.g.: up 5: goes up 5 stack frames. By default, whenever you enter GDB, you will be placed in frame number 0. With the aid of the source code of the program you should be able to select the subroutine where you want to inspect the variables.
- frame number
- goes straight to the selected frame.
- info locals
- prints the value of all local variables belonging to the current frame.
- p variable
- e.g.: p *cur_src. Prints the named variable. Usually C like syntax will be allowed here.
- l
- prints 10 lines of source code around the current line. A separate editor for source browsing is usualy better, but this can be a quick way to see the code.
- help/help command
- Do I need to explain that? :)
There are other commands, but usualy these described will suffice. It's worth mentioning that GDB can be run with the core file, i.e., that big file that is produced after a crash. To do that, type
gdb program_path core_file_path
Also there are GUI front ends to GDB that make it a little bit more friendly (like http://kgdb.sourceforge.net kdbg]). GUI front ends are great to inspect several different variables by clicking with the mouse. But learning to use GDB from the console is by far the fastest way to do the job, not to mention that you can do it quickly in a remote terminal session.
Also note that practice makes it perfect, so the only way to realy learn how to do this is by doing it. You will see that you will quickly learn how to move around GDB, in spite of his daunting console interface. Soon he will become your friend. :)