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