通过WARN(1,“xxx“) 来确定code的flow和打印callstack

如果在code中想要确定code 走的flow,可以通过WARN_ONCE(1, "123446");来进行,并且会打印出callstack。这里的1的话,表示条件成立,"123446" 表示一个标识符,这样在log中直接搜素"123446"就可以找到我们的log
static inline int regmap_write(struct regmap *map, unsigned int reg,
                   unsigned int val)
{
    WARN_ONCE(1, "regmap API is disabled");
    return -EINVAL;
}

WARN_ONCE 定义如下:可见最后会调用dump_stack 来打印stack

#define WARN_ONCE(condition, format...)    ({            \
    static bool __section(.data.unlikely) __warned;        \
    int __ret_warn_once = !!(condition);            \
                                \
    if (unlikely(__ret_warn_once && !__warned)) {        \
        __warned = true;                \
        WARN(1, format);                \
    }                            \
    unlikely(__ret_warn_once);                \
})

#ifndef WARN
#define WARN(condition, format...) ({                        \
    int __ret_warn_on = !!(condition);                \
    if (unlikely(__ret_warn_on))                    \
        __WARN_printf(format);                    \
    unlikely(__ret_warn_on);                    \
})
#endif


#define __WARN_printf(arg...)    warn_slowpath_fmt(__FILE__, __LINE__, arg)


#ifdef WANT_WARN_ON_SLOWPATH
void warn_slowpath_fmt(const char *file, int line, const char *fmt, ...)
{
    struct warn_args args;

    args.fmt = fmt;
    va_start(args.args, fmt);
    __warn(file, line, __builtin_return_address(0), TAINT_WARN, NULL,
           &args);
    va_end(args.args);
}


void __warn(const char *file, int line, void *caller, unsigned taint,
        struct pt_regs *regs, struct warn_args *args)
{
    disable_trace_on_warning();

    pr_warn("------------[ cut here ]------------\n");

    if (file)
        pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
            raw_smp_processor_id(), current->pid, file, line,
            caller);
    else
        pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
            raw_smp_processor_id(), current->pid, caller);

    if (args)
        vprintk(args->fmt, args->args);

    if (panic_on_warn) {
        /*
         * This thread may hit another WARN() in the panic path.
         * Resetting this prevents additional WARN() from panicking the
         * system on this thread.  Other threads are blocked by the
         * panic_mutex in panic().
         */
        panic_on_warn = 0;
        panic("panic_on_warn set ...\n");
    }

    print_modules();

    if (regs)
        show_regs(regs);
    else
        dump_stack();

    print_oops_end_marker();

    /* Just a warning, don't kill lockdep. */
    add_taint(taint, LOCKDEP_STILL_OK);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃鸳鸯锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值