test: use SPDX tag for Intel copyright files
[dpdk.git] / test / test / test_debug.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
9
10 #include <rte_debug.h>
11 #include <rte_common.h>
12 #include <rte_eal.h>
13
14 #include "test.h"
15
16 /*
17  * Debug test
18  * ==========
19  */
20
21 /* use fork() to test rte_panic() */
22 static int
23 test_panic(void)
24 {
25         int pid;
26         int status;
27
28         pid = fork();
29
30         if (pid == 0)
31                 rte_panic("Test Debug\n");
32         else if (pid < 0){
33                 printf("Fork Failed\n");
34                 return -1;
35         }
36         wait(&status);
37         if(status == 0){
38                 printf("Child process terminated normally!\n");
39                 return -1;
40         } else
41                 printf("Child process terminated as expected - Test passed!\n");
42
43         return 0;
44 }
45
46 /* use fork() to test rte_exit() */
47 static int
48 test_exit_val(int exit_val)
49 {
50         int pid;
51         int status;
52
53         pid = fork();
54
55         if (pid == 0)
56                 rte_exit(exit_val, __func__);
57         else if (pid < 0){
58                 printf("Fork Failed\n");
59                 return -1;
60         }
61         wait(&status);
62         printf("Child process status: %d\n", status);
63 #ifndef RTE_EAL_ALWAYS_PANIC_ON_ERROR
64         if(!WIFEXITED(status) || WEXITSTATUS(status) != (uint8_t)exit_val){
65                 printf("Child process terminated with incorrect status (expected = %d)!\n",
66                                 exit_val);
67                 return -1;
68         }
69 #endif
70         return 0;
71 }
72
73 static int
74 test_exit(void)
75 {
76         int test_vals[] = { 0, 1, 2, 255, -1 };
77         unsigned i;
78         for (i = 0; i < sizeof(test_vals) / sizeof(test_vals[0]); i++){
79                 if (test_exit_val(test_vals[i]) < 0)
80                         return -1;
81         }
82         printf("%s Passed\n", __func__);
83         return 0;
84 }
85
86 static void
87 dummy_app_usage(const char *progname)
88 {
89         RTE_SET_USED(progname);
90 }
91
92 static int
93 test_usage(void)
94 {
95         if (rte_set_application_usage_hook(dummy_app_usage) != NULL) {
96                 printf("Non-NULL value returned for initial usage hook\n");
97                 return -1;
98         }
99         if (rte_set_application_usage_hook(NULL) != dummy_app_usage) {
100                 printf("Incorrect value returned for application usage hook\n");
101                 return -1;
102         }
103         return 0;
104 }
105
106 static int
107 test_debug(void)
108 {
109         rte_dump_stack();
110         rte_dump_registers();
111         if (test_panic() < 0)
112                 return -1;
113         if (test_exit() < 0)
114                 return -1;
115         if (test_usage() < 0)
116                 return -1;
117         return 0;
118 }
119
120 REGISTER_TEST_COMMAND(debug_autotest, test_debug);