1. 程式人生 > >Linux eclipse coding and debug caffe

Linux eclipse coding and debug caffe

習慣了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 = 1kernel.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 *) 0x18f23db0

p bottom[0].data_.pn
$7 = {pi_ = 0x18f24a40}

p bottom[0].data_.pn.pi_
$8 = (boost::detail::sp_counted_impl_p<caffe::SyncedMemory> *) 0x18f24a40

p *(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 = 1600

p  *((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跟著資料流跑一遍吧~