1 | ; |
---|
2 | ; RTEMS Trace Linker Trace Buffer |
---|
3 | ; |
---|
4 | ; Copyright 2015 Chris Johns <chrisj@rtems.org> |
---|
5 | ; |
---|
6 | |
---|
7 | ; |
---|
8 | ; A trace buffer generator buffers records to a buffer that can be extracted |
---|
9 | ; latter. |
---|
10 | ; |
---|
11 | [trace-buffer-generator] |
---|
12 | headers = trace-buffer-generator-headers |
---|
13 | code-blocks = trace-buffer-tracers |
---|
14 | lock-local = " rtems_interrupt_lock_context lcontext;" |
---|
15 | lock-acquire = " rtems_interrupt_lock_acquire(&__rtld_tbg_lock, &lcontext);" |
---|
16 | lock-release = " rtems_interrupt_lock_release(&__rtld_tbg_lock, &lcontext);" |
---|
17 | entry-trace = "__rtld_tbg_buffer_entry(&in, @FUNC_INDEX@, RTLD_TBG_REC_OVERHEAD + @FUNC_DATA_ENTRY_SIZE@);" |
---|
18 | entry-alloc = "in = __rtld_tbg_buffer_alloc(@FUNC_INDEX@, RTLD_TBG_REC_OVERHEAD + @FUNC_DATA_ENTRY_SIZE@);" |
---|
19 | arg-trace = "__rtld_tbg_buffer_arg(&in, @ARG_SIZE@, (void*) &@ARG_LABEL@);" |
---|
20 | exit-trace = "__rtld_tbg_buffer_exit(&in, @FUNC_INDEX@, RTLD_TBG_REC_OVERHEAD + @FUNC_DATA_RET_SIZE@);" |
---|
21 | exit-alloc = "in = __rtld_tbg_buffer_alloc(@FUNC_INDEX@, RTLD_TBG_REC_OVERHEAD + @FUNC_DATA_RET_SIZE@);" |
---|
22 | ret-trace = "__rtld_tbg_buffer_ret(in, @RET_SIZE@, (void*) &@RET_LABEL@);" |
---|
23 | buffer-local = " uint8_t* in;" |
---|
24 | |
---|
25 | [trace-buffer-generator-headers] |
---|
26 | header = "#include <stdint.h>" |
---|
27 | header = "#include <rtems.h>" |
---|
28 | header = "#include <rtems/rtems/tasksimpl.h>" |
---|
29 | header = "#include <rtems/score/threadimpl.h>" |
---|
30 | |
---|
31 | [trace-buffer-tracers] |
---|
32 | code = <<<CODE |
---|
33 | /* |
---|
34 | * Mode bits. |
---|
35 | */ |
---|
36 | #define RTLD_TRACE_BUFFER_VERSION 0 /* data format version, lower 8bits */ |
---|
37 | #if RTLD_TRACE_BUFFER_TIMESTAMP |
---|
38 | #undef RTLD_TRACE_BUFFER_TIMESTAMP |
---|
39 | #define RTLD_TRACE_BUFFER_TIMESTAMP (1 << 8) |
---|
40 | #else |
---|
41 | #define RTLD_TRACE_BUFFER_TIMESTAMP 0 |
---|
42 | #endif |
---|
43 | #if defined(RTLD_TRACE_BUFFER_THREAD) |
---|
44 | #undef RTLD_TRACE_BUFFER_THREAD |
---|
45 | #define RTLD_TRACE_BUFFER_THREAD (1 << 9) |
---|
46 | #else |
---|
47 | #define RTLD_TRACE_BUFFER_THREAD 0 |
---|
48 | #endif |
---|
49 | #define RTLD_TRACE_BUFFER_MODE RTLD_TRACE_BUFFER_VERSION | \ |
---|
50 | RTLD_TRACE_BUFFER_TIMESTAMP | \ |
---|
51 | RTLD_TRACE_BUFFER_THREAD |
---|
52 | /* |
---|
53 | * The number of word in the buffer. |
---|
54 | */ |
---|
55 | #define RTLD_TRACE_BUFFER_WORDS (RTLD_TRACE_BUFFER_SIZE / sizeof(uint32_t)) |
---|
56 | /* |
---|
57 | * We log the header record and then a 64bit timestamp. |
---|
58 | */ |
---|
59 | #define RTLD_TBG_REC_OVERHEAD (6 * sizeof(uint32_t)) |
---|
60 | /* |
---|
61 | * Symbols are public to allow external access to the buffers. |
---|
62 | */ |
---|
63 | const bool __rtld_tbg_present = true; |
---|
64 | const uint32_t __rtld_tbg_mode = RTLD_TRACE_BUFFER_MODE; |
---|
65 | const uint32_t __rtld_tbg_buffer_size = RTLD_TRACE_BUFFER_WORDS; |
---|
66 | uint32_t __rtld_tbg_buffer[RTLD_TRACE_BUFFER_WORDS]; |
---|
67 | volatile uint32_t __rtld_tbg_buffer_in; |
---|
68 | volatile bool __rtld_tbg_finished; |
---|
69 | volatile bool __rtld_tbg_triggered; |
---|
70 | /* |
---|
71 | * Lock the access. |
---|
72 | */ |
---|
73 | RTEMS_INTERRUPT_LOCK_DEFINE(static, __rtld_tbg_lock, "rtld-trace-buffer"); |
---|
74 | |
---|
75 | static inline uint32_t __rtld_tbg_in_irq(void) |
---|
76 | { |
---|
77 | return rtems_interrupt_is_in_progress() ? (1 << 31) : 0; |
---|
78 | } |
---|
79 | |
---|
80 | static inline uint32_t __rtld_tbg_executing_id(void) |
---|
81 | { |
---|
82 | return _Thread_Get_executing()->Object.id; |
---|
83 | } |
---|
84 | |
---|
85 | static inline uint32_t __rtld_tbg_executing_status(void) |
---|
86 | { |
---|
87 | /* @fixme Add the current CPU for SMP. */ |
---|
88 | Thread_Control* tc = _Thread_Get_executing(); |
---|
89 | return (_Thread_Get_priority(tc) << 8) | tc->Real_priority.priority; |
---|
90 | } |
---|
91 | |
---|
92 | static inline uint32_t __rtld_tbg_executing_state(void) |
---|
93 | { |
---|
94 | return _Thread_Get_executing()->current_state; |
---|
95 | } |
---|
96 | |
---|
97 | static inline bool __rtld_tbg_is_enabled(const uint32_t index) |
---|
98 | { |
---|
99 | return (__rtld_trace_enables[index / 32] & (1 << (index & (32 - 1)))) != 0 ? true : false; |
---|
100 | } |
---|
101 | |
---|
102 | static inline bool __rtld_tbg_has_triggered(const uint32_t index) |
---|
103 | { |
---|
104 | if (!__rtld_tbg_triggered) |
---|
105 | __rtld_tbg_triggered = |
---|
106 | (__rtld_trace_triggers[index / 32] & (1 << (index & (32 - 1)))) != 0 ? true : false; |
---|
107 | return __rtld_tbg_triggered; |
---|
108 | } |
---|
109 | |
---|
110 | static inline uint8_t* __rtld_tbg_buffer_alloc(const uint32_t index, const uint32_t size) |
---|
111 | { |
---|
112 | uint8_t* in = NULL; |
---|
113 | if (!__rtld_tbg_finished && __rtld_tbg_has_triggered(index) && __rtld_tbg_is_enabled(index)) |
---|
114 | { |
---|
115 | const uint32_t slots = ((size - 1) / sizeof(uint32_t)) + 1; |
---|
116 | if (__rtld_tbg_buffer_in >= ((RTLD_TRACE_BUFFER_WORDS - slots))) |
---|
117 | { |
---|
118 | __rtld_tbg_finished = true; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | in = (uint8_t*) &__rtld_tbg_buffer[__rtld_tbg_buffer_in]; |
---|
123 | __rtld_tbg_buffer_in += slots; |
---|
124 | } |
---|
125 | } |
---|
126 | return in; |
---|
127 | } |
---|
128 | |
---|
129 | static inline void __rtld_tbg_buffer_entry(uint8_t** in, uint32_t func_index, uint32_t size) |
---|
130 | { |
---|
131 | if (*in) |
---|
132 | { |
---|
133 | uint32_t* in32 = (uint32_t*) *in; |
---|
134 | uint64_t now = rtems_clock_get_uptime_nanoseconds(); |
---|
135 | *in32++ = func_index | (size << 16) | __rtld_tbg_in_irq(); |
---|
136 | *in32++ = __rtld_tbg_executing_id(); |
---|
137 | *in32++ = __rtld_tbg_executing_status(); |
---|
138 | *in32++ = __rtld_tbg_executing_state(); |
---|
139 | *in32++ = now >> 32; |
---|
140 | *in32 = now; |
---|
141 | *in += sizeof(func_index) + (3 * sizeof(uint32_t)) + sizeof(uint64_t); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | static inline void __rtld_tbg_buffer_arg(uint8_t** in, int arg_size, void* arg) |
---|
146 | { |
---|
147 | if (*in) |
---|
148 | { |
---|
149 | memcpy(*in, arg, arg_size); |
---|
150 | *in += arg_size; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | static inline void __rtld_tbg_buffer_exit(uint8_t** in, uint32_t func_index, uint32_t size) |
---|
155 | { |
---|
156 | if (*in) |
---|
157 | { |
---|
158 | uint32_t* in32 = (uint32_t*) *in; |
---|
159 | uint64_t now = rtems_clock_get_uptime_nanoseconds(); |
---|
160 | *in32++ = (1 << 30) | func_index | (size << 16) | __rtld_tbg_in_irq(); |
---|
161 | *in32++ = __rtld_tbg_executing_id(); |
---|
162 | *in32++ = __rtld_tbg_executing_status(); |
---|
163 | *in32++ = __rtld_tbg_executing_state(); |
---|
164 | *in32++ = now >> 32; |
---|
165 | *in32 = now; |
---|
166 | *in += sizeof(func_index) + (3 * sizeof(uint32_t)) + sizeof(uint64_t); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | static inline void __rtld_tbg_buffer_ret(uint8_t* in, int ret_size, void* ret) |
---|
171 | { |
---|
172 | if (in) |
---|
173 | { |
---|
174 | memcpy(in, ret, ret_size); |
---|
175 | } |
---|
176 | } |
---|
177 | CODE |
---|