天天看點

opensips/kamailio gdb代碼調試方法詳解

http://www.bkjia.com/Androidjc/884078.html

    要對kamailio/opensips進行單步調試,就需要先了解其代碼的結構及運作方式,kamailio/opensips使用Reactor和Proactor結合的IO網絡模型,使用主程序負責監聽網絡,當有連接配接産生或首包到達時,就通過pipe将檔案描述符發送給worker程序,worker程序就會負責此連接配接的資料取讀、業務處理、資料發送等事情,然後再次等待此socket事件。當我們想要調試一段代碼時,就先确認這段代碼是運作在什麼類型的程序中,通常用于處理SIP邏輯的代碼都是在worker程序中執行的,下面是一個kamailio程序啟動的執行個體:

[[email protected] sipserver]# kamctl ps

Process::  ID=0 PID=6651 Type=attendant

Process::  ID=1 PID=6653 Type=udp receiver child=0 sock=172.16.0.16:53

Process::  ID=2 PID=6654 Type=udp receiver child=1 sock=172.16.0.16:53Process::  ID=3 PID=6656 Type=udp receiver child=2 sock=172.16.0.16:53Process::  ID=4 PID=6658 Type=udp receiver child=3 sock=172.16.0.16:53Process::  ID=5 PID=6661 Type=slow timer

Process::  ID=6 PID=6662 Type=timer

Process::  ID=7 PID=6665 Type=MI FIFO

Process::  ID=8 PID=6668 Type=ctl handler

Process::  ID=9 PID=6669 Type=SNMP AgentX

Process::  ID=10 PID=6672 Type=tcp receiver (generic) child=0

Process::  ID=11 PID=6674 Type=tcp receiver (generic) child=1

Process::  ID=12 PID=6676 Type=tcp receiver (generic) child=2

Process::  ID=13 PID=6677 Type=tcp receiver (generic) child=3

Process::  ID=14 PID=6679 Type=tcp main process上例中Type=tcp receiver和udp receiver這兩種類型的程序都是處理SIP消息的,包括kamailio.cfg配置中route段都是在此程序中執行,通常我們為對SIP協定進行處理而開發的子產品也都在此程序中執行。kamailio和opensips在代碼核心架構上是一緻的,下面以kamailio為例說明如何使用gdb進行調試。調試程式有兩種方法:方法1直接用GDB運作kamailio:将kamailio.cfg配置為fork=no, 這樣kamailio會以單程序方式運作,是以的邏輯都将在一個程序裡執行,但單程序模式不支援TCP listen,是以要調試TCP邏輯,不能使用此方式。直接使用gdb調試kamailio主程式就行:[[email protected] ~]# gdb /usr/sbin/kamailio

GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-45.el5)

Copyright (C) 2009 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>...

Reading symbols from /usr/sbin/kamailio...done.(gdb) b main.c:10Breakpoint 1 at 0x45725c: file main.c, line 10.

(gdb) run

    方法2 将kamailio運作起來後,再用gdb attach調試:第一種方法的缺點非常明顯,不能調試TCP程序,也不能将kamailio多程序的優勢發揮出來。将kamailio.cfg配置為fork=yes将以多程序方式啟動,與此相關的參數還有兩個:children=1            ;配置處理udp端口資料的程序數量tcp_children=1     ;配置處理tcp端口資料的程序資料,如果此參數未配置,預設使用children參數。如果使用以上參數啟動程式,如果listen參數有對應的端口,将會啟動1個UDP處理程序和1個TCP處理程序,執行個體如下:

[[email protected] sipserver]# kamctl ps

Process::  ID=0 PID=6651 Type=attendant

Process::  ID=1 PID=6653 Type=udp receiver child=0 sock=172.16.0.16:53

Process::  ID=5 PID=6661 Type=slow timer

Process::  ID=6 PID=6662 Type=timer

Process::  ID=7 PID=6665 Type=MI FIFO

Process::  ID=8 PID=6668 Type=ctl handler

Process::  ID=9 PID=6669 Type=SNMP AgentX

Process::  ID=10 PID=6672 Type=tcp receiver (generic) child=0

Process::  ID=14 PID=6679 Type=tcp main process當kamailio收到TCP資料時,會由6672程序進行處理,我們可以使用gdb attach到指定程序進行調試,執行個體如下:[[email protected] ~]# gdb

GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-45.el5)

Copyright (C) 2009 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>.

(gdb) attach 6672 

Attaching to process 6672 ......(gdb) b redis_base.c:100

Breakpoint 1 at 0x2b6c45e70082: file redis_base.c, line 100.

(gdb) c

Continuing.

然後再設定斷點,執行run即可,當伺服器收到SIP消息并運作到指定代碼即可單步調試,在調試過程中再結合日志輸出,對定位問題非常有幫助。關于如何使用gdb請參考文檔:http://wenku.baidu.com/link?url=UNUB8A6f2OGt_KJxU4p7-df-j9QFw2z2CFN6nCj4smzijx_UiMIkUTFh7JlOE-byIylp2yGfaTbtKPpOGR3lhTd09p3NTQtZDPrWp9N5pmu

新浪微網誌:@安靜的發狂者郵箱:dennis.yu(@)live.cnQQ:229675152專注于移動網際網路音視訊通信領域,歡迎交流;本文為原創,轉載請保留版權并聯系作者

繼續閱讀