1 /*
2  * Copyright (c) 2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/init.h>
8 #include <zephyr/spinlock.h>
9 #include <zephyr/sys/slist.h>
10 #include <zephyr/tracing/tracking.h>
11 #include <zephyr/sys/iterable_sections.h>
12 
13 struct k_timer *_track_list_k_timer;
14 struct k_spinlock _track_list_k_timer_lock;
15 
16 struct k_mem_slab *_track_list_k_mem_slab;
17 struct k_spinlock _track_list_k_mem_slab_lock;
18 
19 struct k_sem *_track_list_k_sem;
20 struct k_spinlock _track_list_k_sem_lock;
21 
22 struct k_mutex *_track_list_k_mutex;
23 struct k_spinlock _track_list_k_mutex_lock;
24 
25 struct k_stack *_track_list_k_stack;
26 struct k_spinlock _track_list_k_stack_lock;
27 
28 struct k_msgq *_track_list_k_msgq;
29 struct k_spinlock _track_list_k_msgq_lock;
30 
31 struct k_mbox *_track_list_k_mbox;
32 struct k_spinlock _track_list_k_mbox_lock;
33 
34 #ifdef CONFIG_PIPES
35 struct k_pipe *_track_list_k_pipe;
36 struct k_spinlock _track_list_k_pipe_lock;
37 #endif
38 
39 struct k_queue *_track_list_k_queue;
40 struct k_spinlock _track_list_k_queue_lock;
41 
42 #ifdef CONFIG_EVENTS
43 struct k_event *_track_list_k_event;
44 struct k_spinlock _track_list_k_event_lock;
45 #endif
46 
47 #define SYS_TRACK_LIST_PREPEND(list, obj) \
48 	do { \
49 		k_spinlock_key_t key = k_spin_lock(&list ## _lock); \
50 		obj->_obj_track_next = list; \
51 		list = obj; \
52 		k_spin_unlock(&list ## _lock, key); \
53 	} while (false)
54 
55 #define SYS_TRACK_STATIC_INIT(type, ...) \
56 	do { \
57 		STRUCT_SECTION_FOREACH(type, obj) \
58 			_SYS_PORT_TRACKING_OBJ_INIT(type)(obj, ##__VA_ARGS__); \
59 	} while (false)
60 
61 
sys_track_k_timer_init(struct k_timer * timer)62 void sys_track_k_timer_init(struct k_timer *timer)
63 {
64 	SYS_PORT_TRACING_TYPE_MASK(k_timer,
65 			SYS_TRACK_LIST_PREPEND(_track_list_k_timer, timer));
66 }
67 
sys_track_k_mem_slab_init(struct k_mem_slab * slab)68 void sys_track_k_mem_slab_init(struct k_mem_slab *slab)
69 {
70 	SYS_PORT_TRACING_TYPE_MASK(k_mem_slab,
71 			SYS_TRACK_LIST_PREPEND(_track_list_k_mem_slab, slab));
72 }
73 
sys_track_k_sem_init(struct k_sem * sem)74 void sys_track_k_sem_init(struct k_sem *sem)
75 {
76 	if (sem) {
77 		SYS_PORT_TRACING_TYPE_MASK(k_sem,
78 				SYS_TRACK_LIST_PREPEND(_track_list_k_sem, sem));
79 	}
80 }
81 
sys_track_k_mutex_init(struct k_mutex * mutex)82 void sys_track_k_mutex_init(struct k_mutex *mutex)
83 {
84 	SYS_PORT_TRACING_TYPE_MASK(k_mutex,
85 			SYS_TRACK_LIST_PREPEND(_track_list_k_mutex, mutex));
86 }
87 
sys_track_k_stack_init(struct k_stack * stack)88 void sys_track_k_stack_init(struct k_stack *stack)
89 {
90 	SYS_PORT_TRACING_TYPE_MASK(k_stack,
91 			SYS_TRACK_LIST_PREPEND(_track_list_k_stack, stack));
92 }
93 
sys_track_k_msgq_init(struct k_msgq * msgq)94 void sys_track_k_msgq_init(struct k_msgq *msgq)
95 {
96 	SYS_PORT_TRACING_TYPE_MASK(k_msgq,
97 			SYS_TRACK_LIST_PREPEND(_track_list_k_msgq, msgq));
98 }
99 
sys_track_k_mbox_init(struct k_mbox * mbox)100 void sys_track_k_mbox_init(struct k_mbox *mbox)
101 {
102 	SYS_PORT_TRACING_TYPE_MASK(k_mbox,
103 			SYS_TRACK_LIST_PREPEND(_track_list_k_mbox, mbox));
104 }
105 
106 #ifdef CONFIG_PIPES
sys_track_k_pipe_init(struct k_pipe * pipe)107 void sys_track_k_pipe_init(struct k_pipe *pipe)
108 {
109 	SYS_PORT_TRACING_TYPE_MASK(k_pipe,
110 			SYS_TRACK_LIST_PREPEND(_track_list_k_pipe, pipe));
111 }
112 #endif
113 
sys_track_k_queue_init(struct k_queue * queue)114 void sys_track_k_queue_init(struct k_queue *queue)
115 {
116 	SYS_PORT_TRACING_TYPE_MASK(k_queue,
117 			SYS_TRACK_LIST_PREPEND(_track_list_k_queue, queue));
118 }
119 
120 #ifdef CONFIG_EVENTS
sys_track_k_event_init(struct k_event * event)121 void sys_track_k_event_init(struct k_event *event)
122 {
123 	SYS_PORT_TRACING_TYPE_MASK(k_event,
124 			SYS_TRACK_LIST_PREPEND(_track_list_k_event, event));
125 }
126 #endif
127 
128 #ifdef CONFIG_NETWORKING
sys_track_socket_init(int sock,int family,int type,int proto)129 void sys_track_socket_init(int sock, int family, int type, int proto)
130 {
131 	ARG_UNUSED(sock);
132 	ARG_UNUSED(family);
133 	ARG_UNUSED(type);
134 	ARG_UNUSED(proto);
135 }
136 #endif
137 
sys_track_static_init(void)138 static int sys_track_static_init(void)
139 {
140 
141 	SYS_PORT_TRACING_TYPE_MASK(k_timer,
142 			SYS_TRACK_STATIC_INIT(k_timer));
143 
144 	SYS_PORT_TRACING_TYPE_MASK(k_mem_slab,
145 			SYS_TRACK_STATIC_INIT(k_mem_slab, 0));
146 
147 	SYS_PORT_TRACING_TYPE_MASK(k_sem,
148 			SYS_TRACK_STATIC_INIT(k_sem, 0));
149 
150 	SYS_PORT_TRACING_TYPE_MASK(k_mutex,
151 			SYS_TRACK_STATIC_INIT(k_mutex, 0));
152 
153 	SYS_PORT_TRACING_TYPE_MASK(k_stack,
154 			SYS_TRACK_STATIC_INIT(k_stack));
155 
156 	SYS_PORT_TRACING_TYPE_MASK(k_msgq,
157 			SYS_TRACK_STATIC_INIT(k_msgq));
158 
159 	SYS_PORT_TRACING_TYPE_MASK(k_mbox,
160 			SYS_TRACK_STATIC_INIT(k_mbox));
161 
162 #ifdef CONFIG_PIPES
163 	SYS_PORT_TRACING_TYPE_MASK(k_pipe,
164 			SYS_TRACK_STATIC_INIT(k_pipe));
165 #endif
166 
167 	SYS_PORT_TRACING_TYPE_MASK(k_queue,
168 			SYS_TRACK_STATIC_INIT(k_queue));
169 
170 #ifdef CONFIG_EVENTS
171 	SYS_PORT_TRACING_TYPE_MASK(k_event,
172 			SYS_TRACK_STATIC_INIT(k_event));
173 #endif
174 
175 	return 0;
176 }
177 
178 SYS_INIT(sys_track_static_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
179