1. 程式人生 > >gdb attach到已經存在的程序進行線上除錯------能獲取當前棧的所有變數值

gdb attach到已經存在的程序進行線上除錯------能獲取當前棧的所有變數值

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

       不久前, 我們玩過valgrind定位程式的各類記憶體問題, 當時非常希望valgrind能對一個已經存在的程序進行attach線上除錯, 後來看了valgrind的原理, 發現這樣不可行。 只能讓valgrind重新拉取新程序進行除錯。

       在前面的文章中, 也介紹了kill -6和abort函式, 讓程式core dump來獲取當時棧的值, 其實, 這樣似乎沒有必要。 慢慢來看。

       我們再回憶下strace命令, 可以用strace -p pid, 附著到一個已經在跑的程序上, 實時觀察程序。 那麼, gdb能做到嗎? 能! 這就是gdb的attch除錯, 可以獲取當前棧的所有變數值。

    

       回憶一下, 比如編譯(加-g)後生成了a.out檔案:

       1.  可以直接用gdb a.out來除錯,  其實gdb拉取了新的程序。 類似於valgrind.

       2.  如果有core檔案, 那麼用gdb a.out core,  此時沒有拉取新的程序。 在實際開發中, 這種方式常用。

 

       很多時候, 在服務端, 程序已經跑起來, 此時不再適合用gdb來拉取新的程序, 而期望用gdb附著到這個已經存在的程序, 如下兩種方式是等價的:

       3. gdb attach pid  (注意, 沒有拉取新的程序)

       4. gdb a.out pid    (注意, 沒有拉取新的程序)

 

       看程式:

 

#include <iostream>#include <cmath>using namespace std;struct Point{ int x; int y;};int main() { Point po; po.x = 1; po.y = 2float distance = sqrt(po.x * po.x + po.y * po.y);  getchar(); return 0;}

      看看利用方法3,4來除錯(程序執行在一個視窗, 除錯在另外一個視窗):

 

 

xxxxxx $ ps -aux | grep a.outWarning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html1000      3265  0.0  0.0   2804   876 pts/1    S+   08:18   0:00 ./a.out1000     20400  0.0  0.0   3072   804 pts/0    S+   21:02   0:00 grep a.outxxxxxx $ xxxxxx $ xxxxxx $ xxxxxx $ gdb attach 3265GNU gdb 6.6Copyright (C) 2006 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB.  Type "show warranty" for details.This GDB was configured as "i586-suse-linux"...attach: No such file or directory.Attaching to process 3265Reading symbols from /data/home/xxxxxx/a.out...done.Using host libthread_db library "/lib/libthread_db.so.1".Reading symbols from /lib/libonion.so...done.Loaded symbols for /lib/libonion.soReading symbols from /usr/lib/libstdc++.so.6...done.Loaded symbols for /usr/lib/libstdc++.so.6Reading symbols from /lib/libm.so.6...done.Loaded symbols for /lib/libm.so.6Reading symbols from /lib/libgcc_s.so.1...done.Loaded symbols for /lib/libgcc_s.so.1Reading symbols from /lib/libc.so.6...done.Loaded symbols for /lib/libc.so.6Reading symbols from /lib/libdl.so.2...done.Loaded symbols for /lib/libdl.so.2Reading symbols from /lib/libpthread.so.0...done.[Thread debugging using libthread_db enabled][New Thread -1211562320 (LWP 3265)]Loaded symbols for /lib/libpthread.so.0Reading symbols from /lib/ld-linux.so.2...done.Loaded symbols for /lib/ld-linux.so.20xb7d5eb0e in __read_nocancel () from /lib/libc.so.6(gdb) bt#0  0xb7d5eb0e in __read_nocancel () from /lib/libc.so.6#1  0xb7d0e0d8 in _IO_file_read_internal () from /lib/libc.so.6#2  0xb7d0f35b in _IO_new_file_underflow () from /lib/libc.so.6#3  0xb7d0fa9b in _IO_default_uflow_internal () from /lib/libc.so.6#4  0xb7d10ddd in __uflow () from /lib/libc.so.6#5  0xb7d0aebe in getchar () from /lib/libc.so.6#6  0x0804867c in main () at test.cpp:18(gdb) f 6#6  0x0804867c in main () at test.cpp:1818              getchar();(gdb) i localspo = {x = 1, y = 2}distance = 2.23606801(gdb) qThe program is running.  Quit anyway (and detach it)? (y or n) yDetaching from program: /data/home/xxxxxx/a.out, process 3265xxxxxx $ xxxxxx $ xxxxxx $ xxxxxx $ xxxxxx $ xxxxxx $ xxxxxx $ gdb a.out 3265GNU gdb 6.6Copyright (C) 2006 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB.  Type "show warranty" for details.This GDB was configured as "i586-suse-linux"...Using host libthread_db library "/lib/libthread_db.so.1".Attaching to program: /data/home/xxxxxx/a.out, process 3265Reading symbols from /lib/libonion.so...done.Loaded symbols for /lib/libonion.soReading symbols from /usr/lib/libstdc++.so.6...done.Loaded symbols for /usr/lib/libstdc++.so.6Reading symbols from /lib/libm.so.6...done.Loaded symbols for /lib/libm.so.6Reading symbols from /lib/libgcc_s.so.1...done.Loaded symbols for /lib/libgcc_s.so.1Reading symbols from /lib/libc.so.6...done.Loaded symbols for /lib/libc.so.6Reading symbols from /lib/libdl.so.2...done.Loaded symbols for /lib/libdl.so.2Reading symbols from /lib/libpthread.so.0...done.[Thread debugging using libthread_db enabled][New Thread -1211562320 (LWP 3265)]Loaded symbols for /lib/libpthread.so.0Reading symbols from /lib/ld-linux.so.2...done.Loaded symbols for /lib/ld-linux.so.20xb7d5eb0e in __read_nocancel () from /lib/libc.so.6(gdb) bt#0  0xb7d5eb0e in __read_nocancel () from /lib/libc.so.6#1  0xb7d0e0d8 in _IO_file_read_internal () from /lib/libc.so.6#2  0xb7d0f35b in _IO_new_file_underflow () from /lib/libc.so.6#3  0xb7d0fa9b in _IO_default_uflow_internal () from /lib/libc.so.6#4  0xb7d10ddd in __uflow () from /lib/libc.so.6#5  0xb7d0aebe in getchar () from /lib/libc.so.6#6  0x0804867c in main () at test.cpp:18(gdb) f 6#6  0x0804867c in main () at test.cpp:1818              getchar();(gdb) i localspo = {x = 1, y = 2}distance = 2.23606801(gdb) 


       好, 介紹完畢。 簡單。

 

 

 

 

 

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述