]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DBEUG: add a new WARN_ON() macro
authorWilly Tarreau <w@1wt.eu>
Fri, 25 Feb 2022 07:52:39 +0000 (08:52 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 25 Feb 2022 10:55:47 +0000 (11:55 +0100)
This is the same as BUG_ON() except that it never crashes and only emits
a warning and a backtrace, inviting users to report the problem. This will
be usable for non-fatal issues that should not happen and need to be fixed.
This way the BUG_ON() when using DEBUG_STRICT_NOCRASH is effectively an
equivalent of WARN_ON().

include/haproxy/bug.h
src/debug.c

index 4cedbf11f7db6d5bdf89498019fe60e7fd4bf25c..cfd7c9f2ad47d97b344abcaf13b3e2e0fe5bec12 100644 (file)
  * crashes using ABORT_NOW() above.
  */
 #if defined(DEBUG_STRICT)
-#define BUG_ON(cond) _BUG_ON(cond, __FILE__, __LINE__, 1, "FATAL: bug ", "")
+#define BUG_ON(cond)  _BUG_ON(cond, __FILE__, __LINE__, 1, "FATAL: bug ", "")
+#define WARN_ON(cond) _BUG_ON(cond, __FILE__, __LINE__, 0, "WARNING: ",   " (please report to developers)")
 #elif defined(DEBUG_STRICT_NOCRASH)
-#define BUG_ON(cond) _BUG_ON(cond, __FILE__, __LINE__, 0, "FATAL: bug ", " (not crashing but process is untrusted now)")
+#define BUG_ON(cond)  _BUG_ON(cond, __FILE__, __LINE__, 0, "FATAL: bug ", " (not crashing but process is untrusted now)")
+#define WARN_ON(cond) _BUG_ON(cond, __FILE__, __LINE__, 0, "WARNING: ",   " (please report to developers)")
 #else
 #define BUG_ON(cond)
+#define WARN_ON(cond)
 #endif
 
 /* When not optimizing, clang won't remove that code, so only compile it in when optimizing */
index c0c6853dda3aabb893ab6c39fca08dd23bf3df3c..5d34389e5d89fc862d648772390fe7265c2f3dda 100644 (file)
@@ -363,6 +363,19 @@ int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void
        return 1;
 }
 
+/* parse a "debug dev warn" command. It always returns 1.
+ * Note: we make sure not to make the function static so that it appears in the trace.
+ */
+int debug_parse_cli_warn(char **args, char *payload, struct appctx *appctx, void *private)
+{
+       if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
+               return 1;
+
+       _HA_ATOMIC_INC(&debug_commands_issued);
+       WARN_ON(one > zero);
+       return 1;
+}
+
 /* parse a "debug dev close" command. It always returns 1. */
 static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
 {
@@ -1390,6 +1403,7 @@ static struct cli_kw_list cli_kws = {{ },{
        {{ "debug", "dev", "stream",NULL },    "debug dev stream [k=v]*                 : show/manipulate stream flags",            debug_parse_cli_stream,NULL, NULL, NULL, ACCESS_EXPERT },
        {{ "debug", "dev", "sym",   NULL },    "debug dev sym    <addr>                 : resolve symbol address",                  debug_parse_cli_sym,   NULL, NULL, NULL, ACCESS_EXPERT },
        {{ "debug", "dev", "tkill", NULL },    "debug dev tkill  [thr] [sig]            : send signal to thread",                   debug_parse_cli_tkill, NULL, NULL, NULL, ACCESS_EXPERT },
+       {{ "debug", "dev", "warn",  NULL },    "debug dev warn                          : call WARN_ON() and possibly crash",       debug_parse_cli_warn,  NULL, NULL, NULL, ACCESS_EXPERT },
        {{ "debug", "dev", "write", NULL },    "debug dev write  [size]                 : write that many bytes in return",         debug_parse_cli_write, NULL, NULL, NULL, ACCESS_EXPERT },
 #if defined(HA_HAVE_DUMP_LIBS)
        {{ "show", "libs", NULL, NULL },       "show libs                               : show loaded object files and libraries", debug_parse_cli_show_libs, NULL, NULL },