[PATCH 1/2] lib: sbi: add some macros to detect BUG at runtime

Bin Meng bmeng.cn at gmail.com
Wed Sep 15 00:41:19 PDT 2021


On Wed, Sep 15, 2021 at 2:44 PM Xiang W <wxjstz at 126.com> wrote:
>
> Two macros are mainly added. One is called BUG(), which is used to put
> in unreachable branches. One named BUG_ON, used for assertion.
>
> Signed-off-by: Xiang W <wxjstz at 126.com>
> ---
>  include/sbi/sbi_console.h | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
>
> diff --git a/include/sbi/sbi_console.h b/include/sbi/sbi_console.h
> index e24ba5f..ce08541 100644
> --- a/include/sbi/sbi_console.h
> +++ b/include/sbi/sbi_console.h
> @@ -11,6 +11,7 @@
>  #define __SBI_CONSOLE_H__
>
>  #include <sbi/sbi_types.h>
> +#include <sbi/riscv_asm.h>
>
>  struct sbi_console_device {
>         /** Name of the console device */
> @@ -51,4 +52,17 @@ struct sbi_scratch;
>
>  int sbi_console_init(struct sbi_scratch *scratch);
>
> +#define BUG() do { \
> +       sbi_printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
> +       while (1)       \
> +               wfi();  \
> +       __builtin_unreachable(); \
> +} while (0)
> +
> +#define BUG_ON(cond) do { \
> +       if (cond)       \
> +               break;  \
> +       BUG(); \
> +} while (1)

Shouldn't this be:

if (cond)
    BUG()

?

Regards,
Bin



More information about the opensbi mailing list