gdb 簡易入門

imlihj2007發表於2011-10-16

--------------------------gdb..study first---------------------
--1 connect db and get processid
select spid from v$process where addr in (select paddr from v$session where sid=(select sid from v$mystat where rownum=1));
--16795
--2 gdb the prcesss
gdb -p 16795
---------------------
rac1-> gdb -p 16795
GNU gdb Red Hat Linux (6.3.0.0-1.162.el4rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome 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 "i386-redhat-linux-gnu".
Attaching to process 16795
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/bin/oracle...(no debugging symbols found)...done.
Using host libthread_db library "/lib/tls/libthread_db.so.1".
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libskgxp10.so...(no debugging symbols found)...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libskgxp10.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libhasgen10.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libhasgen10.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libskgxn2.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libskgxn2.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libocr10.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libocr10.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libocrb10.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libocrb10.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libocrutl10.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libocrutl10.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libjox10.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libjox10.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libclsra10.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libclsra10.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libdbcfg10.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libdbcfg10.so
Reading symbols from /u01/app/oracle/product/10.2.0/db_1/lib/libnnz10.so...done.
Loaded symbols for /u01/app/oracle/product/10.2.0/db_1/lib/libnnz10.so
Reading symbols from /usr/lib/libaio.so.1...done.
Loaded symbols for /usr/lib/libaio.so.1
Reading symbols from /lib/libdl.so.2...done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/tls/libm.so.6...done.
Loaded symbols for /lib/tls/libm.so.6
Reading symbols from /lib/tls/libpthread.so.0...done.
[Thread debugging using libthread_db enabled]
[New Thread -163010880 (LWP 16795)]
Loaded symbols for /lib/tls/libpthread.so.0
Reading symbols from /lib/libnsl.so.1...done.
Loaded symbols for /lib/libnsl.so.1
Reading symbols from /lib/tls/libc.so.6...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Reading symbols from /lib/libnss_files.so.2...done.
Loaded symbols for /lib/libnss_files.so.2
0x005107a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
--3. 在gdb中,在latch的獲取和釋放的函式上設定斷點
--3 create table t as select * from dba_objects
--and select count(*) from t
(gdb) b kslgpl
(gdb) b kslgetsl
(gdb) b kslgpl
(gdb) b kslg2c
(gdb) b kslg2csl
(gdb) b kslgprl
(gdb) b kslfre
(gdb) b kslf2c
(gdb) b kslfpl
(gdb) b kslfal
(gdb) b kslfpl0
(gdb)
--注:最後幾個函式只是猜測跟latch有關....latch的請求和釋放所呼叫的函式主要為:kslgetl、kslgetsl和
(gdb) b kslgpl
Breakpoint 1 at 0x83137df
(gdb) b kslgetsl
Breakpoint 2 at 0x8307bdd
(gdb) b kslgpl
Note: breakpoint 1 also set at pc 0x83137df.
Breakpoint 3 at 0x83137df
(gdb) b kslg2c
Breakpoint 4 at 0x8313c86
(gdb) b kslg2csl
Breakpoint 5 at 0x8313d4e
(gdb) b kslgprl
Breakpoint 6 at 0x8312786
(gdb) b kslfre
Breakpoint 7 at 0x83095d5
(gdb) b kslf2c
Breakpoint 8 at 0x8313e1e
(gdb) b kslfpl
Breakpoint 9 at 0x8313c16
(gdb) b kslfal
Breakpoint 10 at 0x830cffe
(gdb) b kslfpl0
Breakpoint 11 at 0x8313a79
(gdb) c
Continuing.
[Switching to Thread -163010880 (LWP 16795)]
Breakpoint 2, 0x08307bdd in kslgetsl ()
(gdb) info f
Stack level 0, frame at 0xfeffce30:
eip = 0x8307bdd in kslgetsl; saved eip 0x83e669c
called by frame at 0xfeffce6c
Arglist at 0xfeffce28, args:
Locals at 0xfeffce28, Previous frame's sp is 0xfeffce30
Saved registers:
ebp at 0xfeffce28, eip at 0xfeffce2c
(gdb) x /10 0xfeffce28
0xfeffce28: 0xfeffce64 0x083e669c 0x2af2c1f8 0x00000001
0xfeffce38: 0x2af0b458 0x00000050 0x00000008 0xfeffce64
0xfeffce48: 0x2af0b458 0x0cbc2b3c
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) info f
Stack level 0, frame at 0xfeffce40:
eip = 0x83095d5 in kslfre; saved eip 0x83e66d5
called by frame at 0xfeffce6c
Arglist at 0xfeffce38, args:
Locals at 0xfeffce38, Previous frame's sp is 0xfeffce40
Saved registers:
ebp at 0xfeffce38, eip at 0xfeffce3c
(gdb) x /10x 0xfeffce38
0xfeffce38: 0xfeffce64 0x083e66d5 0x2af2c1f8 0xfeffce64
0xfeffce48: 0x2af0b458 0x0cbc2b3c 0x0cbc2a40 0x00000002
0xfeffce58: 0x00000000 0x2ababf68
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) info f
Stack level 0, frame at 0xfeffba28:
eip = 0x83095d5 in kslfre; saved eip 0x8319eb3
called by frame at 0xfeffba40
Arglist at 0xfeffba20, args:
Locals at 0xfeffba20, Previous frame's sp is 0xfeffba28
Saved registers:
ebp at 0xfeffba20, eip at 0xfeffba24
(gdb) x /10x 0xfeffce38
0xfeffce38: 0x00000001 0xfeffed24 0x00000005 0xfeff0000
0xfeffce48: 0x2af000a4 0x0cbc0000 0x0cbc2a00 0x00000002
0xfeffce58: 0x0cbc2a40 0x0cbc2b3c
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) info f
Stack level 0, frame at 0xfeffba68:
eip = 0x83095d5 in kslfre; saved eip 0x8319eb3
called by frame at 0xfeffba80
Arglist at 0xfeffba60, args:
Locals at 0xfeffba60, Previous frame's sp is 0xfeffba68
Saved registers:
ebp at 0xfeffba60, eip at 0xfeffba64
(gdb) x /10x 0xfeffba60
0xfeffba60: 0xfeffba78 0x08319eb3 0x20095908 0x0cb740c0
0xfeffba70: 0x0cbc2a40 0x0000000c 0xfeffbad4 0x0abf08a1
0xfeffba80: 0x0cbc2a40 0x20095908
(gdb) x /10x $seb+8 -- 應該相當於資料庫開發過程中的除錯過程
Argument to arithmetic operation not a number or boolean.
(gdb) x /10x $sbp+8
Argument to arithmetic operation not a number or boolean.
(gdb) x /10x $ebp+8
0xfeffba68: 0x20095908 0x0cb740c0 0x0cbc2a40 0x0000000c
0xfeffba78: 0xfeffbad4 0x0abf08a1 0x0cbc2a40 0x20095908
0xfeffba88: 0x27490a98 0x20095908
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) x /10x $ebp+8
0xfeffb904: 0x297c3a74 0x0cb740c0 0xfeffc0dc 0x2ab271b4
0xfeffb914: 0xfeffbaac 0x0ae874a7 0x0cbc2a40 0x297c3a74
0xfeffb924: 0x00000096 0x4e9aeb9b
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) x /10x $ebp+8
0xfeffb890: 0x2a449840 0x00000000 0x00000000 0x2ab27210
0xfeffb8a0: 0x00000000 0x00000000 0x00000001 0x20001b28
0xfeffb8b0: 0x2ab27208 0x297c4214
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) info f
Stack level 0, frame at 0xfeffb4e4:
eip = 0x83095d5 in kslfre; saved eip 0x8319eb3
called by frame at 0xfeffb4fc
Arglist at 0xfeffb4dc, args:
Locals at 0xfeffb4dc, Previous frame's sp is 0xfeffb4e4
Saved registers:
ebp at 0xfeffb4dc, eip at 0xfeffb4e0
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) x /10x $ebp+8
0xfeffa8e0: 0x20095908 0x0cb740c0 0x00000000 0x2001b9ec
0xfeffa8f0: 0xfeffa97c 0x0abecbc1 0x0cbc2a40 0x20095908
0xfeffa900: 0x00000001 0x00000430
---Type to continue, or q to quit---q
Quit
(gdb) info f
Stack level 0, frame at 0xfeffa8e0:
eip = 0x83095d5 in kslfre; saved eip 0x8319eb3
called by frame at 0xfeffa8f8
Arglist at 0xfeffa8d8, args:
Locals at 0xfeffa8d8, Previous frame's sp is 0xfeffa8e0
Saved registers:
ebp at 0xfeffa8d8, eip at 0xfeffa8dc
(gdb) x /10x $ebp+8
0xfeffa8e0: 0x20095908 0x0cb740c0 0x00000000 0x2001b9ec
0xfeffa8f0: 0xfeffa97c 0x0abecbc1 0x0cbc2a40 0x20095908
0xfeffa900: 0x00000001 0x00000430
(gdb) x /10x $ebp+8
0xfeffa8e0: 0x20095908 0x0cb740c0 0x00000000 0x2001b9ec
0xfeffa8f0: 0xfeffa97c 0x0abecbc1 0x0cbc2a40 0x20095908
0xfeffa900: 0x00000001 0x00000430
(gdb) x /10x $ebp+8
0xfeffa8e0: 0x20095908 0x0cb740c0 0x00000000 0x2001b9ec
0xfeffa8f0: 0xfeffa97c 0x0abecbc1 0x0cbc2a40 0x20095908
0xfeffa900: 0x00000001 0x00000430
(gdb) c
Continuing.

