debug: add helper functions for cap type checking

This reduces the usage of magic values to one file only.

Signed-off-by: Axel Heider <axelheider@gmx.de>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index cd6f8b5..9a27dac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,6 +12,7 @@
 
 cmake_minimum_required(VERSION 3.7.2)
 
+add_subdirectory(libsel4debug)
 add_subdirectory(libsel4allocman)
 add_subdirectory(libsel4vka)
 add_subdirectory(libsel4utils)
@@ -20,7 +21,6 @@
 add_subdirectory(libsel4serialserver)
 add_subdirectory(libsel4simple)
 add_subdirectory(libsel4simple-default)
-add_subdirectory(libsel4debug)
 add_subdirectory(libsel4test)
 add_subdirectory(libsel4sync)
 add_subdirectory(libsel4muslcsys)
diff --git a/libsel4debug/include/sel4debug/debug.h b/libsel4debug/include/sel4debug/debug.h
index a128ae5..7b2b04d 100644
--- a/libsel4debug/include/sel4debug/debug.h
+++ b/libsel4debug/include/sel4debug/debug.h
@@ -27,5 +27,25 @@
 
 void debug_cap_identify(seL4_CPtr cap);
 
+static inline int debug_cap_is_valid(seL4_CPtr cap)
+{
+    return (0 != seL4_DebugCapIdentify(cap));
+}
+
+static inline int debug_cap_is_endpoint(seL4_CPtr cap)
+{
+    // there is kernel/generated/arch/object/structures_gen.h that defines
+    // cap_endpoint_cap = 4, but we can't include it here
+    return (4 == seL4_DebugCapIdentify(cap));
+}
+
+static inline int debug_cap_is_notification(seL4_CPtr cap)
+{
+    // there is kernel/generated/arch/object/structures_gen.h that defines
+    // cap_notification_cap = 6, but we can't include it here
+    return (6 == seL4_DebugCapIdentify(cap));
+}
+
+
 void debug_print_bootinfo(seL4_BootInfo *info);
 
diff --git a/libsel4sync/CMakeLists.txt b/libsel4sync/CMakeLists.txt
index 611e0f4..44d3e77 100644
--- a/libsel4sync/CMakeLists.txt
+++ b/libsel4sync/CMakeLists.txt
@@ -20,11 +20,7 @@
 
 add_library(sel4sync STATIC EXCLUDE_FROM_ALL ${deps})
 
-target_include_directories(
-    sel4sync
-    PUBLIC
-        include
-)
+target_include_directories(sel4sync PUBLIC include)
 
 target_link_libraries(
     sel4sync
@@ -37,3 +33,7 @@
         sel4_
         autoconf
 )
+
+if(KernelDebugBuild)
+    target_link_libraries(sel4sync PUBLIC sel4debug)
+endif()
diff --git a/libsel4sync/include/sync/bin_sem.h b/libsel4sync/include/sync/bin_sem.h
index e24ff06..caaaa2f 100644
--- a/libsel4sync/include/sync/bin_sem.h
+++ b/libsel4sync/include/sync/bin_sem.h
@@ -15,6 +15,9 @@
 #include <autoconf.h>
 #include <assert.h>
 #include <sel4/sel4.h>
+#ifdef CONFIG_DEBUG_BUILD
+#include <sel4debug/debug.h>
+#endif
 #include <vka/vka.h>
 #include <vka/object.h>
 #include <stddef.h>
@@ -44,7 +47,7 @@
 
 #ifdef CONFIG_DEBUG_BUILD
     /* Check the cap actually is a notification. */
-    assert(seL4_DebugCapIdentify(notification) == 6);
+    assert(debug_cap_is_notification(notification));
 #endif
 
     sem->notification.cptr = notification;
diff --git a/libsel4sync/include/sync/condition_var.h b/libsel4sync/include/sync/condition_var.h
index 1af9de7..160fff2 100644
--- a/libsel4sync/include/sync/condition_var.h
+++ b/libsel4sync/include/sync/condition_var.h
@@ -14,6 +14,9 @@
 
 #include <autoconf.h>
 #include <sel4/sel4.h>
+#ifdef CONFIG_DEBUG_BUILD
+#include <sel4debug/debug.h>
+#endif
 #include <vka/vka.h>
 #include <vka/object.h>
 #include <platsupport/sync/atomic.h>
@@ -38,7 +41,7 @@
 
 #ifdef CONFIG_DEBUG_BUILD
     /* Check the cap actually is a notification. */
-    assert(seL4_DebugCapIdentify(notification) == 6);
+    assert(debug_cap_is_notification(notification));
 #endif
 
     cv->notification.cptr = notification;
