Mesa (master): intel/tools: refactor logging to be easier to follow by static analyzers

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Nov 5 12:14:33 UTC 2020


Module: Mesa
Branch: master
Commit: c323d7c2a7f65fa2d9504e35b4e3be16d9e06a61
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=c323d7c2a7f65fa2d9504e35b4e3be16d9e06a61

Author: Marcin Ślusarz <marcin.slusarz at intel.com>
Date:   Wed Nov  4 19:05:19 2020 +0100

intel/tools: refactor logging to be easier to follow by static analyzers

Refactor out the part of fail_if function that never returns into
NORETURN function and put the condition check outside.

Addresses many false positive warnings by Coverity.

Signed-off-by: Marcin Ślusarz <marcin.slusarz at intel.com>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7449>

---

 src/intel/tools/aub_write.c      | 15 +--------------
 src/intel/tools/aub_write.h      | 21 +++++++++++++++++++++
 src/intel/tools/error2aub.c      | 15 +--------------
 src/intel/tools/intel_dump_gpu.c | 16 +---------------
 4 files changed, 24 insertions(+), 43 deletions(-)

diff --git a/src/intel/tools/aub_write.c b/src/intel/tools/aub_write.c
index 70ec195a7cb..2995bdd931d 100644
--- a/src/intel/tools/aub_write.c
+++ b/src/intel/tools/aub_write.c
@@ -56,20 +56,7 @@ static void mem_trace_memory_write_header_out(struct aub_file *aub, uint64_t add
                                               uint32_t len, uint32_t addr_space,
                                               const char *desc);
 
-static void __attribute__ ((format(__printf__, 2, 3)))
-fail_if(int cond, const char *format, ...)
-{
-   va_list args;
-
-   if (!cond)
-      return;
-
-   va_start(args, format);
-   vfprintf(stderr, format, args);
-   va_end(args);
-
-   raise(SIGTRAP);
-}
+#define fail_if(cond, ...) _fail_if(cond, NULL, __VA_ARGS__)
 
 static inline uint32_t
 align_u32(uint32_t v, uint32_t a)
diff --git a/src/intel/tools/aub_write.h b/src/intel/tools/aub_write.h
index b4f8649b384..24eb69f6375 100644
--- a/src/intel/tools/aub_write.h
+++ b/src/intel/tools/aub_write.h
@@ -25,8 +25,10 @@
 #ifndef INTEL_AUB_WRITE
 #define INTEL_AUB_WRITE
 
+#include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #include "drm-uapi/i915_drm.h"
 
@@ -37,6 +39,25 @@
 extern "C" {
 #endif
 
+static inline void PRINTFLIKE(2, 3) NORETURN
+_fail(const char *prefix, const char *format, ...)
+{
+   va_list args;
+
+   va_start(args, format);
+   if (prefix)
+      fprintf(stderr, "%s: ", prefix);
+   vfprintf(stderr, format, args);
+   va_end(args);
+
+   abort();
+}
+
+#define _fail_if(cond, prefix, ...) do { \
+   if (cond) \
+      _fail(prefix, __VA_ARGS__); \
+} while (0)
+
 #define MAX_CONTEXT_COUNT 64
 
 struct aub_ppgtt_table {
diff --git a/src/intel/tools/error2aub.c b/src/intel/tools/error2aub.c
index dda8e20191b..1877a3db900 100644
--- a/src/intel/tools/error2aub.c
+++ b/src/intel/tools/error2aub.c
@@ -38,20 +38,7 @@
 #include "drm-uapi/i915_drm.h"
 #include "intel_aub.h"
 
-static void __attribute__ ((format(__printf__, 2, 3)))
-fail_if(int cond, const char *format, ...)
-{
-   va_list args;
-
-   if (!cond)
-      return;
-
-   va_start(args, format);
-   vfprintf(stderr, format, args);
-   va_end(args);
-
-   raise(SIGTRAP);
-}
+#define fail_if(cond, ...) _fail_if(cond, NULL, __VA_ARGS__)
 
 #define fail(...) fail_if(true, __VA_ARGS__)
 
diff --git a/src/intel/tools/intel_dump_gpu.c b/src/intel/tools/intel_dump_gpu.c
index 812d4c481c7..a4b7d41b66f 100644
--- a/src/intel/tools/intel_dump_gpu.c
+++ b/src/intel/tools/intel_dump_gpu.c
@@ -92,21 +92,7 @@ static struct bo *bos;
 #define IS_USERPTR(p) ((uintptr_t) (p) & USERPTR_FLAG)
 #define GET_PTR(p) ( (void *) ((uintptr_t) p & ~(uintptr_t) 1) )
 
-static void __attribute__ ((format(__printf__, 2, 3)))
-fail_if(int cond, const char *format, ...)
-{
-   va_list args;
-
-   if (!cond)
-      return;
-
-   va_start(args, format);
-   fprintf(stderr, "intel_dump_gpu: ");
-   vfprintf(stderr, format, args);
-   va_end(args);
-
-   raise(SIGTRAP);
-}
+#define fail_if(cond, ...) _fail_if(cond, "intel_dump_gpu", __VA_ARGS__)
 
 static struct bo *
 get_bo(unsigned fd, uint32_t handle)



More information about the mesa-commit mailing list