[心得] 基本的 DBus 偵錯技巧

看板Linux作者 (Rex Tsai)時間15年前 (2011/03/16 22:31), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
原文網址 http://bit.ly/ijSHE1 [2]基本的 DBus 偵錯技巧 經過幾年發展,[3]DBus 已經取代早年 Linux 桌面環境所用的 [4]GNOME Bonobo, [5]KDE DCOP,用於許多[6]應用程式,成為主流 [7]IPC 系統。隨著軟體原件逐漸成熟,眾多程式 語言都已經支援 DBus APIs,DBus daemon 的 [8]footprint 也逐漸能夠被嵌入式所接受 ,而被行動裝置作業系統如 [9]MeeGo, [10]WebOS 所採用。 Linux 開發者難免因?介接需求,需要測試或使用 DBus 除錯。這裡分享幾個常用的小技 巧。DBus 使用[11]物件導向的 API 界面,所有 Services 的 Object 函式都是以 Method , Signals, [12]Properties 的概念揭露給外界存取,配合 [13]Introspectable API,很 容易讓第三方介接。 最常用到的工具之一是 [14]dbus-send,它可以用來從指令列測試接傳 DBus messages, 像是列出系統上所有註冊在 Session Bus 的 Services $ dbus-send --session --print-reply --reply-timeout=2000 \ --type=method_call --dest=org.freedesktop.DBus /org/freedesktop/DBus \ org.freedesktop.DBus.ListActivatableNames 有了 Services 名稱,接下來你就可以用 Service 為名以 [15]Introspection 界面去查 詢其所開放之 API,如 $ dbus-send --session --print-reply --reply-timeout=2000 --type=method_call \ --dest=org.gnome.ScreenSaver / org.freedesktop.DBus.Introspectable.Introspect method return sender=:1.7256 -> dest=:1.7286 reply_serial=2 string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "" rel="nofollow">http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> <node> <interface name="org.freedesktop.DBus.Introspectable"> <method name="Introspect"> <arg name="data" direction="out" type="s"/> </method> </interface> <interface name="org.gnome.ScreenSaver"> <method name="Lock"> </method> <method name="Cycle"> </method> <method name="SimulateUserActivity"> </method> <method name="Inhibit"> <arg name="application_name" direction="in" type="s"/> <arg name="reason" direction="in" type="s"/> <arg name="cookie" direction="out" type="u"/> </method> <method name="UnInhibit"> <arg name="cookie" direction="in" type="u"/> </method> <method name="GetInhibitors"> <arg name="list" direction="out" type="as"/> </method> ... <signal name="ActiveChanged"> <arg name="new_value" type="b"/> </signal> </interface> </node> " $ dbus-send --session --print-reply --reply-timeout=2000 \ --type=method_call --dest=org.gnome.Tomboy \ /org/gnome/Tomboy/RemoteControl org.freedesktop.DBus.Introspectable.Introspect string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "" rel="nofollow">http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> <!-- NDesk.DBus 0.6.0 --> <node> <interface name="org.freedesktop.DBus.Introspectable"> <method name="Introspect"> <arg name="data" direction="out" type="s" /> </method> </interface> ... <signal name="NoteSaved"> <arg name="uri" direction="out" type="s" /> </signal> </interface> </node>" 注意其中 object name 因各 Service 定義不同,首次查詢可從 / 開始查,Dbus 會答覆 其子路徑。 有了 API 界面之後,就可以直接用 dbus-send 送些指令給這些 Services 啦。如下啟動 螢幕保護程式。 dbus-send --session --dest=org.gnome.ScreenSaver \ --type=method_call --print-reply --reply-timeout=20000 \ / org.gnome.ScreenSaver.SetActive boolean:true 參數的格式可參考 DBus 規格中的 [16]Type Signatures。 如果你是在 Desktop 環境想查詢測試 DBus APIs, 比較容易的工具是使用 [17]d-feet, 這是 Python/GTK 所寫的工具,只消滑鼠點點就可以查詢各種 API 與送出指令訊息。(以 下圖片出自 [18]d-feet) [19][D-Feet-screenshot-cropped-300x192] 知道了基本的測試工具後,測試期間通常需要觀測訊息的傳送與反應是否正確,此時可以 利用 [20]dbus-monitor。它可以用來監測系統中所有的 Dbus messages,方便查詢軟體是 否運作正常。 由於安全性的考量,dbus-monitor 預設只能監錄 Session Bus 中的訊息,意即在每個 [21]Login session 中使用者所開啟的軟體。至於 System Bus,像是 [22]Network Manager 等預設是無法存取的,這是避免惡意軟體竊取隱私,如密碼等資訊。 必須手動開啟權限,設定方法是更改以下設定檔,開啟 eavesdrop policy,並重啟 Dbus daemon. 這樣才能竊聽相關通訊。 cat > /etc/dbus-1/system-local.conf <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" "" rel="nofollow">http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> <busconfig> <policy context="default"> <!-- All messages may be received by default --> <allow receive_requested_reply="false" receive_type="method_call" eavesdrop="true"/> <allow receive_requested_reply="false" receive_type="method_return" eavesdrop="true"/> <allow receive_requested_reply="false" receive_type="error" eavesdrop="true"/> <allow receive_requested_reply="false" receive_type="signal" eavesdrop="true"/> <allow eavesdrop="true"/> </policy> <policy user="root"> <allow send_destination="*" eavesdrop="true"/> <allow receive_sender="*" eavesdrop="true"/> </policy> </busconfig> 詳盡資訊請參考 o D-Bus Specification o [23]MeeGo D-Bus/Overview o [24]Scripting D-Bus References [1] http://people.debian.org.tw/~chihchun [2] http://people.debian.org.tw/~chihchun/2011/03/08/test-and-debug-dbus/ [3] http://www.freedesktop.org/wiki/Software/dbus [4] http://en.wikipedia.org/wiki/Bonobo_%28component_model%29 [5] http://en.wikipedia.org/wiki/DCOP [6] http://freedesktop.org/wiki/Software/DbusProjects [7] http://en.wikipedia.org/wiki/Inter-process_communication [8] http://en.wikipedia.org/wiki/Memory_footprint [9] http://wiki.meego.com/D-Bus/Overview [10] http://www.webos-internals.org/wiki/Introspecting_Dbus [11] http://en.wikipedia.org/wiki/Object-oriented_programming [12] http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties [13] http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-introspectable [14] http://dbus.freedesktop.org/doc/dbus-send.1.html [15] http://en.wikipedia.org/wiki/Type_introspection [16] http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures [17] https://fedorahosted.org/d-feet/ [18] https://live.gnome.org/DFeet/ [19] http://people.debian.org.tw/~chihchun/wp-content/uploads/2011/03/D-Feet-screenshot-cropped.png
[20] http://dbus.freedesktop.org/doc/dbus-monitor.1.html [21] http://en.wikipedia.org/wiki/Login_session [22] http://projects.gnome.org/NetworkManager/ [23] http://wiki.meego.com/D-Bus/Overview [24] http://blog.fpmurphy.com/2009/02/dbus-scripting.html -- http://people.debian.org.tw/~chihchun/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 112.104.53.166

03/17 13:21, , 1F
謝謝你的心得分享~
03/17 13:21, 1F
文章代碼(AID): #1DWCdCmD (Linux)
文章代碼(AID): #1DWCdCmD (Linux)