使用debugfs

在進行Linux驅動開發時經常見到使用pr_debug和dev_dbg打印驅動的log,如果在內核配置時選擇了CONFIG_DYNAMIC_DEBUG宏,那麼就可以利用類似下面的命令打開對應文件的log:

echo -n “file xxx.c +p” > /sys/kernel/debug/dynamic_debug/control
但是有時候我們需要看到這個文件在內核啓動階段的log,那麼改怎麼辦呢?

這裏有兩種方法:

方法一 修改內核傳參

修改bootloader傳遞給kernel的bootargs,如果使用了設備樹的話,可以修改在chosen節點中bootargs屬性的值,具體方法內核文檔:Documentation/dynamic-debug-howto.txt

這種方案的優點是不需要修改驅動代碼。

比如我們需要開機內核啓動的時候就打開tfa98xx.c、wcd-mbhc-v2.c和q6asm.c的log,首先我們可以看一下這兩個文件對應的KBUILD_MODNAME,這裏有兩種方法可以查看這個值:

查看對應的Makefile
在驅動中查看上面的三個文件對應的Makefile,如下:

snd-soc-tfa98xx-objs := tfa98xx.o tfa_container.o tfa_dsp.o tfa9887B_init.o tfa9887_init.o tfa9888_init.o tfa9890_init.o tfa9891_init.o tfa9897_init.o
obj-$(CONFIG_SND_SOC_TFA98XX) += snd-soc-tfa98xx.o

snd-soc-wcd-mbhc-objs := wcd-mbhc-v2.o
obj-$(CONFIG_SND_SOC_WCD_MBHC) += snd-soc-wcd-mbhc.o

obj-y += audio_calibration.o audio_cal_utils.o q6adm.o q6afe.o q6asm.o
q6audio-v2.o q6voice.o q6core.o rtac.o q6lsm.o audio_slimslave.o
其中,tfa98xx.c對應的KBUILD_MODNAME就是snd-soc-tfa98xx,wcd-mbhc-v2.c對應的KBUILD_MODNAME就是snd-soc-wcd-mbhc,q6asm.c對應的KBUILD_MODNAME是q6asm

第二種方法如下,用如下命令打開這三個文件的log,那麼在這三個文件輸出log的同時也會將對應的KBUILD_MODNAME也輸出出來
adb shell ‘echo -n “file tfa98xx.c +pmflt” > /sys/kernel/debug/dynamic_debug/control’
adb shell ‘echo -n “file wcd-mbhc-v2.c +pmflt” > /sys/kernel/debug/dynamic_debug/control’
adb shell ‘echo -n “file q6asm.c +pmflt” > /sys/kernel/debug/dynamic_debug/control’
其中涉及到的符號的含義如下:

The flags specification comprises a change operation followed
by one or more flag characters. The change operation is one
of the characters:

- remove the given flags
+ add the given flags
= set the flags to the given flags

The flags are:

p enables the pr_debug() callsite.
f Include the function name in the printed message
l Include line number in the printed message
m Include module name in the printed message
t Include thread ID in messages not generated from interrupt context
_ No flags are set. (Or’d with others on input)
然後執行相關的操作,讓這幾個文件輸出log,在log中可以看到:

