In this post I will briefly introduce how to use GDB(GNU Dbug tool) to debug caffe CPU code on Ubuntu.
1. Installation
Firstly you install GDB on you Ubuntu system, simplely type sudo apt-get install gdb
.
2. Build caffe with Debug mode
We have to build caffe with debug mode on, this can be done easily by uncommenting DEBUG := 1
in “Makefile.config, if you are familar with gcc compiler, it’s simplely add a -g
option when compling.
3. Using gdb to debug lenet as an example
Assume you are at root folder of caffe source:
As gdb cannot debug the GPU code, so you have to set solver mode to ‘CPU’ at the solver file(usually named solver.prototxt). In our example, this file is examples/mnist/lenet_solver.prototxt
, modify solver_mode=GPU
to CPU
.
-
Start debug lenet with
gdb --args build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt
The
--args
option indicates that you will take some arguments into the target. -
Set a breakpoint using
break
command:break conv_layer.cpp:30
(orb conv_layer.cpp:30
for short),choosey
When ask whether to pending from extra library.- Use of
break
:break FILE_NAME:LINE_NUMBER
- Or more complicate you can set conditional break point by
break FILE_NAME:LINE_NUMBER if (SOME CONDITION)
- Use of
- Start running by
run
(orr
for short). You will break at line30 ofconv_layer.cpp
, then you can access the intermediate value by print them.print top.size()
: print the number of top(output) blobs in conv_layer.print top[0]->shape()
: print shape of the first output blob of conv_layer.
More complicated function you should refer to information about GDB itself, because this post aim at telling you how to debug caffe, rather than a tutorial on how to use GDB.