| 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 | |
| 12 | #include <stdarg.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/limits.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/cache.h> |
| 17 | #include <linux/timex.h> |
| 18 | #include <linux/wait.h> |
| 19 | #include <asm/atomic.h> |
| 20 | #include <asm/local.h> |
| 21 | |
| 22 | #include "wrapper/trace-clock.h" |
| 23 | #include "ltt-tracer-core.h" |
| 24 | #include "ltt-events.h" |
| 25 | |
| 26 | /* Number of bytes to log with a read/write event */ |
| 27 | #define LTT_LOG_RW_SIZE 32L |
| 28 | |
| 29 | /* Maximum number of callbacks per marker */ |
| 30 | #define LTT_NR_CALLBACKS 10 |
| 31 | |
| 32 | struct ltt_serialize_closure; |
| 33 | |
| 34 | /* Serialization callback */ |
| 35 | typedef size_t (*ltt_serialize_cb)(struct lib_ring_buffer *buf, |
| 36 | size_t buf_offset, |
| 37 | struct ltt_serialize_closure *closure, |
| 38 | void *serialize_private, |
| 39 | unsigned int stack_pos_ctx, |
| 40 | int *largest_align, |
| 41 | const char *fmt, va_list *args); |
| 42 | |
| 43 | struct ltt_serialize_closure { |
| 44 | ltt_serialize_cb *callbacks; |
| 45 | long cb_args[LTT_NR_CALLBACKS]; |
| 46 | unsigned int cb_idx; |
| 47 | }; |
| 48 | |
| 49 | size_t ltt_serialize_data(struct lib_ring_buffer *buf, size_t buf_offset, |
| 50 | struct ltt_serialize_closure *closure, |
| 51 | void *serialize_private, unsigned int stack_pos_ctx, |
| 52 | int *largest_align, const char *fmt, va_list *args); |
| 53 | |
| 54 | enum ltt_channels { |
| 55 | LTT_CHANNEL_METADATA, |
| 56 | LTT_CHANNEL_FD_STATE, |
| 57 | LTT_CHANNEL_GLOBAL_STATE, |
| 58 | LTT_CHANNEL_IRQ_STATE, |
| 59 | LTT_CHANNEL_MODULE_STATE, |
| 60 | LTT_CHANNEL_NETIF_STATE, |
| 61 | LTT_CHANNEL_SOFTIRQ_STATE, |
| 62 | LTT_CHANNEL_SWAP_STATE, |
| 63 | LTT_CHANNEL_SYSCALL_STATE, |
| 64 | LTT_CHANNEL_TASK_STATE, |
| 65 | LTT_CHANNEL_VM_STATE, |
| 66 | LTT_CHANNEL_FS, |
| 67 | LTT_CHANNEL_INPUT, |
| 68 | LTT_CHANNEL_IPC, |
| 69 | LTT_CHANNEL_KERNEL, |
| 70 | LTT_CHANNEL_MM, |
| 71 | LTT_CHANNEL_RCU, |
| 72 | LTT_CHANNEL_DEFAULT, |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * Hardcoded event headers |
| 77 | * |
| 78 | * event header for a trace with active heartbeat : 27 bits timestamps |
| 79 | * |
| 80 | * headers are 32-bits aligned. In order to insure such alignment, a dynamic per |
| 81 | * trace alignment value must be done. |
| 82 | * |
| 83 | * Remember that the C compiler does align each member on the boundary |
| 84 | * equivalent to their own size. |
| 85 | * |
| 86 | * As relay subbuffers are aligned on pages, we are sure that they are 4 and 8 |
| 87 | * bytes aligned, so the buffer header and trace header are aligned. |
| 88 | * |
| 89 | * Event headers are aligned depending on the trace alignment option. |
| 90 | * |
| 91 | * Note using C structure bitfields for cross-endianness and portability |
| 92 | * concerns. |
| 93 | */ |
| 94 | |
| 95 | #define LTT_RESERVED_EVENTS 3 |
| 96 | #define LTT_EVENT_BITS 5 |
| 97 | #define LTT_FREE_EVENTS ((1 << LTT_EVENT_BITS) - LTT_RESERVED_EVENTS) |
| 98 | #define LTT_TSC_BITS 27 |
| 99 | #define LTT_TSC_MASK ((1 << LTT_TSC_BITS) - 1) |
| 100 | |
| 101 | struct event_header { |
| 102 | u32 id_time; /* 5 bits event id (MSB); 27 bits time (LSB) */ |
| 103 | }; |
| 104 | |
| 105 | /* Reservation flags */ |
| 106 | #define LTT_RFLAG_ID (1 << 0) |
| 107 | #define LTT_RFLAG_ID_SIZE (1 << 1) |
| 108 | #define LTT_RFLAG_ID_SIZE_TSC (1 << 2) |
| 109 | |
| 110 | #define LTT_MAX_SMALL_SIZE 0xFFFFU |
| 111 | |
| 112 | /* |
| 113 | * We use asm/timex.h : cpu_khz/HZ variable in here : we might have to deal |
| 114 | * specifically with CPU frequency scaling someday, so using an interpolation |
| 115 | * between the start and end of buffer values is not flexible enough. Using an |
| 116 | * immediate frequency value permits to calculate directly the times for parts |
| 117 | * of a buffer that would be before a frequency change. |
| 118 | * |
| 119 | * Keep the natural field alignment for _each field_ within this structure if |
| 120 | * you ever add/remove a field from this header. Packed attribute is not used |
| 121 | * because gcc generates poor code on at least powerpc and mips. Don't ever |
| 122 | * let gcc add padding between the structure elements. |
| 123 | */ |
| 124 | struct packet_header { |
| 125 | uint32_t magic; /* |
| 126 | * Trace magic number. |
| 127 | * contains endianness information. |
| 128 | */ |
| 129 | uint8_t trace_uuid[16]; |
| 130 | uint32_t stream_id; |
| 131 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ |
| 132 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ |
| 133 | uint32_t content_size; /* Size of data in subbuffer */ |
| 134 | uint32_t packet_size; /* Subbuffer size (include padding) */ |
| 135 | uint32_t events_lost; /* |
| 136 | * Events lost in this subbuffer since |
| 137 | * the beginning of the trace. |
| 138 | * (may overflow) |
| 139 | */ |
| 140 | /* TODO: move to metadata */ |
| 141 | #if 0 |
| 142 | uint8_t major_version; |
| 143 | uint8_t minor_version; |
| 144 | uint8_t arch_size; /* Architecture pointer size */ |
| 145 | uint8_t alignment; /* LTT data alignment */ |
| 146 | uint64_t start_time_sec; /* NTP-corrected start time */ |
| 147 | uint64_t start_time_usec; |
| 148 | uint64_t start_freq; /* |
| 149 | * Frequency at trace start, |
| 150 | * used all along the trace. |
| 151 | */ |
| 152 | uint32_t freq_scale; /* Frequency scaling (divisor) */ |
| 153 | #endif //0 |
| 154 | uint8_t header_end[0]; /* End of header */ |
| 155 | }; |
| 156 | |
| 157 | static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan) |
| 158 | { |
| 159 | return trace_clock_read64(); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * record_header_size - Calculate the header size and padding necessary. |
| 164 | * @config: ring buffer instance configuration |
| 165 | * @chan: channel |
| 166 | * @offset: offset in the write buffer |
| 167 | * @data_size: size of the payload |
| 168 | * @pre_header_padding: padding to add before the header (output) |
| 169 | * @rflags: reservation flags |
| 170 | * @ctx: reservation context |
| 171 | * |
| 172 | * Returns the event header size (including padding). |
| 173 | * |
| 174 | * Important note : |
| 175 | * The event header must be 32-bits. The total offset calculated here : |
| 176 | * |
| 177 | * Alignment of header struct on 32 bits (min arch size, header size) |
| 178 | * + sizeof(header struct) (32-bits) |
| 179 | * + (opt) u16 (ext. event id) |
| 180 | * + (opt) u16 (event_size) |
| 181 | * (if event_size == LTT_MAX_SMALL_SIZE, has ext. event size) |
| 182 | * + (opt) u32 (ext. event size) |
| 183 | * + (opt) u64 full TSC (aligned on min(64-bits, arch size)) |
| 184 | * |
| 185 | * The payload must itself determine its own alignment from the biggest type it |
| 186 | * contains. |
| 187 | */ |
| 188 | static __inline__ |
| 189 | unsigned char record_header_size(const struct lib_ring_buffer_config *config, |
| 190 | struct channel *chan, size_t offset, |
| 191 | size_t data_size, size_t *pre_header_padding, |
| 192 | unsigned int rflags, |
| 193 | struct lib_ring_buffer_ctx *ctx) |
| 194 | { |
| 195 | size_t orig_offset = offset; |
| 196 | size_t padding; |
| 197 | |
| 198 | BUILD_BUG_ON(sizeof(struct event_header) != sizeof(u32)); |
| 199 | |
| 200 | padding = lib_ring_buffer_align(offset, |
| 201 | sizeof(struct event_header)); |
| 202 | offset += padding; |
| 203 | offset += sizeof(struct event_header); |
| 204 | |
| 205 | if (unlikely(rflags)) { |
| 206 | switch (rflags) { |
| 207 | case LTT_RFLAG_ID_SIZE_TSC: |
| 208 | offset += sizeof(u16) + sizeof(u16); |
| 209 | if (data_size >= LTT_MAX_SMALL_SIZE) |
| 210 | offset += sizeof(u32); |
| 211 | offset += lib_ring_buffer_align(offset, sizeof(u64)); |
| 212 | offset += sizeof(u64); |
| 213 | break; |
| 214 | case LTT_RFLAG_ID_SIZE: |
| 215 | offset += sizeof(u16) + sizeof(u16); |
| 216 | if (data_size >= LTT_MAX_SMALL_SIZE) |
| 217 | offset += sizeof(u32); |
| 218 | break; |
| 219 | case LTT_RFLAG_ID: |
| 220 | offset += sizeof(u16); |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | *pre_header_padding = padding; |
| 226 | return offset - orig_offset; |
| 227 | } |
| 228 | |
| 229 | #include "wrapper/ringbuffer/api.h" |
| 230 | |
| 231 | extern |
| 232 | void ltt_write_event_header_slow(const struct lib_ring_buffer_config *config, |
| 233 | struct lib_ring_buffer_ctx *ctx, |
| 234 | u16 eID, u32 event_size); |
| 235 | |
| 236 | /* |
| 237 | * ltt_write_event_header |
| 238 | * |
| 239 | * Writes the event header to the offset (already aligned on 32-bits). |
| 240 | * |
| 241 | * @config: ring buffer instance configuration |
| 242 | * @ctx: reservation context |
| 243 | * @eID : event ID |
| 244 | * @event_size : size of the event, excluding the event header. |
| 245 | */ |
| 246 | static __inline__ |
| 247 | void ltt_write_event_header(const struct lib_ring_buffer_config *config, |
| 248 | struct lib_ring_buffer_ctx *ctx, |
| 249 | u16 eID, u32 event_size) |
| 250 | { |
| 251 | struct event_header header; |
| 252 | |
| 253 | if (unlikely(ctx->rflags)) |
| 254 | goto slow_path; |
| 255 | |
| 256 | header.id_time = eID << LTT_TSC_BITS; |
| 257 | header.id_time |= (u32)ctx->tsc & LTT_TSC_MASK; |
| 258 | lib_ring_buffer_write(config, ctx, &header, sizeof(header)); |
| 259 | |
| 260 | slow_path: |
| 261 | ltt_write_event_header_slow(config, ctx, eID, event_size); |
| 262 | } |
| 263 | |
| 264 | /* Tracer properties */ |
| 265 | #define CTF_MAGIC_NUMBER 0xC1FC1FC1 |
| 266 | #define LTT_TRACER_VERSION_MAJOR 3 |
| 267 | #define LTT_TRACER_VERSION_MINOR 0 |
| 268 | |
| 269 | /** |
| 270 | * ltt_write_trace_header - Write trace header |
| 271 | * @priv: Private data (struct trace) |
| 272 | * @header: Memory address where the information must be written to |
| 273 | */ |
| 274 | static __inline__ |
| 275 | void write_trace_header(const struct lib_ring_buffer_config *config, |
| 276 | struct packet_header *header) |
| 277 | { |
| 278 | header->magic = CTF_MAGIC_NUMBER; |
| 279 | #if 0 |
| 280 | /* TODO: move start time to metadata */ |
| 281 | header->major_version = LTT_TRACER_VERSION_MAJOR; |
| 282 | header->minor_version = LTT_TRACER_VERSION_MINOR; |
| 283 | header->arch_size = sizeof(void *); |
| 284 | header->alignment = lib_ring_buffer_get_alignment(config); |
| 285 | header->start_time_sec = ltt_chan->session->start_time.tv_sec; |
| 286 | header->start_time_usec = ltt_chan->session->start_time.tv_usec; |
| 287 | header->start_freq = ltt_chan->session->start_freq; |
| 288 | header->freq_scale = ltt_chan->session->freq_scale; |
| 289 | #endif //0 |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Size reserved for high priority events (interrupts, NMI, BH) at the end of a |
| 294 | * nearly full buffer. User space won't use this last amount of space when in |
| 295 | * blocking mode. This space also includes the event header that would be |
| 296 | * written by this user space event. |
| 297 | */ |
| 298 | #define LTT_RESERVE_CRITICAL 4096 |
| 299 | |
| 300 | /* Register and unregister function pointers */ |
| 301 | |
| 302 | enum ltt_module_function { |
| 303 | LTT_FUNCTION_RUN_FILTER, |
| 304 | LTT_FUNCTION_FILTER_CONTROL, |
| 305 | LTT_FUNCTION_STATEDUMP |
| 306 | }; |
| 307 | |
| 308 | extern int ltt_module_register(enum ltt_module_function name, void *function, |
| 309 | struct module *owner); |
| 310 | extern void ltt_module_unregister(enum ltt_module_function name); |
| 311 | |
| 312 | /* Exported control function */ |
| 313 | |
| 314 | void ltt_core_register(int (*function)(u8, void *)); |
| 315 | |
| 316 | void ltt_core_unregister(void); |
| 317 | |
| 318 | extern |
| 319 | void ltt_statedump_register_kprobes_dump(void (*callback)(void *call_data)); |
| 320 | extern |
| 321 | void ltt_statedump_unregister_kprobes_dump(void (*callback)(void *call_data)); |
| 322 | |
| 323 | extern void ltt_dump_softirq_vec(void *call_data); |
| 324 | |
| 325 | #ifdef CONFIG_HAVE_LTT_DUMP_TABLES |
| 326 | extern void ltt_dump_sys_call_table(void *call_data); |
| 327 | extern void ltt_dump_idt_table(void *call_data); |
| 328 | #else |
| 329 | static inline void ltt_dump_sys_call_table(void *call_data) |
| 330 | { |
| 331 | } |
| 332 | |
| 333 | static inline void ltt_dump_idt_table(void *call_data) |
| 334 | { |
| 335 | } |
| 336 | #endif |
| 337 | |
| 338 | #endif /* _LTT_TRACER_H */ |