162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci.. include:: ../disclaimer-zh_TW.rst
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci:Original: :doc:`../../../filesystems/debugfs`
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci=======
862306a36Sopenharmony_ciDebugfs
962306a36Sopenharmony_ci=======
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci譯者
1262306a36Sopenharmony_ci::
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci	中文版維護者:羅楚成 Chucheng Luo <luochucheng@vivo.com>
1562306a36Sopenharmony_ci	中文版翻譯者:羅楚成 Chucheng Luo <luochucheng@vivo.com>
1662306a36Sopenharmony_ci	中文版校譯者: 羅楚成 Chucheng Luo <luochucheng@vivo.com>
1762306a36Sopenharmony_ci	繁體中文版校譯者: 胡皓文 Hu Haowen <src.res.211@gmail.com>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci版權所有2020 羅楚成 <luochucheng@vivo.com>
2262306a36Sopenharmony_ci版權所有2021 胡皓文 Hu Haowen <src.res.211@gmail.com>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciDebugfs是內核開發人員在用戶空間獲取信息的簡單方法。與/proc不同,proc只提供進程
2662306a36Sopenharmony_ci信息。也不像sysfs,具有嚴格的「每個文件一個值「的規則。debugfs根本沒有規則,開發
2762306a36Sopenharmony_ci人員可以在這裡放置他們想要的任何信息。debugfs文件系統也不能用作穩定的ABI接口。
2862306a36Sopenharmony_ci從理論上講,debugfs導出文件的時候沒有任何約束。但是[1]實際情況並不總是那麼
2962306a36Sopenharmony_ci簡單。即使是debugfs接口,也最好根據需要進行設計,並儘量保持接口不變。
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciDebugfs通常使用以下命令安裝::
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci    mount -t debugfs none /sys/kernel/debug
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci(或等效的/etc/fstab行)。
3762306a36Sopenharmony_cidebugfs根目錄默認僅可由root用戶訪問。要更改對文件樹的訪問,請使用「 uid」,「 gid」
3862306a36Sopenharmony_ci和「 mode」掛載選項。請注意,debugfs API僅按照GPL協議導出到模塊。
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci使用debugfs的代碼應包含<linux/debugfs.h>。然後,首先是創建至少一個目錄來保存
4162306a36Sopenharmony_ci一組debugfs文件::
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci    struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci如果成功,此調用將在指定的父目錄下創建一個名爲name的目錄。如果parent參數爲空,
4662306a36Sopenharmony_ci則會在debugfs根目錄中創建。創建目錄成功時,返回值是一個指向dentry結構體的指針。
4762306a36Sopenharmony_ci該dentry結構體的指針可用於在目錄中創建文件(以及最後將其清理乾淨)。ERR_PTR
4862306a36Sopenharmony_ci(-ERROR)返回值表明出錯。如果返回ERR_PTR(-ENODEV),則表明內核是在沒有debugfs
4962306a36Sopenharmony_ci支持的情況下構建的,並且下述函數都不會起作用。
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci在debugfs目錄中創建文件的最通用方法是::
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci    struct dentry *debugfs_create_file(const char *name, umode_t mode,
5462306a36Sopenharmony_ci				       struct dentry *parent, void *data,
5562306a36Sopenharmony_ci				       const struct file_operations *fops);
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci在這裡,name是要創建的文件的名稱,mode描述了訪問文件應具有的權限,parent指向
5862306a36Sopenharmony_ci應該保存文件的目錄,data將存儲在產生的inode結構體的i_private欄位中,而fops是
5962306a36Sopenharmony_ci一組文件操作函數,這些函數中實現文件操作的具體行爲。至少,read()和/或
6062306a36Sopenharmony_ciwrite()操作應提供;其他可以根據需要包括在內。同樣的,返回值將是指向創建文件
6162306a36Sopenharmony_ci的dentry指針,錯誤時返回ERR_PTR(-ERROR),系統不支持debugfs時返回值爲ERR_PTR
6262306a36Sopenharmony_ci(-ENODEV)。創建一個初始大小的文件,可以使用以下函數代替::
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci    struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
6562306a36Sopenharmony_ci				struct dentry *parent, void *data,
6662306a36Sopenharmony_ci				const struct file_operations *fops,
6762306a36Sopenharmony_ci				loff_t file_size);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cifile_size是初始文件大小。其他參數跟函數debugfs_create_file的相同。
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci在許多情況下,沒必要自己去創建一組文件操作;對於一些簡單的情況,debugfs代碼提供
7262306a36Sopenharmony_ci了許多幫助函數。包含單個整數值的文件可以使用以下任何一項創建::
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci    void debugfs_create_u8(const char *name, umode_t mode,
7562306a36Sopenharmony_ci			   struct dentry *parent, u8 *value);
7662306a36Sopenharmony_ci    void debugfs_create_u16(const char *name, umode_t mode,
7762306a36Sopenharmony_ci			    struct dentry *parent, u16 *value);
7862306a36Sopenharmony_ci    struct dentry *debugfs_create_u32(const char *name, umode_t mode,
7962306a36Sopenharmony_ci				      struct dentry *parent, u32 *value);
8062306a36Sopenharmony_ci    void debugfs_create_u64(const char *name, umode_t mode,
8162306a36Sopenharmony_ci			    struct dentry *parent, u64 *value);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci這些文件支持讀取和寫入給定值。如果某個文件不支持寫入,只需根據需要設置mode
8462306a36Sopenharmony_ci參數位。這些文件中的值以十進位表示;如果需要使用十六進位,可以使用以下函數
8562306a36Sopenharmony_ci替代::
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci    void debugfs_create_x8(const char *name, umode_t mode,
8862306a36Sopenharmony_ci			   struct dentry *parent, u8 *value);
8962306a36Sopenharmony_ci    void debugfs_create_x16(const char *name, umode_t mode,
9062306a36Sopenharmony_ci			    struct dentry *parent, u16 *value);
9162306a36Sopenharmony_ci    void debugfs_create_x32(const char *name, umode_t mode,
9262306a36Sopenharmony_ci			    struct dentry *parent, u32 *value);
9362306a36Sopenharmony_ci    void debugfs_create_x64(const char *name, umode_t mode,
9462306a36Sopenharmony_ci			    struct dentry *parent, u64 *value);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci這些功能只有在開發人員知道導出值的大小的時候才有用。某些數據類型在不同的架構上
9762306a36Sopenharmony_ci有不同的寬度,這樣會使情況變得有些複雜。在這種特殊情況下可以使用以下函數::
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci    void debugfs_create_size_t(const char *name, umode_t mode,
10062306a36Sopenharmony_ci			       struct dentry *parent, size_t *value);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci不出所料,此函數將創建一個debugfs文件來表示類型爲size_t的變量。
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci同樣地,也有導出無符號長整型變量的函數,分別以十進位和十六進位表示如下::
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci    struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
10762306a36Sopenharmony_ci					struct dentry *parent,
10862306a36Sopenharmony_ci					unsigned long *value);
10962306a36Sopenharmony_ci    void debugfs_create_xul(const char *name, umode_t mode,
11062306a36Sopenharmony_ci			    struct dentry *parent, unsigned long *value);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci布爾值可以通過以下方式放置在debugfs中::
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci    struct dentry *debugfs_create_bool(const char *name, umode_t mode,
11562306a36Sopenharmony_ci				       struct dentry *parent, bool *value);
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci讀取結果文件將產生Y(對於非零值)或N,後跟換行符寫入的時候,它只接受大寫或小寫
11962306a36Sopenharmony_ci值或1或0。任何其他輸入將被忽略。
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci同樣,atomic_t類型的值也可以放置在debugfs中::
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci    void debugfs_create_atomic_t(const char *name, umode_t mode,
12462306a36Sopenharmony_ci				 struct dentry *parent, atomic_t *value)
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci讀取此文件將獲得atomic_t值,寫入此文件將設置atomic_t值。
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci另一個選擇是通過以下結構體和函數導出一個任意二進位數據塊::
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci    struct debugfs_blob_wrapper {
13162306a36Sopenharmony_ci	void *data;
13262306a36Sopenharmony_ci	unsigned long size;
13362306a36Sopenharmony_ci    };
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci    struct dentry *debugfs_create_blob(const char *name, umode_t mode,
13662306a36Sopenharmony_ci				       struct dentry *parent,
13762306a36Sopenharmony_ci				       struct debugfs_blob_wrapper *blob);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci讀取此文件將返回由指針指向debugfs_blob_wrapper結構體的數據。一些驅動使用「blobs」
14062306a36Sopenharmony_ci作爲一種返回幾行(靜態)格式化文本的簡單方法。這個函數可用於導出二進位信息,但
14162306a36Sopenharmony_ci似乎在主線中沒有任何代碼這樣做。請注意,使用debugfs_create_blob()命令創建的
14262306a36Sopenharmony_ci所有文件是只讀的。
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci如果您要轉儲一個寄存器塊(在開發過程中經常會這麼做,但是這樣的調試代碼很少上傳
14562306a36Sopenharmony_ci到主線中。Debugfs提供兩個函數:一個用於創建僅寄存器文件,另一個把一個寄存器塊
14662306a36Sopenharmony_ci插入一個順序文件中::
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci    struct debugfs_reg32 {
14962306a36Sopenharmony_ci	char *name;
15062306a36Sopenharmony_ci	unsigned long offset;
15162306a36Sopenharmony_ci    };
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci    struct debugfs_regset32 {
15462306a36Sopenharmony_ci	struct debugfs_reg32 *regs;
15562306a36Sopenharmony_ci	int nregs;
15662306a36Sopenharmony_ci	void __iomem *base;
15762306a36Sopenharmony_ci    };
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci    struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
16062306a36Sopenharmony_ci				     struct dentry *parent,
16162306a36Sopenharmony_ci				     struct debugfs_regset32 *regset);
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci    void debugfs_print_regs32(struct seq_file *s, struct debugfs_reg32 *regs,
16462306a36Sopenharmony_ci			 int nregs, void __iomem *base, char *prefix);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci「base」參數可能爲0,但您可能需要使用__stringify構建reg32數組,實際上有許多寄存器
16762306a36Sopenharmony_ci名稱(宏)是寄存器塊在基址上的字節偏移量。
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci如果要在debugfs中轉儲u32數組,可以使用以下函數創建文件::
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci     void debugfs_create_u32_array(const char *name, umode_t mode,
17262306a36Sopenharmony_ci			struct dentry *parent,
17362306a36Sopenharmony_ci			u32 *array, u32 elements);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci「array」參數提供數據,而「elements」參數爲數組中元素的數量。注意:數組創建後,數組
17662306a36Sopenharmony_ci大小無法更改。
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci有一個函數來創建與設備相關的seq_file::
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci   struct dentry *debugfs_create_devm_seqfile(struct device *dev,
18162306a36Sopenharmony_ci				const char *name,
18262306a36Sopenharmony_ci				struct dentry *parent,
18362306a36Sopenharmony_ci				int (*read_fn)(struct seq_file *s,
18462306a36Sopenharmony_ci					void *data));
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci「dev」參數是與此debugfs文件相關的設備,並且「read_fn」是一個函數指針,這個函數在
18762306a36Sopenharmony_ci列印seq_file內容的時候被回調。
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci還有一些其他的面向目錄的函數::
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci    struct dentry *debugfs_rename(struct dentry *old_dir,
19262306a36Sopenharmony_ci		                  struct dentry *old_dentry,
19362306a36Sopenharmony_ci		                  struct dentry *new_dir,
19462306a36Sopenharmony_ci				  const char *new_name);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci    struct dentry *debugfs_create_symlink(const char *name,
19762306a36Sopenharmony_ci                                          struct dentry *parent,
19862306a36Sopenharmony_ci                                          const char *target);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci調用debugfs_rename()將爲現有的debugfs文件重命名,可能同時切換目錄。 new_name
20162306a36Sopenharmony_ci函數調用之前不能存在;返回值爲old_dentry,其中包含更新的信息。可以使用
20262306a36Sopenharmony_cidebugfs_create_symlink()創建符號連結。
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci所有debugfs用戶必須考慮的一件事是:
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cidebugfs不會自動清除在其中創建的任何目錄。如果一個模塊在不顯式刪除debugfs目錄的
20762306a36Sopenharmony_ci情況下卸載模塊,結果將會遺留很多野指針,從而導致系統不穩定。因此,所有debugfs
20862306a36Sopenharmony_ci用戶-至少是那些可以作爲模塊構建的用戶-必須做模塊卸載的時候準備刪除在此創建的
20962306a36Sopenharmony_ci所有文件和目錄。一份文件可以通過以下方式刪除::
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci    void debugfs_remove(struct dentry *dentry);
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cidentry值可以爲NULL或錯誤值,在這種情況下,不會有任何文件被刪除。
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci很久以前,內核開發者使用debugfs時需要記錄他們創建的每個dentry指針,以便最後所有
21662306a36Sopenharmony_ci文件都可以被清理掉。但是,現在debugfs用戶能調用以下函數遞歸清除之前創建的文件::
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci    void debugfs_remove_recursive(struct dentry *dentry);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci如果將對應頂層目錄的dentry傳遞給以上函數,則該目錄下的整個層次結構將會被刪除。
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci注釋:
22362306a36Sopenharmony_ci[1] http://lwn.net/Articles/309298/
22462306a36Sopenharmony_ci
225