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