1 /*
2  * Copyright (c) 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/kernel_structs.h>
9 #include <kernel_internal.h>
10 #include <ctf_top.h>
11 #include <zephyr/net/net_core.h>
12 #include <zephyr/net/net_ip.h>
13 #include <zephyr/net/socket_poll.h>
14 #include <zephyr/net/net_if.h>
15 #include <zephyr/net/net_pkt.h>
16 #include <zephyr/debug/cpu_load.h>
17 
_get_thread_name(struct k_thread * thread,ctf_bounded_string_t * name)18 static void _get_thread_name(struct k_thread *thread,
19 			     ctf_bounded_string_t *name)
20 {
21 	const char *tname = k_thread_name_get(thread);
22 
23 	if (tname != NULL && tname[0] != '\0') {
24 		strncpy(name->buf, tname, sizeof(name->buf));
25 		/* strncpy may not always null-terminate */
26 		name->buf[sizeof(name->buf) - 1] = 0;
27 	}
28 }
29 
sys_trace_k_thread_switched_out(void)30 void sys_trace_k_thread_switched_out(void)
31 {
32 	ctf_bounded_string_t name = { "unknown" };
33 	struct k_thread *thread;
34 
35 	thread = k_sched_current_thread_query();
36 	_get_thread_name(thread, &name);
37 
38 	ctf_top_thread_switched_out((uint32_t)(uintptr_t)thread, name);
39 }
40 
sys_trace_k_thread_user_mode_enter(void)41 void sys_trace_k_thread_user_mode_enter(void)
42 {
43 	struct k_thread *thread;
44 	ctf_bounded_string_t name = { "unknown" };
45 
46 	thread = k_sched_current_thread_query();
47 	_get_thread_name(thread, &name);
48 	ctf_top_thread_user_mode_enter((uint32_t)(uintptr_t)thread, name);
49 }
50 
sys_trace_k_thread_wakeup(struct k_thread * thread)51 void sys_trace_k_thread_wakeup(struct k_thread *thread)
52 {
53 	ctf_bounded_string_t name = { "unknown" };
54 
55 	_get_thread_name(thread, &name);
56 	ctf_top_thread_wakeup((uint32_t)(uintptr_t)thread, name);
57 }
58 
59 
sys_trace_k_thread_switched_in(void)60 void sys_trace_k_thread_switched_in(void)
61 {
62 	struct k_thread *thread;
63 	ctf_bounded_string_t name = { "unknown" };
64 
65 	thread = k_sched_current_thread_query();
66 	_get_thread_name(thread, &name);
67 
68 	ctf_top_thread_switched_in((uint32_t)(uintptr_t)thread, name);
69 }
70 
sys_trace_k_thread_priority_set(struct k_thread * thread)71 void sys_trace_k_thread_priority_set(struct k_thread *thread)
72 {
73 	ctf_bounded_string_t name = { "unknown" };
74 
75 	_get_thread_name(thread, &name);
76 	ctf_top_thread_priority_set((uint32_t)(uintptr_t)thread,
77 				    thread->base.prio, name);
78 }
79 
sys_trace_k_thread_sleep_enter(k_timeout_t timeout)80 void sys_trace_k_thread_sleep_enter(k_timeout_t timeout)
81 {
82 	ctf_top_thread_sleep_enter(
83 		k_ticks_to_us_floor32((uint32_t)timeout.ticks)
84 		);
85 }
86 
sys_trace_k_thread_sleep_exit(k_timeout_t timeout,int ret)87 void sys_trace_k_thread_sleep_exit(k_timeout_t timeout, int ret)
88 {
89 	ctf_top_thread_sleep_exit(
90 		k_ticks_to_us_floor32((uint32_t)timeout.ticks),
91 		(uint32_t)ret
92 		);
93 }
94 
sys_trace_k_thread_create(struct k_thread * thread,size_t stack_size,int prio)95 void sys_trace_k_thread_create(struct k_thread *thread, size_t stack_size, int prio)
96 {
97 	ctf_bounded_string_t name = { "unknown" };
98 
99 	_get_thread_name(thread, &name);
100 	ctf_top_thread_create(
101 		(uint32_t)(uintptr_t)thread,
102 		thread->base.prio,
103 		name
104 		);
105 
106 #if defined(CONFIG_THREAD_STACK_INFO)
107 	ctf_top_thread_info(
108 		(uint32_t)(uintptr_t)thread,
109 		name,
110 		thread->stack_info.start,
111 		thread->stack_info.size
112 		);
113 #endif
114 }
115 
sys_trace_k_thread_abort(struct k_thread * thread)116 void sys_trace_k_thread_abort(struct k_thread *thread)
117 {
118 	ctf_bounded_string_t name = { "unknown" };
119 
120 	_get_thread_name(thread, &name);
121 	ctf_top_thread_abort((uint32_t)(uintptr_t)thread, name);
122 }
123 
sys_trace_k_thread_suspend(struct k_thread * thread)124 void sys_trace_k_thread_suspend(struct k_thread *thread)
125 {
126 	ctf_bounded_string_t name = { "unknown" };
127 
128 	_get_thread_name(thread, &name);
129 	ctf_top_thread_suspend((uint32_t)(uintptr_t)thread, name);
130 }
131 
sys_trace_k_thread_resume(struct k_thread * thread)132 void sys_trace_k_thread_resume(struct k_thread *thread)
133 {
134 	ctf_bounded_string_t name = { "unknown" };
135 
136 	_get_thread_name(thread, &name);
137 
138 	ctf_top_thread_resume((uint32_t)(uintptr_t)thread, name);
139 }
140 
sys_trace_k_thread_ready(struct k_thread * thread)141 void sys_trace_k_thread_ready(struct k_thread *thread)
142 {
143 	ctf_bounded_string_t name = { "unknown" };
144 
145 	_get_thread_name(thread, &name);
146 
147 	ctf_top_thread_ready((uint32_t)(uintptr_t)thread, name);
148 }
149 
sys_trace_k_thread_start(struct k_thread * thread)150 void sys_trace_k_thread_start(struct k_thread *thread)
151 {
152 
153 }
154 
sys_trace_k_thread_pend(struct k_thread * thread)155 void sys_trace_k_thread_pend(struct k_thread *thread)
156 {
157 	ctf_bounded_string_t name = { "unknown" };
158 
159 	_get_thread_name(thread, &name);
160 	ctf_top_thread_pend((uint32_t)(uintptr_t)thread, name);
161 }
162 
sys_trace_k_thread_info(struct k_thread * thread)163 void sys_trace_k_thread_info(struct k_thread *thread)
164 {
165 #if defined(CONFIG_THREAD_STACK_INFO)
166 	ctf_bounded_string_t name = { "unknown" };
167 
168 	_get_thread_name(thread, &name);
169 	ctf_top_thread_info(
170 		(uint32_t)(uintptr_t)thread,
171 		name,
172 		thread->stack_info.start,
173 		thread->stack_info.size
174 		);
175 #endif
176 }
177 
sys_trace_k_thread_name_set(struct k_thread * thread,int ret)178 void sys_trace_k_thread_name_set(struct k_thread *thread, int ret)
179 {
180 	ctf_bounded_string_t name = { "unknown" };
181 
182 	_get_thread_name(thread, &name);
183 	ctf_top_thread_name_set(
184 		(uint32_t)(uintptr_t)thread,
185 		name
186 		);
187 
188 }
189 
sys_trace_isr_enter(void)190 void sys_trace_isr_enter(void)
191 {
192 	ctf_top_isr_enter();
193 }
194 
sys_trace_isr_exit(void)195 void sys_trace_isr_exit(void)
196 {
197 	ctf_top_isr_exit();
198 }
199 
sys_trace_isr_exit_to_scheduler(void)200 void sys_trace_isr_exit_to_scheduler(void)
201 {
202 	ctf_top_isr_exit_to_scheduler();
203 }
204 
sys_trace_idle(void)205 void sys_trace_idle(void)
206 {
207 #ifdef CONFIG_TRACING_IDLE
208 	ctf_top_idle();
209 #endif
210 	if (IS_ENABLED(CONFIG_CPU_LOAD)) {
211 		cpu_load_on_enter_idle();
212 	}
213 }
214 
sys_trace_idle_exit(void)215 void sys_trace_idle_exit(void)
216 {
217 	if (IS_ENABLED(CONFIG_CPU_LOAD)) {
218 		cpu_load_on_exit_idle();
219 	}
220 }
221 
222 /* Semaphore */
sys_trace_k_sem_init(struct k_sem * sem,int ret)223 void sys_trace_k_sem_init(struct k_sem *sem, int ret)
224 {
225 	ctf_top_semaphore_init(
226 		(uint32_t)(uintptr_t)sem,
227 		(int32_t)ret
228 		);
229 }
230 
sys_trace_k_sem_take_enter(struct k_sem * sem,k_timeout_t timeout)231 void sys_trace_k_sem_take_enter(struct k_sem *sem, k_timeout_t timeout)
232 {
233 	ctf_top_semaphore_take_enter(
234 		(uint32_t)(uintptr_t)sem,
235 		k_ticks_to_us_floor32((uint32_t)timeout.ticks)
236 		);
237 }
238 
239 
sys_trace_k_sem_take_blocking(struct k_sem * sem,k_timeout_t timeout)240 void sys_trace_k_sem_take_blocking(struct k_sem *sem, k_timeout_t timeout)
241 {
242 	ctf_top_semaphore_take_blocking(
243 		(uint32_t)(uintptr_t)sem,
244 		k_ticks_to_us_floor32((uint32_t)timeout.ticks)
245 		);
246 }
247 
sys_trace_k_sem_take_exit(struct k_sem * sem,k_timeout_t timeout,int ret)248 void sys_trace_k_sem_take_exit(struct k_sem *sem, k_timeout_t timeout, int ret)
249 {
250 	ctf_top_semaphore_take_exit(
251 		(uint32_t)(uintptr_t)sem,
252 		k_ticks_to_us_floor32((uint32_t)timeout.ticks),
253 		(uint32_t)ret
254 		);
255 }
256 
sys_trace_k_sem_reset(struct k_sem * sem)257 void sys_trace_k_sem_reset(struct k_sem *sem)
258 {
259 	ctf_top_semaphore_reset(
260 		(uint32_t)(uintptr_t)sem
261 		);
262 }
263 
sys_trace_k_sem_give_enter(struct k_sem * sem)264 void sys_trace_k_sem_give_enter(struct k_sem *sem)
265 {
266 	ctf_top_semaphore_give_enter(
267 		(uint32_t)(uintptr_t)sem
268 		);
269 }
270 
sys_trace_k_sem_give_exit(struct k_sem * sem)271 void sys_trace_k_sem_give_exit(struct k_sem *sem)
272 {
273 	ctf_top_semaphore_give_exit(
274 		(uint32_t)(uintptr_t)sem
275 		);
276 }
277 
278 /* Mutex */
sys_trace_k_mutex_init(struct k_mutex * mutex,int ret)279 void sys_trace_k_mutex_init(struct k_mutex *mutex, int ret)
280 {
281 	ctf_top_mutex_init(
282 		(uint32_t)(uintptr_t)mutex,
283 		(int32_t)ret
284 		);
285 }
286 
sys_trace_k_mutex_lock_enter(struct k_mutex * mutex,k_timeout_t timeout)287 void sys_trace_k_mutex_lock_enter(struct k_mutex *mutex, k_timeout_t timeout)
288 {
289 	ctf_top_mutex_lock_enter(
290 		(uint32_t)(uintptr_t)mutex,
291 		k_ticks_to_us_floor32((uint32_t)timeout.ticks)
292 		);
293 }
294 
sys_trace_k_mutex_lock_blocking(struct k_mutex * mutex,k_timeout_t timeout)295 void sys_trace_k_mutex_lock_blocking(struct k_mutex *mutex, k_timeout_t timeout)
296 {
297 	ctf_top_mutex_lock_blocking(
298 		(uint32_t)(uintptr_t)mutex,
299 		k_ticks_to_us_floor32((uint32_t)timeout.ticks)
300 		);
301 }
302 
sys_trace_k_mutex_lock_exit(struct k_mutex * mutex,k_timeout_t timeout,int ret)303 void sys_trace_k_mutex_lock_exit(struct k_mutex *mutex, k_timeout_t timeout, int ret)
304 {
305 	ctf_top_mutex_lock_exit(
306 		(uint32_t)(uintptr_t)mutex,
307 		k_ticks_to_us_floor32((uint32_t)timeout.ticks),
308 		(int32_t)ret
309 		);
310 }
311 
sys_trace_k_mutex_unlock_enter(struct k_mutex * mutex)312 void sys_trace_k_mutex_unlock_enter(struct k_mutex *mutex)
313 {
314 	ctf_top_mutex_unlock_enter(
315 		(uint32_t)(uintptr_t)mutex
316 		);
317 }
318 
sys_trace_k_mutex_unlock_exit(struct k_mutex * mutex,int ret)319 void sys_trace_k_mutex_unlock_exit(struct k_mutex *mutex, int ret)
320 {
321 	ctf_top_mutex_unlock_exit(
322 		(uint32_t)(uintptr_t)mutex,
323 		(int32_t)ret
324 		);
325 }
326 
327 /* Timer */
sys_trace_k_timer_init(struct k_timer * timer)328 void sys_trace_k_timer_init(struct k_timer *timer)
329 {
330 	ctf_top_timer_init(
331 		(uint32_t)(uintptr_t)timer);
332 }
333 
sys_trace_k_timer_start(struct k_timer * timer,k_timeout_t duration,k_timeout_t period)334 void sys_trace_k_timer_start(struct k_timer *timer, k_timeout_t duration,
335 			     k_timeout_t period)
336 {
337 	ctf_top_timer_start(
338 		(uint32_t)(uintptr_t)timer,
339 		k_ticks_to_us_floor32((uint32_t)duration.ticks),
340 		k_ticks_to_us_floor32((uint32_t)period.ticks)
341 		);
342 }
343 
sys_trace_k_timer_stop(struct k_timer * timer)344 void sys_trace_k_timer_stop(struct k_timer *timer)
345 {
346 	ctf_top_timer_stop(
347 		(uint32_t)(uintptr_t)timer
348 		);
349 }
350 
sys_trace_k_timer_status_sync_enter(struct k_timer * timer)351 void sys_trace_k_timer_status_sync_enter(struct k_timer *timer)
352 {
353 	ctf_top_timer_status_sync_enter(
354 		(uint32_t)(uintptr_t)timer
355 		);
356 }
357 
sys_trace_k_timer_status_sync_blocking(struct k_timer * timer,k_timeout_t timeout)358 void sys_trace_k_timer_status_sync_blocking(struct k_timer *timer, k_timeout_t timeout)
359 {
360 	ctf_top_timer_status_sync_blocking(
361 		(uint32_t)(uintptr_t)timer,
362 		k_ticks_to_us_floor32((uint32_t)timeout.ticks)
363 		);
364 }
365 
sys_trace_k_timer_status_sync_exit(struct k_timer * timer,uint32_t result)366 void sys_trace_k_timer_status_sync_exit(struct k_timer *timer, uint32_t result)
367 {
368 	ctf_top_timer_status_sync_exit(
369 		(uint32_t)(uintptr_t)timer,
370 		result
371 		);
372 }
373 
374 /* Network socket */
sys_trace_socket_init(int sock,int family,int type,int proto)375 void sys_trace_socket_init(int sock, int family, int type, int proto)
376 {
377 	ctf_top_socket_init(sock, family, type, proto);
378 }
379 
sys_trace_socket_close_enter(int sock)380 void sys_trace_socket_close_enter(int sock)
381 {
382 	ctf_top_socket_close_enter(sock);
383 }
384 
sys_trace_socket_close_exit(int sock,int ret)385 void sys_trace_socket_close_exit(int sock, int ret)
386 {
387 	ctf_top_socket_close_exit(sock, ret);
388 }
389 
sys_trace_socket_shutdown_enter(int sock,int how)390 void sys_trace_socket_shutdown_enter(int sock, int how)
391 {
392 	ctf_top_socket_shutdown_enter(sock, how);
393 }
394 
sys_trace_socket_shutdown_exit(int sock,int ret)395 void sys_trace_socket_shutdown_exit(int sock, int ret)
396 {
397 	ctf_top_socket_shutdown_exit(sock, ret);
398 }
399 
sys_trace_socket_bind_enter(int sock,const struct sockaddr * addr,size_t addrlen)400 void sys_trace_socket_bind_enter(int sock, const struct sockaddr *addr, size_t addrlen)
401 {
402 	ctf_net_bounded_string_t addr_str;
403 
404 	(void)net_addr_ntop(addr->sa_family, &net_sin(addr)->sin_addr,
405 			    addr_str.buf, sizeof(addr_str.buf));
406 
407 	ctf_top_socket_bind_enter(sock, addr_str, addrlen, ntohs(net_sin(addr)->sin_port));
408 }
409 
sys_trace_socket_bind_exit(int sock,int ret)410 void sys_trace_socket_bind_exit(int sock, int ret)
411 {
412 	ctf_top_socket_bind_exit(sock, ret);
413 }
414 
sys_trace_socket_connect_enter(int sock,const struct sockaddr * addr,size_t addrlen)415 void sys_trace_socket_connect_enter(int sock, const struct sockaddr *addr, size_t addrlen)
416 {
417 	ctf_net_bounded_string_t addr_str;
418 
419 	(void)net_addr_ntop(addr->sa_family, &net_sin(addr)->sin_addr,
420 			    addr_str.buf, sizeof(addr_str.buf));
421 
422 	ctf_top_socket_connect_enter(sock, addr_str, addrlen);
423 }
424 
sys_trace_socket_connect_exit(int sock,int ret)425 void sys_trace_socket_connect_exit(int sock, int ret)
426 {
427 	ctf_top_socket_connect_exit(sock, ret);
428 }
429 
sys_trace_socket_listen_enter(int sock,int backlog)430 void sys_trace_socket_listen_enter(int sock, int backlog)
431 {
432 	ctf_top_socket_listen_enter(sock, backlog);
433 }
434 
sys_trace_socket_listen_exit(int sock,int ret)435 void sys_trace_socket_listen_exit(int sock, int ret)
436 {
437 	ctf_top_socket_listen_exit(sock, ret);
438 }
439 
sys_trace_socket_accept_enter(int sock)440 void sys_trace_socket_accept_enter(int sock)
441 {
442 	ctf_top_socket_accept_enter(sock);
443 }
444 
sys_trace_socket_accept_exit(int sock,const struct sockaddr * addr,const size_t * addrlen,int ret)445 void sys_trace_socket_accept_exit(int sock, const struct sockaddr *addr,
446 				  const size_t *addrlen, int ret)
447 {
448 	ctf_net_bounded_string_t addr_str = { "unknown" };
449 	uint32_t addr_len = 0U;
450 	uint16_t port = 0U;
451 
452 	if (addr != NULL) {
453 		(void)net_addr_ntop(addr->sa_family, &net_sin(addr)->sin_addr,
454 				    addr_str.buf, sizeof(addr_str.buf));
455 		port = net_sin(addr)->sin_port;
456 	}
457 
458 	if (addrlen != NULL) {
459 		addr_len = *addrlen;
460 	}
461 
462 	ctf_top_socket_accept_exit(sock, addr_str, addr_len, port, ret);
463 }
464 
sys_trace_socket_sendto_enter(int sock,int len,int flags,const struct sockaddr * dest_addr,size_t addrlen)465 void sys_trace_socket_sendto_enter(int sock, int len, int flags,
466 				   const struct sockaddr *dest_addr, size_t addrlen)
467 {
468 	ctf_net_bounded_string_t addr_str = { "unknown" };
469 
470 	if (dest_addr != NULL) {
471 		(void)net_addr_ntop(dest_addr->sa_family, &net_sin(dest_addr)->sin_addr,
472 				    addr_str.buf, sizeof(addr_str.buf));
473 	}
474 
475 	ctf_top_socket_sendto_enter(sock, len, flags, addr_str, addrlen);
476 }
477 
sys_trace_socket_sendto_exit(int sock,int ret)478 void sys_trace_socket_sendto_exit(int sock, int ret)
479 {
480 	ctf_top_socket_sendto_exit(sock, ret);
481 }
482 
sys_trace_socket_sendmsg_enter(int sock,const struct msghdr * msg,int flags)483 void sys_trace_socket_sendmsg_enter(int sock, const struct msghdr *msg, int flags)
484 {
485 	ctf_net_bounded_string_t addr = { "unknown" };
486 	uint32_t len = 0;
487 
488 	for (int i = 0; msg->msg_iov != NULL && i < msg->msg_iovlen; i++) {
489 		len += msg->msg_iov[i].iov_len;
490 	}
491 
492 	if (msg->msg_name != NULL) {
493 		(void)net_addr_ntop(((struct sockaddr *)msg->msg_name)->sa_family,
494 				    &net_sin((struct sockaddr *)msg->msg_name)->sin_addr,
495 				    addr.buf, sizeof(addr.buf));
496 	}
497 
498 	ctf_top_socket_sendmsg_enter(sock, flags, (uint32_t)(uintptr_t)msg, addr, len);
499 }
500 
sys_trace_socket_sendmsg_exit(int sock,int ret)501 void sys_trace_socket_sendmsg_exit(int sock, int ret)
502 {
503 	ctf_top_socket_sendmsg_exit(sock, ret);
504 }
505 
sys_trace_socket_recvfrom_enter(int sock,int max_len,int flags,struct sockaddr * addr,size_t * addrlen)506 void sys_trace_socket_recvfrom_enter(int sock, int max_len, int flags,
507 				     struct sockaddr *addr, size_t *addrlen)
508 {
509 	ctf_top_socket_recvfrom_enter(sock, max_len, flags,
510 				      (uint32_t)(uintptr_t)addr,
511 				      (uint32_t)(uintptr_t)addrlen);
512 }
513 
sys_trace_socket_recvfrom_exit(int sock,const struct sockaddr * src_addr,const size_t * addrlen,int ret)514 void sys_trace_socket_recvfrom_exit(int sock, const struct sockaddr *src_addr,
515 				    const size_t *addrlen, int ret)
516 {
517 	ctf_net_bounded_string_t addr_str = { "unknown" };
518 	int len = 0;
519 
520 	if (src_addr != NULL) {
521 		(void)net_addr_ntop(src_addr->sa_family, &net_sin(src_addr)->sin_addr,
522 				    addr_str.buf, sizeof(addr_str.buf));
523 	}
524 
525 	if (addrlen != NULL) {
526 		len = *addrlen;
527 	}
528 
529 	ctf_top_socket_recvfrom_exit(sock, addr_str, len, ret);
530 }
531 
sys_trace_socket_recvmsg_enter(int sock,const struct msghdr * msg,int flags)532 void sys_trace_socket_recvmsg_enter(int sock, const struct msghdr *msg, int flags)
533 {
534 	uint32_t max_len = 0;
535 
536 	for (int i = 0; msg->msg_iov != NULL && i < msg->msg_iovlen; i++) {
537 		max_len += msg->msg_iov[i].iov_len;
538 	}
539 
540 	ctf_top_socket_recvmsg_enter(sock, (uint32_t)(uintptr_t)msg, max_len, flags);
541 }
542 
sys_trace_socket_recvmsg_exit(int sock,const struct msghdr * msg,int ret)543 void sys_trace_socket_recvmsg_exit(int sock, const struct msghdr *msg, int ret)
544 {
545 	uint32_t len = 0;
546 	ctf_net_bounded_string_t addr = { "unknown" };
547 
548 	for (int i = 0; msg->msg_iov != NULL && i < msg->msg_iovlen; i++) {
549 		len += msg->msg_iov[i].iov_len;
550 	}
551 
552 	if (msg->msg_name != NULL) {
553 		(void)net_addr_ntop(((struct sockaddr *)msg->msg_name)->sa_family,
554 				    &net_sin((struct sockaddr *)msg->msg_name)->sin_addr,
555 				    addr.buf, sizeof(addr.buf));
556 	}
557 
558 	ctf_top_socket_recvmsg_exit(sock, len, addr, ret);
559 }
560 
sys_trace_socket_fcntl_enter(int sock,int cmd,int flags)561 void sys_trace_socket_fcntl_enter(int sock, int cmd, int flags)
562 {
563 	ctf_top_socket_fcntl_enter(sock, cmd, flags);
564 }
565 
sys_trace_socket_fcntl_exit(int sock,int ret)566 void sys_trace_socket_fcntl_exit(int sock, int ret)
567 {
568 	ctf_top_socket_fcntl_exit(sock, ret);
569 }
570 
sys_trace_socket_ioctl_enter(int sock,int req)571 void sys_trace_socket_ioctl_enter(int sock, int req)
572 {
573 	ctf_top_socket_ioctl_enter(sock, req);
574 }
575 
sys_trace_socket_ioctl_exit(int sock,int ret)576 void sys_trace_socket_ioctl_exit(int sock, int ret)
577 {
578 	ctf_top_socket_ioctl_exit(sock, ret);
579 }
580 
sys_trace_socket_poll_value(int fd,int events)581 void sys_trace_socket_poll_value(int fd, int events)
582 {
583 	ctf_top_socket_poll_value(fd, events);
584 }
585 
sys_trace_socket_poll_enter(const struct zsock_pollfd * fds,int nfds,int timeout)586 void sys_trace_socket_poll_enter(const struct zsock_pollfd *fds, int nfds, int timeout)
587 {
588 	ctf_top_socket_poll_enter((uint32_t)(uintptr_t)fds, nfds, timeout);
589 
590 	for (int i = 0; i < nfds; i++) {
591 		sys_trace_socket_poll_value(fds[i].fd, fds[i].events);
592 	}
593 }
594 
sys_trace_socket_poll_exit(const struct zsock_pollfd * fds,int nfds,int ret)595 void sys_trace_socket_poll_exit(const struct zsock_pollfd *fds, int nfds, int ret)
596 {
597 	ctf_top_socket_poll_exit((uint32_t)(uintptr_t)fds, nfds, ret);
598 
599 	for (int i = 0; i < nfds; i++) {
600 		sys_trace_socket_poll_value(fds[i].fd, fds[i].revents);
601 	}
602 }
603 
sys_trace_socket_getsockopt_enter(int sock,int level,int optname)604 void sys_trace_socket_getsockopt_enter(int sock, int level, int optname)
605 {
606 	ctf_top_socket_getsockopt_enter(sock, level, optname);
607 }
608 
sys_trace_socket_getsockopt_exit(int sock,int level,int optname,void * optval,size_t optlen,int ret)609 void sys_trace_socket_getsockopt_exit(int sock, int level, int optname,
610 				      void *optval, size_t optlen, int ret)
611 {
612 	ctf_top_socket_getsockopt_exit(sock, level, optname,
613 				       (uint32_t)(uintptr_t)optval, optlen, ret);
614 }
615 
sys_trace_socket_setsockopt_enter(int sock,int level,int optname,const void * optval,size_t optlen)616 void sys_trace_socket_setsockopt_enter(int sock, int level, int optname,
617 				       const void *optval, size_t optlen)
618 {
619 	ctf_top_socket_setsockopt_enter(sock, level, optname,
620 					(uint32_t)(uintptr_t)optval, optlen);
621 }
622 
sys_trace_socket_setsockopt_exit(int sock,int ret)623 void sys_trace_socket_setsockopt_exit(int sock, int ret)
624 {
625 	ctf_top_socket_setsockopt_exit(sock, ret);
626 }
627 
sys_trace_socket_getpeername_enter(int sock)628 void sys_trace_socket_getpeername_enter(int sock)
629 {
630 	ctf_top_socket_getpeername_enter(sock);
631 }
632 
sys_trace_socket_getpeername_exit(int sock,struct sockaddr * addr,const size_t * addrlen,int ret)633 void sys_trace_socket_getpeername_exit(int sock,  struct sockaddr *addr,
634 				       const size_t *addrlen, int ret)
635 {
636 	ctf_net_bounded_string_t addr_str;
637 
638 	(void)net_addr_ntop(addr->sa_family, &net_sin(addr)->sin_addr,
639 			    addr_str.buf, sizeof(addr_str.buf));
640 
641 	ctf_top_socket_getpeername_exit(sock, addr_str, *addrlen, ret);
642 }
643 
sys_trace_socket_getsockname_enter(int sock)644 void sys_trace_socket_getsockname_enter(int sock)
645 {
646 	ctf_top_socket_getsockname_enter(sock);
647 }
648 
sys_trace_socket_getsockname_exit(int sock,const struct sockaddr * addr,const size_t * addrlen,int ret)649 void sys_trace_socket_getsockname_exit(int sock, const struct sockaddr *addr,
650 				       const size_t *addrlen, int ret)
651 {
652 	ctf_net_bounded_string_t addr_str;
653 
654 	(void)net_addr_ntop(addr->sa_family, &net_sin(addr)->sin_addr,
655 			    addr_str.buf, sizeof(addr_str.buf));
656 
657 	ctf_top_socket_getsockname_exit(sock, addr_str, *addrlen, ret);
658 }
659 
sys_trace_socket_socketpair_enter(int family,int type,int proto,int * sv)660 void sys_trace_socket_socketpair_enter(int family, int type, int proto, int *sv)
661 {
662 	ctf_top_socket_socketpair_enter(family, type, proto, (uint32_t)(uintptr_t)sv);
663 }
664 
sys_trace_socket_socketpair_exit(int sock_A,int sock_B,int ret)665 void sys_trace_socket_socketpair_exit(int sock_A, int sock_B, int ret)
666 {
667 	ctf_top_socket_socketpair_exit(sock_A, sock_B, ret);
668 }
669 
sys_trace_net_recv_data_enter(struct net_if * iface,struct net_pkt * pkt)670 void sys_trace_net_recv_data_enter(struct net_if *iface, struct net_pkt *pkt)
671 {
672 	ctf_top_net_recv_data_enter((int32_t)net_if_get_by_iface(iface),
673 				    (uint32_t)(uintptr_t)iface,
674 				    (uint32_t)(uintptr_t)pkt,
675 				    (uint32_t)net_pkt_get_len(pkt));
676 }
677 
sys_trace_net_recv_data_exit(struct net_if * iface,struct net_pkt * pkt,int ret)678 void sys_trace_net_recv_data_exit(struct net_if *iface, struct net_pkt *pkt, int ret)
679 {
680 	ctf_top_net_recv_data_exit((int32_t)net_if_get_by_iface(iface),
681 				   (uint32_t)(uintptr_t)iface,
682 				   (uint32_t)(uintptr_t)pkt,
683 				   (int32_t)ret);
684 }
685 
sys_trace_net_send_data_enter(struct net_pkt * pkt)686 void sys_trace_net_send_data_enter(struct net_pkt *pkt)
687 {
688 	struct net_if *iface;
689 	int ifindex;
690 
691 	iface = net_pkt_iface(pkt);
692 	if (iface == NULL) {
693 		ifindex = -1;
694 	} else {
695 		ifindex = net_if_get_by_iface(iface);
696 	}
697 
698 	ctf_top_net_send_data_enter((int32_t)ifindex,
699 				    (uint32_t)(uintptr_t)iface,
700 				    (uint32_t)(uintptr_t)pkt,
701 				    (uint32_t)net_pkt_get_len(pkt));
702 }
703 
sys_trace_net_send_data_exit(struct net_pkt * pkt,int ret)704 void sys_trace_net_send_data_exit(struct net_pkt *pkt, int ret)
705 {
706 	struct net_if *iface;
707 	int ifindex;
708 
709 	iface = net_pkt_iface(pkt);
710 	if (iface == NULL) {
711 		ifindex = -1;
712 	} else {
713 		ifindex = net_if_get_by_iface(iface);
714 	}
715 
716 	ctf_top_net_send_data_exit((int32_t)ifindex,
717 				   (uint32_t)(uintptr_t)iface,
718 				   (uint32_t)(uintptr_t)pkt,
719 				   (int32_t)ret);
720 }
721 
sys_trace_net_rx_time(struct net_pkt * pkt,uint32_t end_time)722 void sys_trace_net_rx_time(struct net_pkt *pkt, uint32_t end_time)
723 {
724 	struct net_if *iface;
725 	int ifindex;
726 	uint32_t diff;
727 	int tc;
728 	uint32_t duration_us;
729 
730 	iface = net_pkt_iface(pkt);
731 	if (iface == NULL) {
732 		ifindex = -1;
733 		tc = 0;
734 		duration_us = 0;
735 	} else {
736 		ifindex = net_if_get_by_iface(iface);
737 		diff = end_time - net_pkt_create_time(pkt);
738 		tc = net_rx_priority2tc(net_pkt_priority(pkt));
739 		duration_us = k_cyc_to_ns_floor64(diff) / 1000U;
740 	}
741 
742 	ctf_top_net_rx_time((int32_t)ifindex,
743 			    (uint32_t)(uintptr_t)iface,
744 			    (uint32_t)(uintptr_t)pkt,
745 			    (uint32_t)net_pkt_priority(pkt),
746 			    (uint32_t)tc,
747 			    (uint32_t)duration_us);
748 }
749 
sys_trace_net_tx_time(struct net_pkt * pkt,uint32_t end_time)750 void sys_trace_net_tx_time(struct net_pkt *pkt, uint32_t end_time)
751 {
752 	struct net_if *iface;
753 	int ifindex;
754 	uint32_t diff;
755 	int tc;
756 	uint32_t duration_us;
757 
758 	iface = net_pkt_iface(pkt);
759 	if (iface == NULL) {
760 		ifindex = -1;
761 		tc = 0;
762 		duration_us = 0;
763 	} else {
764 		ifindex = net_if_get_by_iface(iface);
765 		diff = end_time - net_pkt_create_time(pkt);
766 		tc = net_rx_priority2tc(net_pkt_priority(pkt));
767 		duration_us = k_cyc_to_ns_floor64(diff) / 1000U;
768 	}
769 
770 	ctf_top_net_tx_time((int32_t)ifindex,
771 			    (uint32_t)(uintptr_t)iface,
772 			    (uint32_t)(uintptr_t)pkt,
773 			    (uint32_t)net_pkt_priority(pkt),
774 			    (uint32_t)tc,
775 			    (uint32_t)duration_us);
776 }
777 
sys_trace_named_event(const char * name,uint32_t arg0,uint32_t arg1)778 void sys_trace_named_event(const char *name, uint32_t arg0, uint32_t arg1)
779 {
780 	ctf_bounded_string_t ctf_name = {""};
781 
782 	strncpy(ctf_name.buf, name, CTF_MAX_STRING_LEN);
783 	/* Make sure buffer is NULL terminated */
784 	ctf_name.buf[CTF_MAX_STRING_LEN - 1] = '\0';
785 
786 	ctf_named_event(ctf_name, arg0, arg1);
787 }
788 
789 /* GPIO */
sys_port_trace_gpio_pin_interrupt_configure_enter(const struct device * port,gpio_pin_t pin,gpio_flags_t flags)790 void sys_port_trace_gpio_pin_interrupt_configure_enter(const struct device *port, gpio_pin_t pin,
791 						       gpio_flags_t flags)
792 {
793 	ctf_top_gpio_pin_interrupt_configure_enter((uint32_t)(uintptr_t)port, (uint32_t)pin,
794 						   (uint32_t)flags);
795 }
796 
sys_port_trace_gpio_pin_interrupt_configure_exit(const struct device * port,gpio_pin_t pin,int ret)797 void sys_port_trace_gpio_pin_interrupt_configure_exit(const struct device *port, gpio_pin_t pin,
798 						      int ret)
799 {
800 	ctf_top_gpio_pin_interrupt_configure_exit((uint32_t)(uintptr_t)port, (uint32_t)pin,
801 						  (int32_t)ret);
802 }
803 
sys_port_trace_gpio_pin_configure_enter(const struct device * port,gpio_pin_t pin,gpio_flags_t flags)804 void sys_port_trace_gpio_pin_configure_enter(const struct device *port, gpio_pin_t pin,
805 					     gpio_flags_t flags)
806 {
807 	ctf_top_gpio_pin_configure_enter((uint32_t)(uintptr_t)port, (uint32_t)pin, (uint32_t)flags);
808 }
809 
sys_port_trace_gpio_pin_configure_exit(const struct device * port,gpio_pin_t pin,int ret)810 void sys_port_trace_gpio_pin_configure_exit(const struct device *port, gpio_pin_t pin, int ret)
811 {
812 	ctf_top_gpio_pin_configure_exit((uint32_t)(uintptr_t)port, (uint32_t)pin, (int32_t)ret);
813 }
814 
sys_port_trace_gpio_port_get_direction_enter(const struct device * port,gpio_port_pins_t map,gpio_port_pins_t * inputs,gpio_port_pins_t * outputs)815 void sys_port_trace_gpio_port_get_direction_enter(const struct device *port, gpio_port_pins_t map,
816 						  gpio_port_pins_t *inputs,
817 						  gpio_port_pins_t *outputs)
818 {
819 	ctf_top_gpio_port_get_direction_enter((uint32_t)(uintptr_t)port, (uint32_t)map,
820 					      (uint32_t)(uintptr_t)inputs,
821 					      (uint32_t)(uintptr_t)outputs);
822 }
823 
sys_port_trace_gpio_port_get_direction_exit(const struct device * port,int ret)824 void sys_port_trace_gpio_port_get_direction_exit(const struct device *port, int ret)
825 {
826 	ctf_top_gpio_port_get_direction_exit((uint32_t)(uintptr_t)port, (int32_t)ret);
827 }
828 
sys_port_trace_gpio_pin_get_config_enter(const struct device * port,gpio_pin_t pin,gpio_flags_t flags)829 void sys_port_trace_gpio_pin_get_config_enter(const struct device *port, gpio_pin_t pin,
830 					      gpio_flags_t flags)
831 {
832 	ctf_top_gpio_pin_get_config_enter((uint32_t)(uintptr_t)port, (uint32_t)pin,
833 					  (uint32_t)flags);
834 }
835 
sys_port_trace_gpio_pin_get_config_exit(const struct device * port,gpio_pin_t pin,int ret)836 void sys_port_trace_gpio_pin_get_config_exit(const struct device *port, gpio_pin_t pin, int ret)
837 {
838 	ctf_top_gpio_pin_get_config_exit((uint32_t)(uintptr_t)port, (uint32_t)pin, (int32_t)ret);
839 }
840 
sys_port_trace_gpio_port_get_raw_enter(const struct device * port,gpio_port_value_t * value)841 void sys_port_trace_gpio_port_get_raw_enter(const struct device *port, gpio_port_value_t *value)
842 {
843 	ctf_top_gpio_port_get_raw_enter((uint32_t)(uintptr_t)port, (uint32_t)(uintptr_t)value);
844 }
845 
sys_port_trace_gpio_port_get_raw_exit(const struct device * port,int ret)846 void sys_port_trace_gpio_port_get_raw_exit(const struct device *port, int ret)
847 {
848 	ctf_top_gpio_port_get_raw_exit((uint32_t)(uintptr_t)port, (int32_t)ret);
849 }
850 
sys_port_trace_gpio_port_set_masked_raw_enter(const struct device * port,gpio_port_pins_t mask,gpio_port_value_t value)851 void sys_port_trace_gpio_port_set_masked_raw_enter(const struct device *port, gpio_port_pins_t mask,
852 						   gpio_port_value_t value)
853 {
854 	ctf_top_gpio_port_set_masked_raw_enter((uint32_t)(uintptr_t)port, (uint32_t)mask,
855 					       (uint32_t)value);
856 }
857 
sys_port_trace_gpio_port_set_masked_raw_exit(const struct device * port,int ret)858 void sys_port_trace_gpio_port_set_masked_raw_exit(const struct device *port, int ret)
859 {
860 	ctf_top_gpio_port_set_masked_raw_exit((uint32_t)(uintptr_t)port, (int32_t)ret);
861 }
862 
sys_port_trace_gpio_port_set_bits_raw_enter(const struct device * port,gpio_port_pins_t pins)863 void sys_port_trace_gpio_port_set_bits_raw_enter(const struct device *port, gpio_port_pins_t pins)
864 {
865 	ctf_top_gpio_port_set_bits_raw_enter((uint32_t)(uintptr_t)port, (uint32_t)pins);
866 }
867 
sys_port_trace_gpio_port_set_bits_raw_exit(const struct device * port,int ret)868 void sys_port_trace_gpio_port_set_bits_raw_exit(const struct device *port, int ret)
869 {
870 	ctf_top_gpio_port_set_bits_raw_exit((uint32_t)(uintptr_t)port, (int32_t)ret);
871 }
872 
sys_port_trace_gpio_port_clear_bits_raw_enter(const struct device * port,gpio_port_pins_t pins)873 void sys_port_trace_gpio_port_clear_bits_raw_enter(const struct device *port, gpio_port_pins_t pins)
874 {
875 	ctf_top_gpio_port_clear_bits_raw_enter((uint32_t)(uintptr_t)port, (uint32_t)pins);
876 }
877 
sys_port_trace_gpio_port_clear_bits_raw_exit(const struct device * port,int ret)878 void sys_port_trace_gpio_port_clear_bits_raw_exit(const struct device *port, int ret)
879 {
880 	ctf_top_gpio_port_clear_bits_raw_exit((uint32_t)(uintptr_t)port, (int32_t)ret);
881 }
882 
sys_port_trace_gpio_port_toggle_bits_enter(const struct device * port,gpio_port_pins_t pins)883 void sys_port_trace_gpio_port_toggle_bits_enter(const struct device *port, gpio_port_pins_t pins)
884 {
885 	ctf_top_gpio_port_toggle_bits_enter((uint32_t)(uintptr_t)port, (uint32_t)pins);
886 }
887 
sys_port_trace_gpio_port_toggle_bits_exit(const struct device * port,int ret)888 void sys_port_trace_gpio_port_toggle_bits_exit(const struct device *port, int ret)
889 {
890 	ctf_top_gpio_port_toggle_bits_exit((uint32_t)(uintptr_t)port, (int32_t)ret);
891 }
892 
sys_port_trace_gpio_init_callback_enter(struct gpio_callback * callback,gpio_callback_handler_t handler,gpio_port_pins_t pin_mask)893 void sys_port_trace_gpio_init_callback_enter(struct gpio_callback *callback,
894 					     gpio_callback_handler_t handler,
895 					     gpio_port_pins_t pin_mask)
896 {
897 	ctf_top_gpio_init_callback_enter((uint32_t)(uintptr_t)callback,
898 					 (uint32_t)(uintptr_t)handler, (uint32_t)pin_mask);
899 }
900 
sys_port_trace_gpio_init_callback_exit(struct gpio_callback * callback)901 void sys_port_trace_gpio_init_callback_exit(struct gpio_callback *callback)
902 {
903 	ctf_top_gpio_init_callback_exit((uint32_t)(uintptr_t)callback);
904 }
905 
sys_port_trace_gpio_add_callback_enter(const struct device * port,struct gpio_callback * callback)906 void sys_port_trace_gpio_add_callback_enter(const struct device *port,
907 					    struct gpio_callback *callback)
908 {
909 	ctf_top_gpio_add_callback_enter((uint32_t)(uintptr_t)port, (uint32_t)(uintptr_t)callback);
910 }
911 
sys_port_trace_gpio_add_callback_exit(const struct device * port,int ret)912 void sys_port_trace_gpio_add_callback_exit(const struct device *port, int ret)
913 {
914 	ctf_top_gpio_add_callback_exit((uint32_t)(uintptr_t)port, (int32_t)ret);
915 }
916 
sys_port_trace_gpio_remove_callback_enter(const struct device * port,struct gpio_callback * callback)917 void sys_port_trace_gpio_remove_callback_enter(const struct device *port,
918 					       struct gpio_callback *callback)
919 {
920 	ctf_top_gpio_remove_callback_enter((uint32_t)(uintptr_t)port,
921 					   (uint32_t)(uintptr_t)callback);
922 }
923 
sys_port_trace_gpio_remove_callback_exit(const struct device * port,int ret)924 void sys_port_trace_gpio_remove_callback_exit(const struct device *port, int ret)
925 {
926 	ctf_top_gpio_remove_callback_exit((uint32_t)(uintptr_t)port, (int32_t)ret);
927 }
928 
sys_port_trace_gpio_get_pending_int_enter(const struct device * dev)929 void sys_port_trace_gpio_get_pending_int_enter(const struct device *dev)
930 {
931 	ctf_top_gpio_get_pending_int_enter((uint32_t)(uintptr_t)dev);
932 }
933 
sys_port_trace_gpio_get_pending_int_exit(const struct device * dev,int ret)934 void sys_port_trace_gpio_get_pending_int_exit(const struct device *dev, int ret)
935 {
936 	ctf_top_gpio_get_pending_int_exit((uint32_t)(uintptr_t)dev, (int32_t)ret);
937 }
938 
sys_port_trace_gpio_fire_callbacks_enter(sys_slist_t * list,const struct device * port,gpio_port_pins_t pins)939 void sys_port_trace_gpio_fire_callbacks_enter(sys_slist_t *list, const struct device *port,
940 					      gpio_port_pins_t pins)
941 {
942 	ctf_top_gpio_fire_callbacks_enter((uint32_t)(uintptr_t)list, (uint32_t)(uintptr_t)port,
943 					  (uint32_t)pins);
944 }
945 
sys_port_trace_gpio_fire_callback(const struct device * port,struct gpio_callback * cb)946 void sys_port_trace_gpio_fire_callback(const struct device *port, struct gpio_callback *cb)
947 {
948 	ctf_top_gpio_fire_callback((uint32_t)(uintptr_t)port, (uint32_t)(uintptr_t)cb);
949 }
950