Breakpoint 7, 0x083095d5 in kslfre ()
(gdb) x /10x $ebp+8
0xfeffa878: 0x20095908 0x0cb740c0 0x00000000 0x2001b9ec
0xfeffa888: 0xfeffa914 0x0abecbc1 0x0cbc2a40 0x20095908
0xfeffa898: 0x2a966454 0x00000430
(gdb) info f
Stack level 0, frame at 0xfeffa878:
eip = 0x83095d5 in kslfre; saved eip 0x8319eb3
called by frame at 0xfeffa890
Arglist at 0xfeffa870, args:
Locals at 0xfeffa870, Previous frame's sp is 0xfeffa878
Saved registers:
ebp at 0xfeffa870, eip at 0xfeffa874
(gdb) q
The program is running. Quit anyway (and detach it)? (y or n) y
Detaching from program: /u01/app/oracle/product/10.2.0/db_1/bin/oracle, process 16795'
---以上是設定記憶體斷點 進行驗證....內部函式 這個或許是一種方法而已,不打算研究很深


--------------------------gdb..study second---------------------
Linux GDB除錯基本用法

基本的使用方法簡介

  程式程式碼中的錯誤可分為數類,除了最容易除錯的語法錯誤,編譯程式會告訴你錯誤所在外,
大部分的錯誤都可以歸類為執行時錯誤。GDB 的功能便是尋找執行時錯誤。如果沒有除錯程式,
我們只能在程式中加入輸出變數值的指令來了解程式執行的狀態。有了 GDB 除錯程式,
我們可以設定在任何地方停止程式的執行,然後可以隨意檢視變數值及更動變數,並逐行執行程式。

  一個除錯程式執行的流程通常是這樣的:

  1. 進入除錯程式並指定可執行檔案。
  2. 指定程式程式碼所在目錄。
  3. 設定斷點後執行程式。
  4. 程式於斷點中斷後,可以 (1)檢視程式執行狀態;檢視變數值或變更變數值
