Commit | Line | Data |
---|---|---|
9dad1eb8 PMF |
1 | /* |
2 | * Copyright (C) 2005,2006,2008 Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca) | |
e4621c7f | 3 | * Copyright (C) 2009 Pierre-Marc Fournier |
9dad1eb8 PMF |
4 | * |
5 | * This contains the definitions for the Linux Trace Toolkit tracer. | |
e4621c7f PMF |
6 | * |
7 | * Ported to userspace by Pierre-Marc Fournier. | |
8 | * | |
8fc2d8db PMF |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
9dad1eb8 PMF |
22 | */ |
23 | ||
24 | #ifndef _LTT_TRACER_H | |
25 | #define _LTT_TRACER_H | |
26 | ||
b6bf28ec | 27 | #include <sys/types.h> |
9dad1eb8 | 28 | #include <stdarg.h> |
518d7abb PMF |
29 | #include <ust/marker.h> |
30 | #include <ust/probe.h> | |
31 | #include <ust/core.h> | |
b6bf28ec | 32 | #include "channels.h" |
bb07823d | 33 | #include "tracercore.h" |
b73a4c47 | 34 | #include "tracerconst.h" |
8c36d1ee | 35 | #include "buffers.h" |
9dad1eb8 PMF |
36 | |
37 | /* Number of bytes to log with a read/write event */ | |
38 | #define LTT_LOG_RW_SIZE 32L | |
39 | ||
40 | /* Interval (in jiffies) at which the LTT per-CPU timer fires */ | |
41 | #define LTT_PERCPU_TIMER_INTERVAL 1 | |
42 | ||
43 | #ifndef LTT_ARCH_TYPE | |
44 | #define LTT_ARCH_TYPE LTT_ARCH_TYPE_UNDEFINED | |
45 | #endif | |
46 | ||
47 | #ifndef LTT_ARCH_VARIANT | |
48 | #define LTT_ARCH_VARIANT LTT_ARCH_VARIANT_NONE | |
49 | #endif | |
50 | ||
51 | struct ltt_active_marker; | |
52 | ||
53 | /* Maximum number of callbacks per marker */ | |
54 | #define LTT_NR_CALLBACKS 10 | |
55 | ||
56 | struct ltt_serialize_closure; | |
57 | struct ltt_probe_private_data; | |
58 | ||
9dad1eb8 PMF |
59 | struct ltt_serialize_closure { |
60 | ltt_serialize_cb *callbacks; | |
61 | long cb_args[LTT_NR_CALLBACKS]; | |
62 | unsigned int cb_idx; | |
63 | }; | |
64 | ||
b5b073e2 | 65 | extern size_t ltt_serialize_data(struct ust_buffer *buf, size_t buf_offset, |
9dad1eb8 PMF |
66 | struct ltt_serialize_closure *closure, |
67 | void *serialize_private, | |
bb3132c8 MD |
68 | unsigned int stack_pos_ctx, int *largest_align, |
69 | const char *fmt, va_list *args); | |
9dad1eb8 | 70 | |
9dad1eb8 | 71 | struct ltt_probe_private_data { |
b73a4c47 | 72 | struct ust_trace *trace; /* |
9dad1eb8 PMF |
73 | * Target trace, for metadata |
74 | * or statedump. | |
75 | */ | |
76 | ltt_serialize_cb serializer; /* | |
77 | * Serialization function override. | |
78 | */ | |
79 | void *serialize_private; /* | |
80 | * Private data for serialization | |
81 | * functions. | |
82 | */ | |
83 | }; | |
84 | ||
85 | enum ltt_channels { | |
86 | LTT_CHANNEL_METADATA, | |
b6bf28ec | 87 | LTT_CHANNEL_UST, |
9dad1eb8 PMF |
88 | }; |
89 | ||
223f2e7c AH |
90 | struct chan_info_struct { |
91 | const char *name; | |
92 | unsigned int def_subbufsize; | |
93 | unsigned int def_subbufcount; | |
94 | }; | |
95 | ||
9dad1eb8 PMF |
96 | struct ltt_active_marker { |
97 | struct list_head node; /* active markers list */ | |
98 | const char *channel; | |
99 | const char *name; | |
100 | const char *format; | |
101 | struct ltt_available_probe *probe; | |
102 | }; | |
103 | ||
b6bf28ec | 104 | struct marker; //ust// |
9dad1eb8 | 105 | extern void ltt_vtrace(const struct marker *mdata, void *probe_data, |
75667d04 | 106 | struct registers *regs, void *call_data, const char *fmt, va_list *args); |
9dad1eb8 | 107 | extern void ltt_trace(const struct marker *mdata, void *probe_data, |
75667d04 | 108 | struct registers *regs, void *call_data, const char *fmt, ...); |
9dad1eb8 PMF |
109 | |
110 | /* | |
111 | * Unique ID assigned to each registered probe. | |
112 | */ | |
113 | enum marker_id { | |
114 | MARKER_ID_SET_MARKER_ID = 0, /* Static IDs available (range 0-7) */ | |
115 | MARKER_ID_SET_MARKER_FORMAT, | |
116 | MARKER_ID_COMPACT, /* Compact IDs (range: 8-127) */ | |
117 | MARKER_ID_DYNAMIC, /* Dynamic IDs (range: 128-65535) */ | |
118 | }; | |
119 | ||
120 | /* static ids 0-1 reserved for internal use. */ | |
121 | #define MARKER_CORE_IDS 2 | |
a2fd50ef | 122 | static __inline__ enum marker_id marker_id_type(uint16_t id) |
9dad1eb8 PMF |
123 | { |
124 | if (id < MARKER_CORE_IDS) | |
125 | return (enum marker_id)id; | |
126 | else | |
127 | return MARKER_ID_DYNAMIC; | |
128 | } | |
129 | ||
9dad1eb8 PMF |
130 | struct user_dbg_data { |
131 | unsigned long avail_size; | |
132 | unsigned long write; | |
133 | unsigned long read; | |
134 | }; | |
135 | ||
136 | struct ltt_trace_ops { | |
137 | /* First 32 bytes cache-hot cacheline */ | |
b5b073e2 | 138 | void (*wakeup_channel) (struct ust_channel *channel); |
b73a4c47 | 139 | int (*user_blocking) (struct ust_trace *trace, |
bb07823d PMF |
140 | unsigned int index, size_t data_size, |
141 | struct user_dbg_data *dbg); | |
b5b073e2 | 142 | /* End of first 32 bytes cacheline */ |
b73a4c47 PMF |
143 | int (*create_dirs) (struct ust_trace *new_trace); |
144 | void (*remove_dirs) (struct ust_trace *new_trace); | |
b5b073e2 | 145 | int (*create_channel) (const char *trace_name, |
b73a4c47 | 146 | struct ust_trace *trace, |
b5b073e2 PMF |
147 | const char *channel_name, |
148 | struct ust_channel *channel, | |
149 | unsigned int subbuf_size, | |
150 | unsigned int n_subbufs, int overwrite); | |
151 | void (*finish_channel) (struct ust_channel *channel); | |
152 | void (*remove_channel) (struct ust_channel *channel); | |
b73a4c47 | 153 | void (*user_errors) (struct ust_trace *trace, |
b5b073e2 | 154 | unsigned int index, size_t data_size, |
204141ee | 155 | struct user_dbg_data *dbg, unsigned int cpu); |
b73a4c47 | 156 | }; |
9dad1eb8 PMF |
157 | |
158 | struct ltt_transport { | |
159 | char *name; | |
160 | struct module *owner; | |
161 | struct list_head node; | |
162 | struct ltt_trace_ops ops; | |
163 | }; | |
164 | ||
165 | enum trace_mode { LTT_TRACE_NORMAL, LTT_TRACE_FLIGHT, LTT_TRACE_HYBRID }; | |
166 | ||
167 | #define CHANNEL_FLAG_ENABLE (1U<<0) | |
168 | #define CHANNEL_FLAG_OVERWRITE (1U<<1) | |
169 | ||
170 | /* Per-trace information - each trace/flight recorder represented by one */ | |
b73a4c47 | 171 | struct ust_trace { |
9dad1eb8 PMF |
172 | /* First 32 bytes cache-hot cacheline */ |
173 | struct list_head list; | |
174 | struct ltt_trace_ops *ops; | |
175 | int active; | |
176 | /* Second 32 bytes cache-hot cacheline */ | |
b5b073e2 | 177 | struct ust_channel *channels; |
9dad1eb8 PMF |
178 | unsigned int nr_channels; |
179 | u32 freq_scale; | |
180 | u64 start_freq; | |
181 | u64 start_tsc; | |
182 | unsigned long long start_monotonic; | |
183 | struct timeval start_time; | |
184 | struct ltt_channel_setting *settings; | |
185 | struct { | |
186 | struct dentry *trace_root; | |
187 | } dentry; | |
9dad1eb8 PMF |
188 | struct kref kref; /* Each channel has a kref of the trace struct */ |
189 | struct ltt_transport *transport; | |
190 | struct kref ltt_transport_kref; | |
9dad1eb8 PMF |
191 | char trace_name[NAME_MAX]; |
192 | } ____cacheline_aligned; | |
193 | ||
9dad1eb8 PMF |
194 | /* |
195 | * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal | |
196 | * specifically with CPU frequency scaling someday, so using an interpolation | |
197 | * between the start and end of buffer values is not flexible enough. Using an | |
198 | * immediate frequency value permits to calculate directly the times for parts | |
199 | * of a buffer that would be before a frequency change. | |
200 | * | |
201 | * Keep the natural field alignment for _each field_ within this structure if | |
202 | * you ever add/remove a field from this header. Packed attribute is not used | |
203 | * because gcc generates poor code on at least powerpc and mips. Don't ever | |
204 | * let gcc add padding between the structure elements. | |
205 | */ | |
206 | struct ltt_subbuffer_header { | |
207 | uint64_t cycle_count_begin; /* Cycle count at subbuffer start */ | |
208 | uint64_t cycle_count_end; /* Cycle count at subbuffer end */ | |
209 | uint32_t magic_number; /* | |
210 | * Trace magic number. | |
211 | * contains endianness information. | |
212 | */ | |
213 | uint8_t major_version; | |
214 | uint8_t minor_version; | |
215 | uint8_t arch_size; /* Architecture pointer size */ | |
216 | uint8_t alignment; /* LTT data alignment */ | |
217 | uint64_t start_time_sec; /* NTP-corrected start time */ | |
218 | uint64_t start_time_usec; | |
219 | uint64_t start_freq; /* | |
220 | * Frequency at trace start, | |
221 | * used all along the trace. | |
222 | */ | |
223 | uint32_t freq_scale; /* Frequency scaling (divisor) */ | |
8c36d1ee PMF |
224 | uint32_t data_size; /* Size of data in subbuffer */ |
225 | uint32_t sb_size; /* Subbuffer size (including padding) */ | |
9dad1eb8 PMF |
226 | uint32_t events_lost; /* |
227 | * Events lost in this subbuffer since | |
228 | * the beginning of the trace. | |
229 | * (may overflow) | |
230 | */ | |
231 | uint32_t subbuf_corrupt; /* | |
232 | * Corrupted (lost) subbuffers since | |
233 | * the begginig of the trace. | |
234 | * (may overflow) | |
235 | */ | |
236 | uint8_t header_end[0]; /* End of header */ | |
237 | }; | |
238 | ||
239 | /** | |
240 | * ltt_subbuffer_header_size - called on buffer-switch to a new sub-buffer | |
241 | * | |
242 | * Return header size without padding after the structure. Don't use packed | |
243 | * structure because gcc generates inefficient code on some architectures | |
244 | * (powerpc, mips..) | |
245 | */ | |
a2fd50ef | 246 | static __inline__ size_t ltt_subbuffer_header_size(void) |
9dad1eb8 PMF |
247 | { |
248 | return offsetof(struct ltt_subbuffer_header, header_end); | |
249 | } | |
250 | ||
12e81b07 | 251 | extern size_t ltt_write_event_header_slow(struct ust_channel *channel, |
b73a4c47 PMF |
252 | struct ust_buffer *buf, long buf_offset, |
253 | u16 eID, u32 event_size, | |
254 | u64 tsc, unsigned int rflags); | |
255 | ||
9dad1eb8 PMF |
256 | |
257 | /* | |
258 | * ltt_write_event_header | |
259 | * | |
260 | * Writes the event header to the offset (already aligned on 32-bits). | |
261 | * | |
262 | * @trace : trace to write to. | |
263 | * @channel : pointer to the channel structure.. | |
264 | * @buf : buffer to write to. | |
265 | * @buf_offset : buffer offset to write to (aligned on 32 bits). | |
266 | * @eID : event ID | |
267 | * @event_size : size of the event, excluding the event header. | |
268 | * @tsc : time stamp counter. | |
269 | * @rflags : reservation flags. | |
270 | * | |
271 | * returns : offset where the event data must be written. | |
272 | */ | |
12e81b07 | 273 | static __inline__ size_t ltt_write_event_header(struct ust_channel *chan, |
b5b073e2 | 274 | struct ust_buffer *buf, long buf_offset, |
b73a4c47 | 275 | u16 eID, u32 event_size, |
9dad1eb8 PMF |
276 | u64 tsc, unsigned int rflags) |
277 | { | |
278 | struct ltt_event_header header; | |
9dad1eb8 | 279 | |
b73a4c47 PMF |
280 | if (unlikely(rflags)) |
281 | goto slow_path; | |
9dad1eb8 | 282 | |
b73a4c47 | 283 | header.id_time = eID << LTT_TSC_BITS; |
8c36d1ee PMF |
284 | header.id_time |= (u32)tsc & LTT_TSC_MASK; |
285 | ust_buffers_write(buf, buf_offset, &header, sizeof(header)); | |
286 | buf_offset += sizeof(header); | |
9dad1eb8 | 287 | |
b73a4c47 | 288 | return buf_offset; |
9dad1eb8 | 289 | |
b73a4c47 | 290 | slow_path: |
12e81b07 | 291 | return ltt_write_event_header_slow(chan, buf, buf_offset, |
b73a4c47 PMF |
292 | eID, event_size, tsc, rflags); |
293 | } | |
9dad1eb8 PMF |
294 | |
295 | /* | |
296 | * Control channels : | |
297 | * control/metadata | |
298 | * control/interrupts | |
299 | * control/... | |
300 | * | |
301 | * cpu channel : | |
302 | * cpu | |
303 | */ | |
9dad1eb8 PMF |
304 | |
305 | #define LTT_METADATA_CHANNEL "metadata_state" | |
b6bf28ec | 306 | #define LTT_UST_CHANNEL "ust" |
9dad1eb8 PMF |
307 | |
308 | #define LTT_FLIGHT_PREFIX "flight-" | |
309 | ||
310 | /* Tracer properties */ | |
32072f29 PMF |
311 | //#define LTT_DEFAULT_SUBBUF_SIZE_LOW 134217728 |
312 | #define LTT_DEFAULT_SUBBUF_SIZE_LOW 65536 | |
313 | //#define LTT_DEFAULT_SUBBUF_SIZE_LOW 4096 | |
9dad1eb8 | 314 | #define LTT_DEFAULT_N_SUBBUFS_LOW 2 |
32072f29 PMF |
315 | //#define LTT_DEFAULT_SUBBUF_SIZE_MED 134217728 |
316 | #define LTT_DEFAULT_SUBBUF_SIZE_MED 262144 | |
317 | //#define LTT_DEFAULT_SUBBUF_SIZE_MED 4096 | |
9dad1eb8 | 318 | #define LTT_DEFAULT_N_SUBBUFS_MED 2 |
32072f29 PMF |
319 | //#define LTT_DEFAULT_SUBBUF_SIZE_HIGH 134217728 |
320 | #define LTT_DEFAULT_SUBBUF_SIZE_HIGH 1048576 | |
321 | //#define LTT_DEFAULT_SUBBUF_SIZE_HIGH 4096 | |
9dad1eb8 PMF |
322 | #define LTT_DEFAULT_N_SUBBUFS_HIGH 2 |
323 | #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED | |
324 | #define LTT_TRACER_VERSION_MAJOR 2 | |
31607b38 | 325 | #define LTT_TRACER_VERSION_MINOR 6 |
9dad1eb8 | 326 | |
b73a4c47 PMF |
327 | /** |
328 | * ust_write_trace_header - Write trace header | |
329 | * @trace: Trace information | |
330 | * @header: Memory address where the information must be written to | |
331 | */ | |
332 | static __inline__ void ltt_write_trace_header(struct ust_trace *trace, | |
333 | struct ltt_subbuffer_header *header) | |
334 | { | |
335 | header->magic_number = LTT_TRACER_MAGIC_NUMBER; | |
336 | header->major_version = LTT_TRACER_VERSION_MAJOR; | |
337 | header->minor_version = LTT_TRACER_VERSION_MINOR; | |
338 | header->arch_size = sizeof(void *); | |
339 | header->alignment = ltt_get_alignment(); | |
340 | header->start_time_sec = trace->start_time.tv_sec; | |
341 | header->start_time_usec = trace->start_time.tv_usec; | |
342 | header->start_freq = trace->start_freq; | |
343 | header->freq_scale = trace->freq_scale; | |
344 | } | |
345 | ||
12e81b07 PMF |
346 | static __inline__ int ust_get_cpu(void) |
347 | { | |
348 | #ifndef UST_VALGRIND | |
349 | return sched_getcpu(); | |
350 | #else | |
351 | /* Valgrind does not support the sched_getcpu() vsyscall. | |
352 | * It causes it to detect a segfault in the program and stop it. | |
353 | * So if we want to check libust with valgrind, we have to refrain | |
354 | * from using this call. TODO: it would probably be better to return | |
355 | * other values too, to better test it. | |
356 | */ | |
357 | return 0; | |
358 | #endif | |
359 | } | |
360 | ||
b73a4c47 | 361 | |
9dad1eb8 PMF |
362 | /* |
363 | * Size reserved for high priority events (interrupts, NMI, BH) at the end of a | |
364 | * nearly full buffer. User space won't use this last amount of space when in | |
365 | * blocking mode. This space also includes the event header that would be | |
366 | * written by this user space event. | |
367 | */ | |
368 | #define LTT_RESERVE_CRITICAL 4096 | |
369 | ||
370 | /* Register and unregister function pointers */ | |
371 | ||
372 | enum ltt_module_function { | |
373 | LTT_FUNCTION_RUN_FILTER, | |
374 | LTT_FUNCTION_FILTER_CONTROL, | |
375 | LTT_FUNCTION_STATEDUMP | |
376 | }; | |
377 | ||
064842c6 PMF |
378 | extern void ltt_transport_register(struct ltt_transport *transport); |
379 | extern void ltt_transport_unregister(struct ltt_transport *transport); | |
9dad1eb8 PMF |
380 | |
381 | /* Exported control function */ | |
382 | ||
9dad1eb8 PMF |
383 | union ltt_control_args { |
384 | struct { | |
385 | enum trace_mode mode; | |
386 | unsigned int subbuf_size_low; | |
387 | unsigned int n_subbufs_low; | |
388 | unsigned int subbuf_size_med; | |
389 | unsigned int n_subbufs_med; | |
390 | unsigned int subbuf_size_high; | |
391 | unsigned int n_subbufs_high; | |
392 | } new_trace; | |
393 | }; | |
394 | ||
064842c6 PMF |
395 | extern int _ltt_trace_setup(const char *trace_name); |
396 | extern int ltt_trace_setup(const char *trace_name); | |
b73a4c47 | 397 | extern struct ust_trace *_ltt_trace_find_setup(const char *trace_name); |
064842c6 PMF |
398 | extern int ltt_trace_set_type(const char *trace_name, const char *trace_type); |
399 | extern int ltt_trace_set_channel_subbufsize(const char *trace_name, | |
9dad1eb8 | 400 | const char *channel_name, unsigned int size); |
064842c6 | 401 | extern int ltt_trace_set_channel_subbufcount(const char *trace_name, |
9dad1eb8 | 402 | const char *channel_name, unsigned int cnt); |
064842c6 | 403 | extern int ltt_trace_set_channel_enable(const char *trace_name, |
9dad1eb8 | 404 | const char *channel_name, unsigned int enable); |
064842c6 | 405 | extern int ltt_trace_set_channel_overwrite(const char *trace_name, |
9dad1eb8 | 406 | const char *channel_name, unsigned int overwrite); |
064842c6 | 407 | extern int ltt_trace_alloc(const char *trace_name); |
31d392f1 | 408 | extern int ltt_trace_destroy(const char *trace_name, int drop); |
064842c6 PMF |
409 | extern int ltt_trace_start(const char *trace_name); |
410 | extern int ltt_trace_stop(const char *trace_name); | |
9dad1eb8 | 411 | |
9dad1eb8 PMF |
412 | enum ltt_filter_control_msg { |
413 | LTT_FILTER_DEFAULT_ACCEPT, | |
414 | LTT_FILTER_DEFAULT_REJECT | |
415 | }; | |
416 | ||
417 | extern int ltt_filter_control(enum ltt_filter_control_msg msg, | |
418 | const char *trace_name); | |
419 | ||
420 | extern struct dentry *get_filter_root(void); | |
421 | ||
b73a4c47 | 422 | extern void ltt_write_trace_header(struct ust_trace *trace, |
9dad1eb8 | 423 | struct ltt_subbuffer_header *header); |
b5b073e2 | 424 | extern void ltt_buffer_destroy(struct ust_channel *ltt_chan); |
9dad1eb8 | 425 | |
064842c6 | 426 | extern void ltt_core_register(int (*function)(u8, void *)); |
9dad1eb8 | 427 | |
064842c6 | 428 | extern void ltt_core_unregister(void); |
9dad1eb8 | 429 | |
064842c6 PMF |
430 | extern void ltt_release_trace(struct kref *kref); |
431 | extern void ltt_release_transport(struct kref *kref); | |
9dad1eb8 | 432 | |
b73a4c47 | 433 | extern void ltt_dump_marker_state(struct ust_trace *trace); |
9dad1eb8 | 434 | |
064842c6 PMF |
435 | extern void ltt_lock_traces(void); |
436 | extern void ltt_unlock_traces(void); | |
9dad1eb8 | 437 | |
b73a4c47 | 438 | extern struct ust_trace *_ltt_trace_find(const char *trace_name); |
d0b5f2b9 | 439 | |
9dad1eb8 | 440 | #endif /* _LTT_TRACER_H */ |