diff --git a/libsel4sync/include/sync/sem-bare.h b/libsel4sync/include/sync/sem-bare.h
index 3218e42..203a8a3 100644
--- a/libsel4sync/include/sync/sem-bare.h
+++ b/libsel4sync/include/sync/sem-bare.h
@@ -22,6 +22,9 @@
 #include <assert.h>
 #include <limits.h>
 #include <sel4/sel4.h>
+#ifdef CONFIG_DEBUG_BUILD
+#include <sel4debug/debug.h>
+#endif
 #include <stddef.h>
 #include <platsupport/sync/atomic.h>
 
@@ -29,7 +32,7 @@
 {
 #ifdef CONFIG_DEBUG_BUILD
     /* Check the cap actually is an EP. */
-    assert(seL4_DebugCapIdentify(ep) == 4);
+    assert(debug_cap_is_endpoint(ep));
 #endif
     assert(value != NULL);
     int oldval;
@@ -80,7 +83,7 @@
 {
 #ifdef CONFIG_DEBUG_BUILD
     /* Check the cap actually is an EP. */
-    assert(seL4_DebugCapIdentify(ep) == 4);
+    assert(debug_cap_is_endpoint(ep));
 #endif
     assert(value != NULL);
     /* We can do an "unsafe" increment here because we know the lock cannot be
diff --git a/libsel4sync/include/sync/sem.h b/libsel4sync/include/sync/sem.h
index f96c3b2..1b98435 100644
--- a/libsel4sync/include/sync/sem.h
+++ b/libsel4sync/include/sync/sem.h
@@ -15,6 +15,9 @@
 #include <autoconf.h>
 #include <assert.h>
 #include <sel4/sel4.h>
+#ifdef CONFIG_DEBUG_BUILD
+#include <sel4debug/debug.h>
+#endif
 #include <vka/vka.h>
 #include <vka/object.h>
 #include <stddef.h>
@@ -38,7 +41,7 @@
     }
 #ifdef CONFIG_DEBUG_BUILD
     /* Check the cap actually is an EP. */
-    assert(seL4_DebugCapIdentify(ep) == 4);
+    assert(debug_cap_is_endpoint(ep));
 #endif
 
     sem->ep.cptr = ep;
diff --git a/libsel4sync/src/recursive_mutex.c b/libsel4sync/src/recursive_mutex.c
index f82838f..9862354 100644
--- a/libsel4sync/src/recursive_mutex.c
+++ b/libsel4sync/src/recursive_mutex.c
@@ -17,6 +17,9 @@
 #include <limits.h>
 
 #include <sel4/sel4.h>
+#ifdef CONFIG_DEBUG_BUILD
+#include <sel4debug/debug.h>
+#endif
 
 static void *thread_id(void)
 {
@@ -31,7 +34,7 @@
     }
 #ifdef CONFIG_DEBUG_BUILD
     /* Check the cap actually is a notification. */
-    assert(seL4_DebugCapIdentify(notification) == 6);
+    assert(debug_cap_is_notification(notification));
 #endif
 
     mutex->notification.cptr = notification;
diff --git a/libsel4vka/CMakeLists.txt b/libsel4vka/CMakeLists.txt
index b1e5fe8..9f472a4 100644
--- a/libsel4vka/CMakeLists.txt
+++ b/libsel4vka/CMakeLists.txt
@@ -69,3 +69,6 @@
         sel4vka_Config
         sel4_autoconf
 )
+if(KernelDebugBuild)
+    target_link_libraries(sel4vka PUBLIC sel4debug)
+endif()
diff --git a/libsel4vka/include/vka/vka.h b/libsel4vka/include/vka/vka.h
index 542c7cd..7542489 100644
--- a/libsel4vka/include/vka/vka.h
+++ b/libsel4vka/include/vka/vka.h
@@ -17,6 +17,9 @@
 
 #include <sel4/sel4.h>
 #include <sel4/types.h>
+#ifdef CONFIG_DEBUG_BUILD
+#include <sel4debug/debug.h>
+#endif
 #include <assert.h>
 #include <stdint.h>
 #include <utils/util.h>
@@ -192,7 +195,7 @@
 static inline void vka_cspace_free(vka_t *vka, seL4_CPtr slot)
 {
 #ifdef CONFIG_DEBUG_BUILD
-    if (seL4_DebugCapIdentify(slot) != 0) {
+    if (debug_cap_is_valid(slot)) {
         ZF_LOGF("slot is not free: call vka_cnode_delete first");
         /* this terminates the system */
     }