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