[ ] snd_soc_tfa98xx:tfa98xx_mute:: tfa98xx -: state:
[ : q6asm_callback: nowait_cmd_cnt

[ ] snd_soc_wcd_mbhc:wcd_correct_swch_plug:: wcd_correct_swch_plug: hs_comp_res:
從上面的log中可以看到,第一個冒號前面的字符串就是對應的KBUILD_MODNAME。

在獲得了KBUILD_MODNAME後,就可以修改設備樹文件了,下面是在原有bootargs後追加後的結果。

chosen {
    bootargs = "sched_enable_hmp=1 sched_enable_power_aware=1 snd_soc_wcd_mbhc.dyndbg=\"file wcd-mbhc-v2.c +p\" snd_soc_tfa98xx.dyndbg=\"file tfa98xx.c +p; file tfa_dsp.c +p\"";
};

以 snd_soc_tfa98xx.dyndbg=“file tfa98xx.c +p; file tfa_dsp.c +p” 爲例說明一下:

等號前面的命名規則是 “KBUILD_MODNAME.dyndbg”,等號後面的比較好理解,需要注意的是需要對雙引號進行轉義。

在調試這部分時可以打開kernel/params.c的log,方法是文件的開頭定義DEBUG宏,這樣就會將這個文件的pr_debug和dev_dbg打開。

這樣在kernel啓動的時候就可以看到對命令行的解析過程:

<>[ 0.015794] doing dyndbg params, parsing ARGS: ‘sched_enable_hmp=1 sched_enable_power_aware=1 snd_soc_wcd_mbhc.dyndbg=“file wcd-mbhc-v2.c +p” snd_soc_tfa98xx.dyndbg=“file tfa98xx.c +p; file tfa_dsp.c +p” console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 androidboot.hardware=qcom user_debug=31 msm_rtb.filter=0x237 ehci-hcd.park=3 lpm_levels.sleep_disabled=1 zswap.enabled=1 cma=32M@0-0xffffffff loglevel=0 androidboot.bootdevice=624000.ufshc androidboot.verifiedbootstate=orange androidboot.veritymode=logging androidboot.serialno=d94b873f androidboot.fingerprint.id=fpc androidboot.hardware.id=0x1c uart_enable=0 ro.housing.color=black pmode=0 androidboot.baseband=msm mdss_mdp.panel=1:dsi:0:qcom,mdss_dsi_nt35597_dsc_wqxga_cmd:config2:1:none:cfg:single_dsi fpsimd.fpsimd_settings=0’
<>[ ’
<>[ ’
<>[ 0.015824] doing dyndbg params: snd_soc_wcd_mbhc.dyndbg=‘file wcd-mbhc-v2.c +p’
<>[ 0.015901] doing dyndbg params: snd_soc_tfa98xx.dyndbg=‘file tfa98xx.c +p; file tfa_dsp.c +p’
<>[ 0.015975] doing dyndbg params: console=‘ttyHSL0,115200,n8’
<>[ 0.015980] doing dyndbg params: androidboot.console=‘ttyHSL0’
<>[ 0.015986] doing dyndbg params: androidboot.hardware=‘qcom’
<>[ ’
<>[ 0.015996] doing dyndbg params: msm_rtb.filter=‘0x237’
<>[ ’
<>[ ’
<>[ ’
<>[ 0.016016] doing dyndbg params: cma=‘32M@0-0xffffffff’
<>[ ’
<>[ 0.016026] doing dyndbg params: androidboot.bootdevice=‘624000.ufshc’
<>[ 0.016031] doing dyndbg params: androidboot.verifiedbootstate=‘orange’
<>[ 0.016036] doing dyndbg params: androidboot.veritymode=‘logging’
<>[ 0.016041] doing dyndbg params: androidboot.serialno=‘d94b873f’
<>[ 0.016046] doing dyndbg params: androidboot.fingerprint.id=‘fpc’
<>[ 0.016051] doing dyndbg params: androidboot.hardware.id=‘0x1c’
<>[ ’
<>[ 0.016061] doing dyndbg params: ro.housing.color=‘black’
<>[ ’
<>[ 0.016070] doing dyndbg params: androidboot.baseband=‘msm’
<>[ 0.016075] doing dyndbg params: mdss_mdp.panel=‘1:dsi:0:qcom,mdss_dsi_nt35597_dsc_wqxga_cmd:config2:1:none:cfg:single_dsi’
<>[ ’
方法二 在需要開啓log的驅動文件的開頭定義宏DEBUG

這樣該驅動文件中的pr_debug和dev_dbg就可以打開了。

比如驅動文件的名字是tfa98xx.c,那麼就在其第一個非註釋行添加DEBUG宏的定義:

/*

  • tfa98xx.c tfa98xx codec module
  • Copyright © 2015 NXP Semiconductors
  • Author: Sebastien Jan [email protected]
  • This program is free software; you can redistribute it and/or modify it
  • under the terms of the GNU General Public License as published by the
  • Free Software Foundation; either version 2 of the License, or (at your
  • option) any later version.
    */

#define DEBUG
#define pr_fmt(fmt) "%s(): " fmt, func

#include <linux/module.h>
#include <linux/i2c.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <linux/of_gpio.h>

爲什麼這樣做可以實現呢?下面我們以pr_debug爲例簡單分析。

在內核配置了CONFIG_DYNAMIC_DEBUG後,pr_debug的定義如下:

#define pr_debug(fmt, …)
dynamic_pr_debug(fmt, ##VA_ARGS)
dynamic_pr_debug的定義如下:

#define dynamic_pr_debug(fmt, …)
do {
DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);
if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT))
__dynamic_pr_debug(&descriptor, pr_fmt(fmt),
##VA_ARGS);
} )
這裏用到了宏DEFINE_DYNAMIC_DEBUG_METADATA,定義如下:

#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt)
)
attribute((section("__verbose"))) name = {
.modname = KBUILD_MODNAME,
.function = func,
.filename = FILE,
.format = (fmt),
.lineno = LINE,
.flags = _DPRINTK_FLAGS_DEFAULT,
}
__dynamic_pr_debug的定義如下:

void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, …)
{
va_list args;
struct va_format vaf;
char buf[PREFIX_SIZE];

BUG_ON(!descriptor);
BUG_ON(!fmt);

va_start(args, fmt);

vaf.fmt = fmt;
vaf.va = &args;

printk(KERN_DEBUG "%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);

va_end(args);

}
從上面的代碼可以看到,每一個pr_debug都對應一個名爲descriptor,類型爲struct _ddebug的變量,存放在kernel的__verbose段。決定這個pr_debug能否輸出log的條件就是descriptor.flags & _DPRINTK_FLAGS_PRINT爲true。

在定義descriptor時,將其flags成員賦值爲了_DPRINTK_FLAGS_DEFAULT,下面看一下這兩個宏的定義:

#if defined DEBUG
#define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
#else
#define _DPRINTK_FLAGS_DEFAULT 0
#endif
可以看到,如果定義了宏DEBUG,那麼_DPRINTK_FLAGS_DEFAULT其實就是_DPRINTK_FLAGS_PRINT,所以默認就是可以打印的。如果沒有定義,那麼_DPRINTK_FLAGS_DEFAULT就是0,上面的條件不會成立,也就打印不出來。

在dynamic debug初始化的時候會遍歷__verbose段,處理每一個struct _ddebug類型的變量,如果定義了DEBUG宏,在開機後,可以讀取control節點,會發現已經有"p"參數了。

root@colombo:/ # cat /d/dynamic_debug/control | grep tfa
sound/soc/codecs/tfa98xx.c: [snd_soc_tfa98xx]tfa98xx_mute =p “state: %d\012”
sound/soc/codecs/tfa98xx.c: [snd_soc_tfa98xx]tfa98xx_digital_mute =p “%s enter, mute: %d\012”
sound/soc/codecs/tfa98xx.c: [snd_soc_tfa98xx]tfa98xx_info_vstep =p “vsteps count: %d [prof=%d]\012”
… …
當然,也可以使用下面的命令關閉:

echo -n “file xxx.c -p” > /sys/kernel/debug/dynamic_debug/control
完。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章