(2) 逐步執行程式,或是全速執行程式到下一個斷點或是到程式結束為止。
  5. 離開除錯程式。

  以下將分為下列數項分別介紹:
  1. 進入 GDB 及指定可執行檔
  2. 指定程式程式碼所在目錄及檢視程式程式碼
  3. 斷點的設定與清除
  4. 全速及逐步執行程式
  5. 檢視及更改變數值
  6. 檢視程式執行狀態
  7. 讀取 Core 檔案資訊
  進入 GDB 及指定可執行檔:
  1. 進入 GDB 並讀入可執行檔 (檔名為 'PROGRAM'),準備進行除錯。

  gdb PROGRAM
  指定程式程式碼所在目錄及檢視程式程式碼
  1. 增加目錄 DIR 到收尋程式程式碼的目錄列表 (如果你的程式程式碼和可執行檔放在同一個目錄下,就不須指定程式程式碼所在目錄。):
  (gdb) directory DIR
  2. 檢視程式程式碼,格式計有:
  (gdb) list => 顯示目前執行程式程式碼前後各五行的程式程式碼;或是顯示從上次 list 之後的程式程式碼
  (gdb) list function => 顯示該程式開始處前後五行的程式程式碼。
  (gdb) list - =>上次顯示程式程式碼的前面的十行。
  斷點的設定與清除
  1. 設定斷點(指令為 break,可簡寫為 (b),格式計有:
  (gdb) break filename.c:30 => 在 filename.c 的第三十行處停止執行。
  (gdb) break function => 在進入 function 時中斷程式的執行。
  (gdb) break filename.c:function => 在程式程式碼檔 filename.c 中的函式 function 處設定斷點。
  (gdb) break => 在下一個將被執行的命令設定斷點。
  (gdb) break ... if cond => 只有當 cond 成立的時候才中斷。cond 須以 C 語言的語法寫成。
  2. 顯示各個斷點的資訊。
  (gdb) info break
  3. 清除斷點(命令為 clear),格式同 break 。例如 :
  (gdb) clear filename.c:30
  4. 清除斷點,NUM 是在 info break 顯示出來的斷點編號。
  (gdb) delete NUM
  全速及逐步執行程式
  1. 從程式開頭全速執行程式,直到遇到斷點或是程式執行完畢為止。
  (gdb) run
  2. 在程式被中斷後,全速執行程式到下一個斷點或是程式結束為止 (continue 指令可簡寫為 c)。
  (gdb) continue
  3. 執行一行程式. 若呼叫函式, 則將該包含該函式程式程式碼視為一行程式 (next 指令可簡寫為 n)。
  (gdb) next
  4. 執行一行程式. 若呼叫函式, 則進入函式逐行執行 (step 指令可簡寫為 s)。
  (gdb) step
  5. 執行一行程式,若此時程式是在 for/while/do loop 迴圈的最後一行,則一直執行到迴圈結束後的第一行程式後停止 (until 指令可簡寫為 u)。
  (gdb) until

  6. 執行現行程式到回到上一層程式為止。
  (gdb) finish
  檢視及更改變數值
  1. print 敘述,顯示該敘述執行的結果 (print 指令可簡寫為 p)。如
  (gdb) print a => 顯示 a 變數的內容.
  (gdb) print sizeof(a) => 顯示 a 變數的長度.
  2. display 敘述,在每個斷點或是每執行一步時顯示該敘述值。如
  (gdb) display a
  3. 更改變數值:
  (gdb) print (a=10) => 將變數 a 的值設定為 10.
  檢視程式執行狀態
  1. 檢視程式執行到此時,是經過哪些函式呼叫的程式 (backtrace 指令可簡寫為 bt),也就是檢視函式呼叫堆疊。
  (gdb) backtrace
  讀取 Core 檔案資訊
  1. 讀入 PROGRAM 及 PROGRAM.CORE 檔,可檢視 Core Dump 時程式變數值及程式流程狀態 。
  gdb PROGRAM core

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9879835/viewspace-1055732/,如需轉載,請註明出處,否則將追究法律責任。

相關文章