kernel中用%pR来打印resource结构体

kernel中用struct resource *res;表示资源
struct resource {
    resource_size_t start;
    resource_size_t end;
    const char *name;
    unsigned long flags;
    unsigned long desc;
    struct resource *parent, *sibling, *child;
};
kernel中用dev_info(&dev->dev, "legacy IDE quirk: reg 0x18: %pR\n", res);来打印res结构图,注意这里是用%pR来打印的。但这是怎么实现的呢?其实对%pR最终解析是在vsnprintf
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
    unsigned long long num;
    char *str, *end;
    struct printf_spec spec = {0};

    /* Reject out-of-range values early.  Large positive sizes are
       used for unknown buffer sizes. */
    if (WARN_ON_ONCE(size > INT_MAX))
        return 0;

    str = buf;
    end = buf + size;

    /* Make sure end is always >= buf */
    if (end < buf) {
        end = ((void *)-1);
        size = end - buf;
    }

    while (*fmt) {
        const char *old_fmt = fmt;
        int read = format_decode(fmt, &spec);

        fmt += read;

        switch (spec.type) {
        case FORMAT_TYPE_PTR:
            str = pointer(fmt, str, end, va_arg(args, void *),
                      spec);
            while (isalnum(*fmt))
                fmt++;
            break;
}
%p是在format_decode 中解析返回FORMAT_TYPE_PTR

char *pointer(const char *fmt, char *buf, char *end, void *ptr,
          struct printf_spec spec)
{
    const int default_width = 2 * sizeof(void *);

    if (!ptr && *fmt != 'K') {
        /*
         * Print (null) with the same width as a pointer so it makes
         * tabular output look nice.
         */
        if (spec.field_width == -1)
            spec.field_width = default_width;
        return string(buf, end, "(null)", spec);
    }

    switch (*fmt) {
    case 'F':
    case 'f':
        ptr = dereference_function_descriptor(ptr);
        /* Fallthrough */
    case 'S':
    case 's':
    case 'B':
        return symbol_string(buf, end, ptr, spec, fmt);
    case 'R':
    case 'r':
        return resource_string(buf, end, ptr, spec, fmt);
}
中可见%R 调用的是resource_string
char *resource_string(char *buf, char *end, struct resource *res,
              struct printf_spec spec, const char *fmt)
{
#ifndef IO_RSRC_PRINTK_SIZE
#define IO_RSRC_PRINTK_SIZE    6
#endif

#ifndef MEM_RSRC_PRINTK_SIZE
#define MEM_RSRC_PRINTK_SIZE    10
#endif
    static const struct printf_spec io_spec = {
        .base = 16,
        .field_width = IO_RSRC_PRINTK_SIZE,
        .precision = -1,
        .flags = SPECIAL | SMALL | ZEROPAD,
    };
    static const struct printf_spec mem_spec = {
        .base = 16,
        .field_width = MEM_RSRC_PRINTK_SIZE,
        .precision = -1,
        .flags = SPECIAL | SMALL | ZEROPAD,
    };

    char *p = sym, *pend = sym + sizeof(sym);
    int decode = (fmt[0] == 'R') ? 1 : 0;
    const struct printf_spec *specp;

    *p++ = '[';
    if (res->flags & IORESOURCE_IO) {
        p = string(p, pend, "io  ", str_spec);
        specp = &io_spec;
    } else if (res->flags & IORESOURCE_MEM) {
        p = string(p, pend, "mem ", str_spec);
        specp = &mem_spec;
    } else if (res->flags & IORESOURCE_IRQ) {
        p = string(p, pend, "irq ", str_spec);
        specp = &dec_spec;
    } else if (res->flags & IORESOURCE_DMA) {
        p = string(p, pend, "dma ", str_spec);
        specp = &dec_spec;
    } else if (res->flags & IORESOURCE_BUS) {
        p = string(p, pend, "bus ", str_spec);
        specp = &bus_spec;
    } else {
        p = string(p, pend, "??? ", str_spec);
        specp = &mem_spec;
        decode = 0;
    }
    if (decode && res->flags & IORESOURCE_UNSET) {
        p = string(p, pend, "size ", str_spec);
        p = number(p, pend, resource_size(res), *specp);
    } else {
        p = number(p, pend, res->start, *specp);
        if (res->start != res->end) {
            *p++ = '-';
            p = number(p, pend, res->end, *specp);
        }
    }
    if (decode) {
        if (res->flags & IORESOURCE_MEM_64)
            p = string(p, pend, " 64bit", str_spec);
        if (res->flags & IORESOURCE_PREFETCH)
            p = string(p, pend, " pref", str_spec);
        if (res->flags & IORESOURCE_WINDOW)
            p = string(p, pend, " window", str_spec);
        if (res->flags & IORESOURCE_DISABLED)
            p = string(p, pend, " disabled", str_spec);
    } else {
        p = string(p, pend, " flags ", str_spec);
        p = number(p, pend, res->flags, flag_spec);
    }
    *p++ = ']';
    *p = '\0';

    return string(buf, end, sym, spec);
}

这里面会根据resource的flag来决定是io/mem,并根据flag来决定是64bit/pref等。最终会在开机log中看到如下格式的log
pci_bus 0004:88: root bus resource [mem 0x8a9000000-0x8abfeffff window] (bus address [0xa9000000-0xabfeffff])


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值