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 #include <rte_service_component.h> 14 15 #include "test.h" 16 17 /* 18 * Debug test 19 * ========== 20 */ 21 22 /* use fork() to test rte_panic() */ 23 static int 24 test_panic(void) 25 { 26 int pid; 27 int status; 28 29 pid = fork(); 30 31 if (pid == 0) 32 rte_panic("Test Debug\n"); 33 else if (pid < 0){ 34 printf("Fork Failed\n"); 35 return -1; 36 } 37 wait(&status); 38 if(status == 0){ 39 printf("Child process terminated normally!\n"); 40 return -1; 41 } else 42 printf("Child process terminated as expected - Test passed!\n"); 43 44 return 0; 45 } 46 47 /* use fork() to test rte_exit() */ 48 static int 49 test_exit_val(int exit_val) 50 { 51 int pid; 52 int status; 53 54 /* manually cleanup EAL memory, as the fork() below would otherwise 55 * cause the same hugepages to be free()-ed multiple times. 56 */ 57 rte_service_finalize(); 58 59 pid = fork(); 60 61 if (pid == 0) 62 rte_exit(exit_val, __func__); 63 else if (pid < 0){ 64 printf("Fork Failed\n"); 65 return -1; 66 } 67 wait(&status); 68 printf("Child process status: %d\n", status); 69 if(!WIFEXITED(status) || WEXITSTATUS(status) != (uint8_t)exit_val){ 70 printf("Child process terminated with incorrect status (expected = %d)!\n", 71 exit_val); 72 return -1; 73 } 74 return 0; 75 } 76 77 static int 78 test_exit(void) 79 { 80 int test_vals[] = { 0, 1, 2, 255, -1 }; 81 unsigned i; 82 for (i = 0; i < RTE_DIM(test_vals); i++) { 83 if (test_exit_val(test_vals[i]) < 0) 84 return -1; 85 } 86 printf("%s Passed\n", __func__); 87 return 0; 88 } 89 90 static void 91 dummy_app_usage(const char *progname) 92 { 93 RTE_SET_USED(progname); 94 } 95 96 static int 97 test_usage(void) 98 { 99 if (rte_set_application_usage_hook(dummy_app_usage) != NULL) { 100 printf("Non-NULL value returned for initial usage hook\n"); 101 return -1; 102 } 103 if (rte_set_application_usage_hook(NULL) != dummy_app_usage) { 104 printf("Incorrect value returned for application usage hook\n"); 105 return -1; 106 } 107 return 0; 108 } 109 110 static int 111 test_debug(void) 112 { 113 rte_dump_stack(); 114 if (test_panic() < 0) 115 return -1; 116 if (test_exit() < 0) 117 return -1; 118 if (test_usage() < 0) 119 return -1; 120 return 0; 121 } 122 123 REGISTER_TEST_COMMAND(debug_autotest, test_debug); 124