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