1 /*
2  * Copyright (c) Copyright (c) 2020 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 
10 static volatile int expected_reason = -1;
11 
12 void z_thread_essential_clear(void);
13 
k_sys_fatal_error_handler(unsigned int reason,const z_arch_esf_t * pEsf)14 void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
15 {
16 	printk("Caught system error -- reason %d\n", reason);
17 
18 	if (expected_reason == -1) {
19 		printk("Was not expecting a crash\n");
20 		printk("PROJECT EXECUTION FAILED\n");
21 		k_fatal_halt(reason);
22 	}
23 
24 	if (reason != expected_reason) {
25 		printk("Wrong crash type got %d expected %d\n", reason,
26 		       expected_reason);
27 		printk("PROJECT EXECUTION FAILED\n");
28 		k_fatal_halt(reason);
29 	}
30 
31 	printk("Fatal error expected as part of test case.\n");
32 
33 	expected_reason = -1;
34 }
35 
36 /**
37  * @brief This test case verifies when fatal error
38  *        log message can be captured.
39  * @details
40  * Test Objective:
41  * - When the fatal error is triggered, if the debugging message function
42  *   is turned on, the system can capture the log information.
43  *
44  * Prerequisite Conditions:
45  * - N/A
46  *
47  * Input Specifications:
48  * - N/A
49  *
50  * Test Procedure:
51  * -# Writing a function deliberately triggers a koops exception.
52  * -# When the log module is enabled, it will log some information
53  *    in the process of exception.
54  * -# The regex in testcase.yaml verify the kernel will dump thread id
55  *    information and error type when exception occurs.
56  *
57  * Expected Test Result:
58  * - The expected log message is caught.
59  *
60  * Pass/Fail Criteria:
61  * - Success if the log matching regex in step 3.
62  * - Failure if the log is not matching regex in step 3.
63  *
64  * Assumptions and Constraints:
65  * - N/A
66  * @ingroup kernel_fatal_tests
67  */
test_message_capture(void)68 void test_message_capture(void)
69 {
70 	unsigned int key;
71 
72 	expected_reason = K_ERR_KERNEL_OOPS;
73 
74 	key = irq_lock();
75 	k_oops();
76 	printk("SHOULD NEVER SEE THIS\n");
77 	irq_unlock(key);
78 }
79 
main(void)80 int main(void)
81 {
82 	/* main() is an essential thread, and we try to OOPS it.  When
83 	 * this test was written, that worked (even though it wasn't
84 	 * supposed to per docs).  Now we trap a different error (a
85 	 * panic and not an oops).  Set the thread non-essential as a
86 	 * workaround.
87 	 */
88 	z_thread_essential_clear();
89 
90 	test_message_capture();
91 	return 0;
92 }
93