基本的 DBus 偵錯技巧 - Linux

Skylar DavisLinda avatar
By Skylar DavisLinda
at 2011-03-16T22:31

Table of Contents

原文網址 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"
"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" "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"
"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/

--
Tags: Linux

All Comments

Carol avatar
By Carol
at 2011-03-19T12:24
謝謝你的心得分享~

在Fedora 14 x86_64安裝tar1.13

Todd Johnson avatar
By Todd Johnson
at 2011-03-16T22:14
最近改到Fedora 14 X86_64的環境下。因為有軟體需要用到tar1.13的版 本,重灌了幾次之後,終於成功了.以下是我的步驟,我不確定哪些步驟是 多餘的,總之寫下來給大家參考。 1.重灌fedora。 灌好之後請愛用and#34;yum -y update andamp;andamp; reb ...

mknod: not found DMA6410開發版

Agnes avatar
By Agnes
at 2011-03-16T21:30
※ [本文轉錄自 LinuxDev 看板 #1DWBeOW3 ] 作者: saveload2001 (=口=and#34;被發現了!) 看板: LinuxDev 標題: [問題] mknod: not found DMA6410開發版 時間: Wed Mar 16 21:24:37 2011 各位 ...

docky compiz相衝?

Emma avatar
By Emma
at 2011-03-16T20:19
小弟最近換了 ubuntu 10.10 用compiz 玩了很多很炫的特效 然後用軟體中心的docky 偽裝apple 但最近發現了一個問題 docky 似乎會和compiz相衝到 整個螢幕下方會變成黑色 = =and#34; http://ppt.cc/GDA_ 請問有沒有解決方法呢? ...

簡單的環境變量設定問題。。

Cara avatar
By Cara
at 2011-03-16T19:35
想編一個程式,在它自代的make下出錯 TOOLCHAIN_LIB_DIR:=$(shell dirname `$(CC) -print-file-name=libgcc_s.so` 2andgt;/dev/null) 好想是 $(CC)沒設 我在 .profile ...

想問一個資料轉移權限的問題(SAMBA ACL)

Mason avatar
By Mason
at 2011-03-16T17:26
我在LINUX上面做SAMBA ACL 功能都正常,可以用WINDOWS去管理權限,非常方便 但是原在WINDOWS上面的資料要COPY到SAMBA開出來的SHARE 傳上去後的安全性都會被清掉,必須自己重新設定(有人有遇過嗎?) 想問有辦法在WINDOWS上的權限轉到LINUX上嗎? - ...