习惯了VS编程的同学转到linux下编程和调试都不太方便呢,大神们都用vim和gdb,而我还是选择折腾半天用ide来coding和debug…
最近用eclipse看Caffe代码,确实觉得看源码的话还是用ide方便点,想查看某个变量的定义,一个快捷键就过去了,再一个快捷键就又回来了,而不用在命令行里查找到定义转过去看再找回来,总怕迷路。。
不过今天用eclipse调试c++程序还是花了点功夫,最后觉得还不如直接用gdb调试方便,哈哈,难道我也变成喜欢用命令行的高级程序猿了? 不,我应该还是用20%时间code,80%时间debug的底层程序猿。。
eclipse attach to process 调试的话就如下图:
在run-debug configuration里调出来,可以看到所有调试的程序都是在这里定义的。
这里我想用attach to application调试,也就是VS里大家熟悉的attach to process啦。然后关于这个窗口里的所有功能,我也并不是很懂,都靠一个一个试跟猜的,最后总算是debug到了正在运行的caffe test程序,大概就是像截图那样定位到要调试的命令,其他的好像默认就行了,遇到过几个问题:
attach to process时报错:
Could not attach to process. If your uid matches the uid of the target process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf ptrace: Operation not permitted.
这是个权限问题
参考http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process
修改 /etc/sysctl.d/10-ptrace.conf 文件里的
kernel.yama.ptrace_scope = 1
为kernel.yama.ptrace_scope = 0
或
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
获得暂时权限还有挺多。。。。记不清了,大多数google可以解决的
总之有时候这种东西,得靠运气,我也讲不清楚最后到底怎么调通的。
attach到caffe test命令后,就如下图:
感觉终于可以清晰地查看caffe的数据结构了,嵌套了那么多层的指针终于不用在gdb里一个一个*出来看了,不过就debug了一小会,eclipse的cdt插件有问题吧,variables栏里的变量都消失了,就是你看不到字和地址,但还是能点到它。。所以就在eclipse的console里继续运行gdb命令调试了,然后就突然发现gdb其实还是挺好用的
p bottom[0]
$1 = (caffe::Blob<float> *) 0x18f26280
p *(bottom[0])
$2 = {data_ = {px = 0x18f23db0, pn = {pi_ = 0x18f24a40}}, diff_ = {px = 0x18f259e0, pn = {pi_ = 0x18f26990}}, shape_data_ = {px = 0x18f25980, pn = {pi_ = 0x18f25f30}}, shape_ = std::vector of length 2, capacity 2 = {
200, 2}, count_ = 400, capacity_ = 400}p bottom[0].data_
$5 = {px = 0x18f23db0, pn = {pi_ = 0x18f24a40}}p bottom[0].data_.px
$6 = (boost::shared_ptr<caffe::SyncedMemory>::element_type *) 0x18f23db0p bottom[0].data_.pn
$7 = {pi_ = 0x18f24a40}p bottom[0].data_.pn.pi_
$8 = (boost::detail::sp_counted_impl_p<caffe::SyncedMemory> *) 0x18f24a40p *(bottom[0].data_.px)
$11 = {cpu_ptr_ = 0x1020a402a00, gpu_ptr_ = 0x10213000000, size_ = 1600, head_ = caffe::SyncedMemory::HEAD_AT_GPU, own_cpu_data_ = true, cpu_malloc_use_cuda_ = true, own_gpu_data_ = true, gpu_device_ = 0}p *((*(bottom[1])).data_.px)
$41 = {cpu_ptr_ = 0x1020860bc00, gpu_ptr_ = 0x10208007200, size_ = 800, head_ = caffe::SyncedMemory::HEAD_AT_GPU, own_cpu_data_ = true, cpu_malloc_use_cuda_ = true, own_gpu_data_ = true, gpu_device_ = 0}p (*(bottom[0].data_.px)).size_
$12 = 1600p *((float*)0x10208007200)@200 //我的test batch是200
$45 = {
0 <repeats 200 times>}p *((float *) 0x10213000000)@200
$16 = {
0 <repeats 200 times>}p *((float*)0x1020860bc00)@200
$44 = {
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 <repeats 18 times>, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0 <repeats 25 times>, 1, 0, 0, 1, 0 <repeats 13 times>, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0 <repeats 32 times>, 1, 0 <repeats 14 times>, 1, 0, 1, 0 <repeats 13 times>, 1, 0 <repeats 12 times>, 1, 0, 1, 0, 0}p *((float*)0x1020a402a00)@400
$19 = {
4.78650761, -4.7421093, 3.12269378, -3.14344192, 2.31754971, -2.33390522, -1.2435168, 1.28806901, 1.03970253, -1.06744337, 0.555218518, -0.562352717, 2.9875927, -3.0197072, 2.01207781, -2.02964878, 2.16913319, -2.1898067, 4.16555309, -4.12213516, 2.09989023, -2.12747359, -0.947544992, 0.987294137, 1.44577825, -1.45407832, 0.929420888, -0.93540293, 4.85911512, -4.8141551, -0.939215004, 0.975851238, 4.95238924, -4.91008759, 4.84604311, -4.80103207, 4.84232807, -4.79724741, 1.87894046, -1.88940215, 1.44175196, -1.42660546, 1.99126816, -2.00464535, 4.83181763, -4.78731918, 4.80154133, -4.75688696, 2.21910715, -2.23471236, 2.07072353, -2.09080362, 0.956796587, -0.968076527, 1.25807989, -1.24478781, 2.25229812, -2.26760745, 2.03899932, -2.05396581, 2.43122721, -2.45933867, 4.83268642, -4.78767633, 3.00421977, -2.98535705, 1.52376819, -1.52473664, -0.55711484, 0.595759749, -1.74641323, 1.82676637, 4.80526114, -4.76053762, 1.7961216, -1.81073916, 4.81223249, -4.7676754, 2.03819323, -2.05352426, 0.390252352, -0.402691036, 4.84600592, -4.80104494, 0.256235868, -0.226787716, -0.127874374, 0.170145392, 0.220693976, -0.190397635, 3.08126497, -3.09196281, 1.81312668, -1.8304013, 2.0143044, -2.02224422, 2.39664674, -2.42595935, 1.85862899, -1.86523247, 1.32934928, -1.37163091, 2.05775476, -2.04366517, 4.8015604, -4.75740242, 2.55886269, -2.58293796, 2.42973018, -2.44261789, 2.36269593, -2.38659286, 2.40934396, -2.43213892, 4.56758833, -4.54119587, 1.90870714, -1.91162217, -0.57104522, 0.594657838, 2.34378457, -2.29764199, 4.82635736, -4.7811017, 3.56734848, -3.5122788, 1.88475311, -1.89801395, 0.506634951, -0.49810645, 2.18225026, -2.22121596, 1.63964081, -1.63528061, 1.75569677, -1.69575357, -2.00355029, 2.06677222, 4.78930712, -4.74517727, 0.854989946, -0.787993133, 0.755178034, -0.78094542, 2.52562261, -2.56160784, -0.265122503, 0.2786282, 2.31916595, -2.33949041, 1.58546066, -1.61469269, 2.4022162, -2.38045526, 2.03355956, -2.05004787, 2.53933024, -2.56596375, 0.952844858, -0.940357924, 4.8454299, -4.80025625, 4.84715605, -4.80216455, 4.82812738, -4.78480673, 4.8510747, -4.80572653, 2.83262253, -2.82969761, -1.25599003, 1.28918505, 2.06752133, -2.09038949, 2.18665862, -2.15507078, 4.83977222, -4.79502106, 1.94258714, -1.96585834, 4.85405397, -4.8088336, 2.03362656, -2.05115795, 4.81647444, -4.77204084, 2.34646344, -2.37259412, 2.11363649, -2.13865781, -0.50652808, 0.537424207, 4.90132141, -4.86209202, 4.80105877, -4.75669384, 1.60057652, -1.60887361, 4.83262873, -4.78797197...}
//值存在CPU_data里
这样在eclipse里跑gdb真是多次一举啊,以后直接命令行gdb /home/zfq/caffe/build/tools/caffe
吧,感觉也还可以,终于慢慢摆脱VS的使用习惯了呢。。
(不过vs都出mac版了,linux版还远吗)
这样跟着程序跑一遍以后,对caffe的流程更加熟悉了,看懂别人代码最快捷的方式应该就是debug跟着数据流跑一遍吧~