1 /*
2  * Copyright (c) 2019 Intel corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /* Disable syscall tracing for all calls from this compilation unit to avoid
8  * undefined symbols as the macros are not expanded recursively
9  */
10 #define DISABLE_SYSCALL_TRACING
11 
12 #include <zephyr/init.h>
13 #include <string.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/sys/util.h>
16 #include <zephyr/sys/atomic.h>
17 #include <tracing_core.h>
18 #include <tracing_buffer.h>
19 #include <tracing_backend.h>
20 
21 #define TRACING_CMD_ENABLE  "enable"
22 #define TRACING_CMD_DISABLE "disable"
23 
24 #ifdef CONFIG_TRACING_BACKEND_UART
25 #define TRACING_BACKEND_NAME "tracing_backend_uart"
26 #elif defined CONFIG_TRACING_BACKEND_USB
27 #define TRACING_BACKEND_NAME "tracing_backend_usb"
28 #elif defined CONFIG_TRACING_BACKEND_POSIX
29 #define TRACING_BACKEND_NAME "tracing_backend_posix"
30 #elif defined CONFIG_TRACING_BACKEND_RAM
31 #define TRACING_BACKEND_NAME "tracing_backend_ram"
32 #else
33 #define TRACING_BACKEND_NAME ""
34 #endif
35 
36 enum tracing_state {
37 	TRACING_DISABLE = 0,
38 	TRACING_ENABLE
39 };
40 
41 static atomic_t tracing_state;
42 static atomic_t tracing_packet_drop_num;
43 static struct tracing_backend *working_backend;
44 
45 #ifdef CONFIG_TRACING_ASYNC
46 #define TRACING_THREAD_NAME "tracing_thread"
47 
48 static k_tid_t tracing_thread_tid;
49 static struct k_thread tracing_thread;
50 static struct k_timer tracing_thread_timer;
51 static K_SEM_DEFINE(tracing_thread_sem, 0, 1);
52 static K_THREAD_STACK_DEFINE(tracing_thread_stack,
53 			CONFIG_TRACING_THREAD_STACK_SIZE);
54 
tracing_thread_func(void * dummy1,void * dummy2,void * dummy3)55 static void tracing_thread_func(void *dummy1, void *dummy2, void *dummy3)
56 {
57 	uint8_t *transferring_buf;
58 	uint32_t transferring_length, tracing_buffer_max_length;
59 
60 	tracing_thread_tid = k_current_get();
61 
62 	tracing_buffer_max_length = tracing_buffer_capacity_get();
63 
64 	while (true) {
65 		if (tracing_buffer_is_empty()) {
66 			k_sem_take(&tracing_thread_sem, K_FOREVER);
67 		} else {
68 			transferring_length =
69 				tracing_buffer_get_claim(
70 						&transferring_buf,
71 						tracing_buffer_max_length);
72 			tracing_buffer_handle(transferring_buf,
73 					      transferring_length);
74 			tracing_buffer_get_finish(transferring_length);
75 		}
76 	}
77 }
78 
tracing_thread_timer_expiry_fn(struct k_timer * timer)79 static void tracing_thread_timer_expiry_fn(struct k_timer *timer)
80 {
81 	k_sem_give(&tracing_thread_sem);
82 }
83 #endif
84 
tracing_set_state(enum tracing_state state)85 static void tracing_set_state(enum tracing_state state)
86 {
87 	atomic_set(&tracing_state, state);
88 }
89 
tracing_init(void)90 static int tracing_init(void)
91 {
92 
93 	tracing_buffer_init();
94 
95 	working_backend = tracing_backend_get(TRACING_BACKEND_NAME);
96 	tracing_backend_init(working_backend);
97 
98 	atomic_set(&tracing_packet_drop_num, 0);
99 
100 	if (IS_ENABLED(CONFIG_TRACING_HANDLE_HOST_CMD)) {
101 		tracing_set_state(TRACING_DISABLE);
102 	} else {
103 		tracing_set_state(TRACING_ENABLE);
104 	}
105 
106 #ifdef CONFIG_TRACING_ASYNC
107 	k_timer_init(&tracing_thread_timer,
108 		     tracing_thread_timer_expiry_fn, NULL);
109 
110 	k_thread_create(&tracing_thread, tracing_thread_stack,
111 			K_THREAD_STACK_SIZEOF(tracing_thread_stack),
112 			tracing_thread_func, NULL, NULL, NULL,
113 			K_LOWEST_APPLICATION_THREAD_PRIO, 0, K_NO_WAIT);
114 	k_thread_name_set(&tracing_thread, TRACING_THREAD_NAME);
115 #endif
116 
117 	return 0;
118 }
119 
120 SYS_INIT(tracing_init, APPLICATION, 0);
121 
122 #ifdef CONFIG_TRACING_ASYNC
tracing_trigger_output(bool before_put_is_empty)123 void tracing_trigger_output(bool before_put_is_empty)
124 {
125 	if (before_put_is_empty) {
126 		k_timer_start(&tracing_thread_timer,
127 			      K_MSEC(CONFIG_TRACING_THREAD_WAIT_THRESHOLD),
128 			      K_NO_WAIT);
129 	}
130 }
131 
is_tracing_thread(void)132 bool is_tracing_thread(void)
133 {
134 	return (!k_is_in_isr() && (k_current_get() == tracing_thread_tid));
135 }
136 #endif
137 
is_tracing_enabled(void)138 bool is_tracing_enabled(void)
139 {
140 	return atomic_get(&tracing_state) == TRACING_ENABLE;
141 }
142 
tracing_cmd_handle(uint8_t * buf,uint32_t length)143 void tracing_cmd_handle(uint8_t *buf, uint32_t length)
144 {
145 	if (strncmp(buf, TRACING_CMD_ENABLE, length) == 0) {
146 		tracing_set_state(TRACING_ENABLE);
147 	} else if (strncmp(buf, TRACING_CMD_DISABLE, length) == 0) {
148 		tracing_set_state(TRACING_DISABLE);
149 	}
150 }
151 
tracing_buffer_handle(uint8_t * data,uint32_t length)152 void tracing_buffer_handle(uint8_t *data, uint32_t length)
153 {
154 	tracing_backend_output(working_backend, data, length);
155 }
156 
tracing_packet_drop_handle(void)157 void tracing_packet_drop_handle(void)
158 {
159 	atomic_inc(&tracing_packet_drop_num);
160 }
161