LTTng modules now builds again
[lttng-modules.git] / ltt-tracer.h
CommitLineData
1c8284eb
MD
1/*
2 * Copyright (C) 2005,2006,2008 Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
3 *
4 * This contains the definitions for the Linux Trace Toolkit tracer.
5 *
6 * Dual LGPL v2.1/GPL v2 license.
7 */
8
9#ifndef _LTT_TRACER_H
10#define _LTT_TRACER_H
11
7514523f
MD
12#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
13/* Align data on its natural alignment */
14#define RING_BUFFER_ALIGN
15#endif
16
17#include <linux/ringbuffer/config.h>
18
1c8284eb
MD
19#include <stdarg.h>
20#include <linux/types.h>
21#include <linux/limits.h>
22#include <linux/list.h>
23#include <linux/cache.h>
24#include <linux/kernel.h>
25#include <linux/timex.h>
26#include <linux/wait.h>
1c8284eb 27#include <linux/trace-clock.h>
1c8284eb
MD
28#include <asm/atomic.h>
29#include <asm/local.h>
30
31#include "ltt-tracer-core.h"
1c25284c 32#include "ltt-events.h"
1c8284eb
MD
33
34/* Number of bytes to log with a read/write event */
35#define LTT_LOG_RW_SIZE 32L
36
1c8284eb
MD
37/* Maximum number of callbacks per marker */
38#define LTT_NR_CALLBACKS 10
39
11b5a3c2
MD
40struct ltt_serialize_closure;
41
42/* Serialization callback */
43typedef size_t (*ltt_serialize_cb)(struct lib_ring_buffer *buf,
44 size_t buf_offset,
45 struct ltt_serialize_closure *closure,
46 void *serialize_private,
47 unsigned int stack_pos_ctx,
48 int *largest_align,
49 const char *fmt, va_list *args);
50
1c8284eb
MD
51struct ltt_serialize_closure {
52 ltt_serialize_cb *callbacks;
53 long cb_args[LTT_NR_CALLBACKS];
54 unsigned int cb_idx;
55};
56
11b5a3c2 57size_t ltt_serialize_data(struct lib_ring_buffer *buf, size_t buf_offset,
1c8284eb
MD
58 struct ltt_serialize_closure *closure,
59 void *serialize_private, unsigned int stack_pos_ctx,
60 int *largest_align, const char *fmt, va_list *args);
61
1c8284eb
MD
62enum ltt_channels {
63 LTT_CHANNEL_METADATA,
64 LTT_CHANNEL_FD_STATE,
65 LTT_CHANNEL_GLOBAL_STATE,
66 LTT_CHANNEL_IRQ_STATE,
67 LTT_CHANNEL_MODULE_STATE,
68 LTT_CHANNEL_NETIF_STATE,
69 LTT_CHANNEL_SOFTIRQ_STATE,
70 LTT_CHANNEL_SWAP_STATE,
71 LTT_CHANNEL_SYSCALL_STATE,
72 LTT_CHANNEL_TASK_STATE,
73 LTT_CHANNEL_VM_STATE,
74 LTT_CHANNEL_FS,
75 LTT_CHANNEL_INPUT,
76 LTT_CHANNEL_IPC,
77 LTT_CHANNEL_KERNEL,
78 LTT_CHANNEL_MM,
79 LTT_CHANNEL_RCU,
80 LTT_CHANNEL_DEFAULT,
81};
82
11b5a3c2
MD
83#if 0
84size_t ltt_serialize_printf(struct lib_ring_buffer *buf, unsigned long buf_offset,
1c8284eb
MD
85 size_t *msg_size, char *output, size_t outlen,
86 const char *fmt);
87
88/*
89 * Unique ID assigned to each registered probe.
90 */
91enum marker_id {
92 MARKER_ID_SET_MARKER_ID = 0, /* Static IDs available (range 0-7) */
93 MARKER_ID_SET_MARKER_FORMAT,
94 MARKER_ID_COMPACT, /* Compact IDs (range: 8-127) */
95 MARKER_ID_DYNAMIC, /* Dynamic IDs (range: 128-65535) */
96};
97
98/* static ids 0-1 reserved for internal use. */
99#define MARKER_CORE_IDS 2
100static __inline__ enum marker_id marker_id_type(uint16_t id)
101{
102 if (id < MARKER_CORE_IDS)
103 return (enum marker_id)id;
104 else
105 return MARKER_ID_DYNAMIC;
106}
107
108struct user_dbg_data {
109 unsigned long avail_size;
110 unsigned long write;
111 unsigned long read;
112};
113
1c8284eb
MD
114enum trace_mode { LTT_TRACE_NORMAL, LTT_TRACE_FLIGHT, LTT_TRACE_HYBRID };
115
116#define CHANNEL_FLAG_ENABLE (1U<<0)
117#define CHANNEL_FLAG_OVERWRITE (1U<<1)
11b5a3c2 118#endif //0
1c8284eb 119
c0e31d2e 120#if 0
1c8284eb
MD
121/* Per-trace information - each trace/flight recorder represented by one */
122struct ltt_trace {
123 /* First 32 bytes cache-hot cacheline */
124 struct list_head list;
7514523f 125 struct ltt_chan **channels;
1c8284eb
MD
126 unsigned int nr_channels;
127 int active;
128 /* Second 32 bytes cache-hot cacheline */
129 struct ltt_trace_ops *ops;
130 u32 freq_scale;
131 u64 start_freq;
132 u64 start_tsc;
133 unsigned long long start_monotonic;
134 struct timeval start_time;
135 struct ltt_channel_setting *settings;
136 struct {
137 struct dentry *trace_root;
138 struct dentry *ascii_root;
139 } dentry;
140 struct kref kref; /* Each channel has a kref of the trace struct */
141 struct ltt_transport *transport;
142 struct kref ltt_transport_kref;
143 wait_queue_head_t kref_wq; /* Place for ltt_trace_destroy to sleep */
144 char trace_name[NAME_MAX];
145} ____cacheline_aligned;
c0e31d2e 146#endif //0
1c8284eb 147
7514523f
MD
148/*
149 * Hardcoded event headers
1c8284eb
MD
150 *
151 * event header for a trace with active heartbeat : 27 bits timestamps
152 *
153 * headers are 32-bits aligned. In order to insure such alignment, a dynamic per
154 * trace alignment value must be done.
155 *
156 * Remember that the C compiler does align each member on the boundary
157 * equivalent to their own size.
158 *
159 * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8
160 * bytes aligned, so the buffer header and trace header are aligned.
161 *
162 * Event headers are aligned depending on the trace alignment option.
163 *
164 * Note using C structure bitfields for cross-endianness and portability
165 * concerns.
166 */
167
168#define LTT_RESERVED_EVENTS 3
169#define LTT_EVENT_BITS 5
170#define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS)
171#define LTT_TSC_BITS 27
172#define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1)
173
7514523f 174struct event_header {
1c8284eb
MD
175 u32 id_time; /* 5 bits event id (MSB); 27 bits time (LSB) */
176};
177
178/* Reservation flags */
179#define LTT_RFLAG_ID (1 << 0)
180#define LTT_RFLAG_ID_SIZE (1 << 1)
181#define LTT_RFLAG_ID_SIZE_TSC (1 << 2)
182
183#define LTT_MAX_SMALL_SIZE 0xFFFFU
184
185/*
186 * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal
187 * specifically with CPU frequency scaling someday, so using an interpolation
188 * between the start and end of buffer values is not flexible enough. Using an
189 * immediate frequency value permits to calculate directly the times for parts
190 * of a buffer that would be before a frequency change.
191 *
192 * Keep the natural field alignment for _each field_ within this structure if
193 * you ever add/remove a field from this header. Packed attribute is not used
194 * because gcc generates poor code on at least powerpc and mips. Don't ever
195 * let gcc add padding between the structure elements.
196 */
1c25284c
MD
197struct packet_header {
198 uint32_t magic; /*
1c8284eb
MD
199 * Trace magic number.
200 * contains endianness information.
201 */
1c25284c
MD
202 uint8_t trace_uuid[16];
203 uint32_t stream_id;
204 uint64_t timestamp_begin; /* Cycle count at subbuffer start */
205 uint64_t timestamp_end; /* Cycle count at subbuffer end */
206 uint32_t content_size; /* Size of data in subbuffer */
207 uint32_t packet_size; /* Subbuffer size (include padding) */
208 uint32_t events_lost; /*
209 * Events lost in this subbuffer since
210 * the beginning of the trace.
211 * (may overflow)
212 */
213 /* TODO: move to metadata */
214#if 0
1c8284eb
MD
215 uint8_t major_version;
216 uint8_t minor_version;
217 uint8_t arch_size; /* Architecture pointer size */
218 uint8_t alignment; /* LTT data alignment */
219 uint64_t start_time_sec; /* NTP-corrected start time */
220 uint64_t start_time_usec;
221 uint64_t start_freq; /*
222 * Frequency at trace start,
223 * used all along the trace.
224 */
225 uint32_t freq_scale; /* Frequency scaling (divisor) */
1c25284c 226#endif //0
1c8284eb
MD
227 uint8_t header_end[0]; /* End of header */
228};
229
7514523f 230static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan)
1c8284eb 231{
7514523f 232 return trace_clock_read64();
1c8284eb
MD
233}
234
235/*
7514523f
MD
236 * record_header_size - Calculate the header size and padding necessary.
237 * @config: ring buffer instance configuration
238 * @chan: channel
239 * @offset: offset in the write buffer
240 * @data_size: size of the payload
241 * @pre_header_padding: padding to add before the header (output)
242 * @rflags: reservation flags
243 * @ctx: reservation context
1c8284eb 244 *
7514523f 245 * Returns the event header size (including padding).
1c8284eb
MD
246 *
247 * Important note :
248 * The event header must be 32-bits. The total offset calculated here :
249 *
250 * Alignment of header struct on 32 bits (min arch size, header size)
251 * + sizeof(header struct) (32-bits)
252 * + (opt) u16 (ext. event id)
253 * + (opt) u16 (event_size)
254 * (if event_size == LTT_MAX_SMALL_SIZE, has ext. event size)
255 * + (opt) u32 (ext. event size)
256 * + (opt) u64 full TSC (aligned on min(64-bits, arch size))
257 *
258 * The payload must itself determine its own alignment from the biggest type it
259 * contains.
7514523f 260 */
1c8284eb 261static __inline__
7514523f
MD
262unsigned char record_header_size(const struct lib_ring_buffer_config *config,
263 struct channel *chan, size_t offset,
264 size_t data_size, size_t *pre_header_padding,
265 unsigned int rflags,
266 struct lib_ring_buffer_ctx *ctx)
1c8284eb
MD
267{
268 size_t orig_offset = offset;
269 size_t padding;
270
7514523f 271 BUILD_BUG_ON(sizeof(struct event_header) != sizeof(u32));
1c8284eb 272
7514523f
MD
273 padding = lib_ring_buffer_align(config, offset,
274 sizeof(struct event_header));
1c8284eb 275 offset += padding;
7514523f 276 offset += sizeof(struct event_header);
1c8284eb
MD
277
278 if (unlikely(rflags)) {
279 switch (rflags) {
280 case LTT_RFLAG_ID_SIZE_TSC:
281 offset += sizeof(u16) + sizeof(u16);
282 if (data_size >= LTT_MAX_SMALL_SIZE)
283 offset += sizeof(u32);
284 offset += ltt_align(offset, sizeof(u64));
285 offset += sizeof(u64);
286 break;
287 case LTT_RFLAG_ID_SIZE:
288 offset += sizeof(u16) + sizeof(u16);
289 if (data_size >= LTT_MAX_SMALL_SIZE)
290 offset += sizeof(u32);
291 break;
292 case LTT_RFLAG_ID:
293 offset += sizeof(u16);
294 break;
295 }
296 }
297
7514523f 298 *pre_header_padding = padding;
1c8284eb
MD
299 return offset - orig_offset;
300}
301
7514523f
MD
302#include <linux/ringbuffer/api.h>
303
1c8284eb 304extern
1c25284c
MD
305void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config,
306 struct lib_ring_buffer_ctx *ctx,
307 u16 eID, u32 event_size);
1c8284eb
MD
308
309/*
310 * ltt_write_event_header
311 *
312 * Writes the event header to the offset (already aligned on 32-bits).
313 *
7514523f
MD
314 * @config: ring buffer instance configuration
315 * @ctx: reservation context
1c8284eb
MD
316 * @eID : event ID
317 * @event_size : size of the event, excluding the event header.
1c8284eb
MD
318 */
319static __inline__
7514523f
MD
320void ltt_write_event_header(const struct lib_ring_buffer_config *config,
321 struct lib_ring_buffer_ctx *ctx,
322 u16 eID, u32 event_size)
1c8284eb 323{
7514523f 324 struct event_header header;
1c8284eb 325
7514523f 326 if (unlikely(ctx->rflags))
1c8284eb
MD
327 goto slow_path;
328
329 header.id_time = eID << LTT_TSC_BITS;
7514523f
MD
330 header.id_time |= (u32)ctx->tsc & LTT_TSC_MASK;
331 lib_ring_buffer_write(config, ctx, &header, sizeof(header));
1c8284eb
MD
332
333slow_path:
7514523f 334 ltt_write_event_header_slow(config, ctx, eID, event_size);
1c8284eb
MD
335}
336
7514523f 337#if 0
1c8284eb
MD
338/*
339 * ltt_read_event_header
340 * buf_offset must aligned on 32 bits
341 */
342static __inline__
343size_t ltt_read_event_header(struct ltt_chanbuf_alloc *bufa, long buf_offset,
344 u64 *tsc, u32 *event_size, u16 *eID,
345 unsigned int *rflags)
346{
347 struct ltt_event_header header;
348 u16 small_size;
349
350 ltt_relay_read(bufa, buf_offset, &header, sizeof(header));
351 buf_offset += sizeof(header);
352
353 *event_size = INT_MAX;
354 *eID = header.id_time >> LTT_TSC_BITS;
355 *tsc = header.id_time & LTT_TSC_MASK;
356
357 switch (*eID) {
358 case 29:
359 *rflags = LTT_RFLAG_ID_SIZE_TSC;
360 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
361 buf_offset += sizeof(u16);
362 ltt_relay_read(bufa, buf_offset, &small_size, sizeof(u16));
363 buf_offset += sizeof(u16);
364 if (small_size == LTT_MAX_SMALL_SIZE) {
365 ltt_relay_read(bufa, buf_offset, event_size,
366 sizeof(u32));
367 buf_offset += sizeof(u32);
368 } else
369 *event_size = small_size;
370 buf_offset += ltt_align(buf_offset, sizeof(u64));
371 ltt_relay_read(bufa, buf_offset, tsc, sizeof(u64));
372 buf_offset += sizeof(u64);
373 break;
374 case 30:
375 *rflags = LTT_RFLAG_ID_SIZE;
376 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
377 buf_offset += sizeof(u16);
378 ltt_relay_read(bufa, buf_offset, &small_size, sizeof(u16));
379 buf_offset += sizeof(u16);
380 if (small_size == LTT_MAX_SMALL_SIZE) {
381 ltt_relay_read(bufa, buf_offset, event_size,
382 sizeof(u32));
383 buf_offset += sizeof(u32);
384 } else
385 *event_size = small_size;
386 break;
387 case 31:
388 *rflags = LTT_RFLAG_ID;
389 ltt_relay_read(bufa, buf_offset, eID, sizeof(u16));
390 buf_offset += sizeof(u16);
391 break;
392 default:
393 *rflags = 0;
394 break;
395 }
396
397 return buf_offset;
398}
7514523f 399#endif //0
1c8284eb 400
1c8284eb 401/* Tracer properties */
1c25284c
MD
402#define CTF_MAGIC_NUMBER 0xC1FC1FC1
403#define LTT_TRACER_VERSION_MAJOR 3
404#define LTT_TRACER_VERSION_MINOR 0
1c8284eb
MD
405
406/**
407 * ltt_write_trace_header - Write trace header
7514523f 408 * @priv: Private data (struct trace)
1c8284eb
MD
409 * @header: Memory address where the information must be written to
410 */
411static __inline__
1c25284c
MD
412void write_trace_header(const struct lib_ring_buffer_config *config,
413 struct packet_header *header)
1c8284eb 414{
1c25284c
MD
415 header->magic = CTF_MAGIC_NUMBER;
416#if 0
417 /* TODO: move start time to metadata */
1c8284eb
MD
418 header->major_version = LTT_TRACER_VERSION_MAJOR;
419 header->minor_version = LTT_TRACER_VERSION_MINOR;
420 header->arch_size = sizeof(void *);
1c25284c
MD
421 header->alignment = lib_ring_buffer_get_alignment(config);
422 header->start_time_sec = ltt_chan->session->start_time.tv_sec;
423 header->start_time_usec = ltt_chan->session->start_time.tv_usec;
424 header->start_freq = ltt_chan->session->start_freq;
425 header->freq_scale = ltt_chan->session->freq_scale;
426#endif //0
1c8284eb
MD
427}
428
429/*
430 * Size reserved for high priority events (interrupts, NMI, BH) at the end of a
431 * nearly full buffer. User space won't use this last amount of space when in
432 * blocking mode. This space also includes the event header that would be
433 * written by this user space event.
434 */
435#define LTT_RESERVE_CRITICAL 4096
436
437/* Register and unregister function pointers */
438
439enum ltt_module_function {
440 LTT_FUNCTION_RUN_FILTER,
441 LTT_FUNCTION_FILTER_CONTROL,
442 LTT_FUNCTION_STATEDUMP
443};
444
445extern int ltt_module_register(enum ltt_module_function name, void *function,
446 struct module *owner);
447extern void ltt_module_unregister(enum ltt_module_function name);
448
1c8284eb
MD
449/* Exported control function */
450
1c8284eb
MD
451void ltt_core_register(int (*function)(u8, void *));
452
453void ltt_core_unregister(void);
454
1c8284eb
MD
455extern
456void ltt_statedump_register_kprobes_dump(void (*callback)(void *call_data));
457extern
458void ltt_statedump_unregister_kprobes_dump(void (*callback)(void *call_data));
459
460extern void ltt_dump_softirq_vec(void *call_data);
461
f277b4c1 462#ifdef CONFIG_HAVE_LTT_DUMP_TABLES
1c8284eb
MD
463extern void ltt_dump_sys_call_table(void *call_data);
464extern void ltt_dump_idt_table(void *call_data);
f277b4c1
MD
465#else
466static inline void ltt_dump_sys_call_table(void *call_data)
467{
468}
469
470static inline void ltt_dump_idt_table(void *call_data)
471{
472}
473#endif
1c8284eb 474
1c8284eb 475#endif /* _LTT_TRACER_H */
This page took 0.041204 seconds and 4 git commands to generate.