[PATCH 1/2] Add CHECK and CHECK_RET macro


Yan, Like
 

From: "Yan, Like" <like.yan@...>

- CHECK macro checks the given condition, if false, print a fatal
message, dump both host and guest context and return void;
- CHECK_RET macro checks the given condition, if false, print a fatal
message, dump both host and guest context and return a given value;
- dump_contexts function dump host and guest contexts

Change-Id: I482998c0ac7de32474097032abbf3835057c1f21
Signed-off-by: Yan, Like <like.yan@...>
---
debug/dump.c | 10 ++++++++++
include/debug/assert.h | 33 ++++++++++++++++++++++++++++++---
include/debug/dump.h | 5 +++++
3 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/debug/dump.c b/debug/dump.c
index 87fb334..9e02caf 100644
--- a/debug/dump.c
+++ b/debug/dump.c
@@ -293,6 +293,16 @@ void __assert(uint32_t line, const char *file, char *txt)
} while (1);
}

+void dump_contexts(void)
+{
+ uint32_t cpu_id = get_cpu_id();
+ uint64_t rsp = cpu_rsp_get();
+ uint64_t rbp = cpu_rbp_get();
+
+ show_host_call_trace(rsp, rbp, cpu_id);
+ dump_guest_context(cpu_id);
+}
+
void dump_exception(struct intr_ctx *ctx, uint32_t cpu_id)
{
const char *name = "Not defined";
diff --git a/include/debug/assert.h b/include/debug/assert.h
index 83b26c1..b777fdd 100644
--- a/include/debug/assert.h
+++ b/include/debug/assert.h
@@ -34,18 +34,45 @@
#ifdef HV_DEBUG
void __assert(uint32_t line, const char *file, char *txt);

-#define ASSERT(x, ...) \
- if (!(x)) {\
- pr_fatal(__VA_ARGS__);\
+#define ASSERT(x, ...) \
+ if (!(x)) { \
+ pr_fatal(__VA_ARGS__); \
__assert(__LINE__, __FILE__, "fatal error");\
}
+
+#define CHECK(x, ...) \
+ if (!(x)) { \
+ pr_fatal(__VA_ARGS__); \
+ dump_contexts(); \
+ return ; \
+ }
+
+#define CHECK_RET(x, r, ...) \
+ if (!(x)) { \
+ pr_fatal(__VA_ARGS__); \
+ dump_contexts(); \
+ return (r); \
+ }
#else
#define ASSERT(x, ...) \
if (!(x)) { \
+ pr_fatal(__VA_ARGS__); \
do { \
asm volatile ("pause" ::: "memory"); \
} while (1); \
}
+
+#define CHECK(x, ...) \
+ if (!(x)) { \
+ pr_fatal(__VA_ARGS__); \
+ return ; \
+ }
+
+#define CHECK_RET(x, r, ...) \
+ if (!(x)) { \
+ pr_fatal(__VA_ARGS__); \
+ return (r); \
+ }
#endif

/* Force a compilation error if condition is false */
diff --git a/include/debug/dump.h b/include/debug/dump.h
index 1d3ebf3..d38415a 100644
--- a/include/debug/dump.h
+++ b/include/debug/dump.h
@@ -37,11 +37,16 @@ struct intr_ctx;
#define CALL_TRACE_HIERARCHY_MAX 20
#define DUMP_STACK_SIZE 0x200

+void dump_contexts(void);
void dump_exception(struct intr_ctx *ctx, uint32_t cpu_id);
void dump_interrupt(struct intr_ctx *ctx);

#else

+static inline void dump_contexts(void)
+{
+}
+
static inline void dump_exception(__unused struct intr_ctx *ctx,
__unused uint32_t cpu_id)
{
--
2.7.4

Join {acrn-dev@lists.projectacrn.org to automatically receive all group messages.