Fix: allow racy tracepoint string input from kernel and userspace
[lttng-modules.git] / lttng-events.h
1 #ifndef _LTTNG_EVENTS_H
2 #define _LTTNG_EVENTS_H
3
4 /*
5 * lttng-events.h
6 *
7 * Holds LTTng per-session event registry.
8 *
9 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; only
14 * version 2.1 of the License.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <linux/version.h>
27 #include <linux/list.h>
28 #include <linux/kprobes.h>
29 #include <linux/kref.h>
30 #include "wrapper/uuid.h"
31 #include "lttng-abi.h"
32 #include "lttng-abi-old.h"
33
34 #define lttng_is_signed_type(type) (((type)(-1)) < 0)
35
36 struct lttng_channel;
37 struct lttng_session;
38 struct lttng_metadata_cache;
39 struct lib_ring_buffer_ctx;
40 struct perf_event;
41 struct perf_event_attr;
42 struct lib_ring_buffer_config;
43
44 /* Type description */
45
46 /* Update the astract_types name table in lttng-types.c along with this enum */
47 enum abstract_types {
48 atype_integer,
49 atype_enum,
50 atype_array,
51 atype_sequence,
52 atype_string,
53 NR_ABSTRACT_TYPES,
54 };
55
56 /* Update the string_encodings name table in lttng-types.c along with this enum */
57 enum lttng_string_encodings {
58 lttng_encode_none = 0,
59 lttng_encode_UTF8 = 1,
60 lttng_encode_ASCII = 2,
61 NR_STRING_ENCODINGS,
62 };
63
64 enum channel_type {
65 PER_CPU_CHANNEL,
66 METADATA_CHANNEL,
67 };
68
69 struct lttng_enum_entry {
70 unsigned long long start, end; /* start and end are inclusive */
71 const char *string;
72 };
73
74 #define __type_integer(_type, _byte_order, _base, _encoding) \
75 { \
76 .atype = atype_integer, \
77 .u.basic.integer = \
78 { \
79 .size = sizeof(_type) * CHAR_BIT, \
80 .alignment = lttng_alignof(_type) * CHAR_BIT, \
81 .signedness = lttng_is_signed_type(_type), \
82 .reverse_byte_order = _byte_order != __BYTE_ORDER, \
83 .base = _base, \
84 .encoding = lttng_encode_##_encoding, \
85 }, \
86 } \
87
88 struct lttng_integer_type {
89 unsigned int size; /* in bits */
90 unsigned short alignment; /* in bits */
91 unsigned int signedness:1,
92 reverse_byte_order:1;
93 unsigned int base; /* 2, 8, 10, 16, for pretty print */
94 enum lttng_string_encodings encoding;
95 };
96
97 union _lttng_basic_type {
98 struct lttng_integer_type integer;
99 struct {
100 const char *name;
101 } enumeration;
102 struct {
103 enum lttng_string_encodings encoding;
104 } string;
105 };
106
107 struct lttng_basic_type {
108 enum abstract_types atype;
109 union {
110 union _lttng_basic_type basic;
111 } u;
112 };
113
114 struct lttng_type {
115 enum abstract_types atype;
116 union {
117 union _lttng_basic_type basic;
118 struct {
119 struct lttng_basic_type elem_type;
120 unsigned int length; /* num. elems. */
121 } array;
122 struct {
123 struct lttng_basic_type length_type;
124 struct lttng_basic_type elem_type;
125 } sequence;
126 } u;
127 };
128
129 struct lttng_enum {
130 const char *name;
131 struct lttng_type container_type;
132 const struct lttng_enum_entry *entries;
133 unsigned int len;
134 };
135
136 /* Event field description */
137
138 struct lttng_event_field {
139 const char *name;
140 struct lttng_type type;
141 };
142
143 /*
144 * We need to keep this perf counter field separately from struct
145 * lttng_ctx_field because cpu hotplug needs fixed-location addresses.
146 */
147 struct lttng_perf_counter_field {
148 struct notifier_block nb;
149 int hp_enable;
150 struct perf_event_attr *attr;
151 struct perf_event **e; /* per-cpu array */
152 };
153
154 struct lttng_ctx_field {
155 struct lttng_event_field event_field;
156 size_t (*get_size)(size_t offset);
157 void (*record)(struct lttng_ctx_field *field,
158 struct lib_ring_buffer_ctx *ctx,
159 struct lttng_channel *chan);
160 union {
161 struct lttng_perf_counter_field *perf_counter;
162 } u;
163 void (*destroy)(struct lttng_ctx_field *field);
164 };
165
166 struct lttng_ctx {
167 struct lttng_ctx_field *fields;
168 unsigned int nr_fields;
169 unsigned int allocated_fields;
170 };
171
172 struct lttng_event_desc {
173 const char *name; /* lttng-modules name */
174 const char *kname; /* Linux kernel name (tracepoints) */
175 void *probe_callback;
176 const struct lttng_event_ctx *ctx; /* context */
177 const struct lttng_event_field *fields; /* event payload */
178 unsigned int nr_fields;
179 struct module *owner;
180 };
181
182 struct lttng_probe_desc {
183 const struct lttng_event_desc **event_desc;
184 unsigned int nr_events;
185 struct list_head head; /* chain registered probes */
186 };
187
188 struct lttng_krp; /* Kretprobe handling */
189
190 /*
191 * lttng_event structure is referred to by the tracing fast path. It must be
192 * kept small.
193 */
194 struct lttng_event {
195 unsigned int id;
196 struct lttng_channel *chan;
197 int enabled;
198 const struct lttng_event_desc *desc;
199 void *filter;
200 struct lttng_ctx *ctx;
201 enum lttng_kernel_instrumentation instrumentation;
202 union {
203 struct {
204 struct kprobe kp;
205 char *symbol_name;
206 } kprobe;
207 struct {
208 struct lttng_krp *lttng_krp;
209 char *symbol_name;
210 } kretprobe;
211 struct {
212 char *symbol_name;
213 } ftrace;
214 } u;
215 struct list_head list; /* Event list */
216 unsigned int metadata_dumped:1;
217 };
218
219 struct lttng_channel_ops {
220 struct channel *(*channel_create)(const char *name,
221 struct lttng_channel *lttng_chan,
222 void *buf_addr,
223 size_t subbuf_size, size_t num_subbuf,
224 unsigned int switch_timer_interval,
225 unsigned int read_timer_interval);
226 void (*channel_destroy)(struct channel *chan);
227 struct lib_ring_buffer *(*buffer_read_open)(struct channel *chan);
228 int (*buffer_has_read_closed_stream)(struct channel *chan);
229 void (*buffer_read_close)(struct lib_ring_buffer *buf);
230 int (*event_reserve)(struct lib_ring_buffer_ctx *ctx,
231 uint32_t event_id);
232 void (*event_commit)(struct lib_ring_buffer_ctx *ctx);
233 void (*event_write)(struct lib_ring_buffer_ctx *ctx, const void *src,
234 size_t len);
235 void (*event_write_from_user)(struct lib_ring_buffer_ctx *ctx,
236 const void *src, size_t len);
237 void (*event_memset)(struct lib_ring_buffer_ctx *ctx,
238 int c, size_t len);
239 void (*event_strcpy)(struct lib_ring_buffer_ctx *ctx, const char *src,
240 size_t len);
241 void (*event_strcpy_from_user)(struct lib_ring_buffer_ctx *ctx,
242 const char __user *src, size_t len);
243 /*
244 * packet_avail_size returns the available size in the current
245 * packet. Note that the size returned is only a hint, since it
246 * may change due to concurrent writes.
247 */
248 size_t (*packet_avail_size)(struct channel *chan);
249 wait_queue_head_t *(*get_writer_buf_wait_queue)(struct channel *chan, int cpu);
250 wait_queue_head_t *(*get_hp_wait_queue)(struct channel *chan);
251 int (*is_finalized)(struct channel *chan);
252 int (*is_disabled)(struct channel *chan);
253 int (*timestamp_begin) (const struct lib_ring_buffer_config *config,
254 struct lib_ring_buffer *bufb,
255 uint64_t *timestamp_begin);
256 int (*timestamp_end) (const struct lib_ring_buffer_config *config,
257 struct lib_ring_buffer *bufb,
258 uint64_t *timestamp_end);
259 int (*events_discarded) (const struct lib_ring_buffer_config *config,
260 struct lib_ring_buffer *bufb,
261 uint64_t *events_discarded);
262 int (*content_size) (const struct lib_ring_buffer_config *config,
263 struct lib_ring_buffer *bufb,
264 uint64_t *content_size);
265 int (*packet_size) (const struct lib_ring_buffer_config *config,
266 struct lib_ring_buffer *bufb,
267 uint64_t *packet_size);
268 int (*stream_id) (const struct lib_ring_buffer_config *config,
269 struct lib_ring_buffer *bufb,
270 uint64_t *stream_id);
271 int (*current_timestamp) (const struct lib_ring_buffer_config *config,
272 struct lib_ring_buffer *bufb,
273 uint64_t *ts);
274 };
275
276 struct lttng_transport {
277 char *name;
278 struct module *owner;
279 struct list_head node;
280 struct lttng_channel_ops ops;
281 };
282
283 struct lttng_channel {
284 unsigned int id;
285 struct channel *chan; /* Channel buffers */
286 int enabled;
287 struct lttng_ctx *ctx;
288 /* Event ID management */
289 struct lttng_session *session;
290 struct file *file; /* File associated to channel */
291 unsigned int free_event_id; /* Next event ID to allocate */
292 struct list_head list; /* Channel list */
293 struct lttng_channel_ops *ops;
294 struct lttng_transport *transport;
295 struct lttng_event **sc_table; /* for syscall tracing */
296 struct lttng_event **compat_sc_table;
297 struct lttng_event *sc_unknown; /* for unknown syscalls */
298 struct lttng_event *sc_compat_unknown;
299 struct lttng_event *sc_exit; /* for syscall exit */
300 int header_type; /* 0: unset, 1: compact, 2: large */
301 enum channel_type channel_type;
302 unsigned int metadata_dumped:1;
303 };
304
305 struct lttng_metadata_stream {
306 void *priv; /* Ring buffer private data */
307 struct lttng_metadata_cache *metadata_cache;
308 unsigned int metadata_in; /* Bytes read from the cache */
309 unsigned int metadata_out; /* Bytes consumed from stream */
310 int finalized; /* Has channel been finalized */
311 wait_queue_head_t read_wait; /* Reader buffer-level wait queue */
312 struct list_head list; /* Stream list */
313 struct lttng_transport *transport;
314 };
315
316 struct lttng_session {
317 int active; /* Is trace session active ? */
318 int been_active; /* Has trace session been active ? */
319 struct file *file; /* File associated to session */
320 struct list_head chan; /* Channel list head */
321 struct list_head events; /* Event list head */
322 struct list_head list; /* Session list */
323 unsigned int free_chan_id; /* Next chan ID to allocate */
324 uuid_le uuid; /* Trace session unique ID */
325 struct lttng_metadata_cache *metadata_cache;
326 unsigned int metadata_dumped:1;
327 };
328
329 struct lttng_metadata_cache {
330 char *data; /* Metadata cache */
331 unsigned int cache_alloc; /* Metadata allocated size (bytes) */
332 unsigned int metadata_written; /* Number of bytes written in metadata cache */
333 struct kref refcount; /* Metadata cache usage */
334 struct list_head metadata_stream; /* Metadata stream list */
335 };
336
337 struct lttng_session *lttng_session_create(void);
338 int lttng_session_enable(struct lttng_session *session);
339 int lttng_session_disable(struct lttng_session *session);
340 void lttng_session_destroy(struct lttng_session *session);
341 void metadata_cache_destroy(struct kref *kref);
342
343 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
344 const char *transport_name,
345 void *buf_addr,
346 size_t subbuf_size, size_t num_subbuf,
347 unsigned int switch_timer_interval,
348 unsigned int read_timer_interval,
349 enum channel_type channel_type);
350 struct lttng_channel *lttng_global_channel_create(struct lttng_session *session,
351 int overwrite, void *buf_addr,
352 size_t subbuf_size, size_t num_subbuf,
353 unsigned int switch_timer_interval,
354 unsigned int read_timer_interval);
355
356 void lttng_metadata_channel_destroy(struct lttng_channel *chan);
357 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
358 struct lttng_kernel_event *event_param,
359 void *filter,
360 const struct lttng_event_desc *internal_desc);
361 struct lttng_event *lttng_event_compat_old_create(struct lttng_channel *chan,
362 struct lttng_kernel_old_event *old_event_param,
363 void *filter,
364 const struct lttng_event_desc *internal_desc);
365
366 int lttng_channel_enable(struct lttng_channel *channel);
367 int lttng_channel_disable(struct lttng_channel *channel);
368 int lttng_event_enable(struct lttng_event *event);
369 int lttng_event_disable(struct lttng_event *event);
370
371 void lttng_transport_register(struct lttng_transport *transport);
372 void lttng_transport_unregister(struct lttng_transport *transport);
373
374 void synchronize_trace(void);
375 int lttng_abi_init(void);
376 int lttng_abi_compat_old_init(void);
377 void lttng_abi_exit(void);
378 void lttng_abi_compat_old_exit(void);
379
380 int lttng_probe_register(struct lttng_probe_desc *desc);
381 void lttng_probe_unregister(struct lttng_probe_desc *desc);
382 const struct lttng_event_desc *lttng_event_get(const char *name);
383 void lttng_event_put(const struct lttng_event_desc *desc);
384 int lttng_probes_init(void);
385 void lttng_probes_exit(void);
386
387 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
388 struct channel *chan);
389
390 #if defined(CONFIG_HAVE_SYSCALL_TRACEPOINTS)
391 int lttng_syscalls_register(struct lttng_channel *chan, void *filter);
392 int lttng_syscalls_unregister(struct lttng_channel *chan);
393 #else
394 static inline int lttng_syscalls_register(struct lttng_channel *chan, void *filter)
395 {
396 return -ENOSYS;
397 }
398
399 static inline int lttng_syscalls_unregister(struct lttng_channel *chan)
400 {
401 return 0;
402 }
403 #endif
404
405 struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx);
406 int lttng_find_context(struct lttng_ctx *ctx, const char *name);
407 void lttng_remove_context_field(struct lttng_ctx **ctx,
408 struct lttng_ctx_field *field);
409 void lttng_destroy_context(struct lttng_ctx *ctx);
410 int lttng_add_pid_to_ctx(struct lttng_ctx **ctx);
411 int lttng_add_procname_to_ctx(struct lttng_ctx **ctx);
412 int lttng_add_prio_to_ctx(struct lttng_ctx **ctx);
413 int lttng_add_nice_to_ctx(struct lttng_ctx **ctx);
414 int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx);
415 int lttng_add_tid_to_ctx(struct lttng_ctx **ctx);
416 int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx);
417 int lttng_add_ppid_to_ctx(struct lttng_ctx **ctx);
418 int lttng_add_vppid_to_ctx(struct lttng_ctx **ctx);
419 int lttng_add_hostname_to_ctx(struct lttng_ctx **ctx);
420 #if defined(CONFIG_PERF_EVENTS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33))
421 int lttng_add_perf_counter_to_ctx(uint32_t type,
422 uint64_t config,
423 const char *name,
424 struct lttng_ctx **ctx);
425 #else
426 static inline
427 int lttng_add_perf_counter_to_ctx(uint32_t type,
428 uint64_t config,
429 const char *name,
430 struct lttng_ctx **ctx)
431 {
432 return -ENOSYS;
433 }
434 #endif
435
436 int lttng_logger_init(void);
437 void lttng_logger_exit(void);
438
439 extern int lttng_statedump_start(struct lttng_session *session);
440
441 #ifdef CONFIG_KPROBES
442 int lttng_kprobes_register(const char *name,
443 const char *symbol_name,
444 uint64_t offset,
445 uint64_t addr,
446 struct lttng_event *event);
447 void lttng_kprobes_unregister(struct lttng_event *event);
448 void lttng_kprobes_destroy_private(struct lttng_event *event);
449 #else
450 static inline
451 int lttng_kprobes_register(const char *name,
452 const char *symbol_name,
453 uint64_t offset,
454 uint64_t addr,
455 struct lttng_event *event)
456 {
457 return -ENOSYS;
458 }
459
460 static inline
461 void lttng_kprobes_unregister(struct lttng_event *event)
462 {
463 }
464
465 static inline
466 void lttng_kprobes_destroy_private(struct lttng_event *event)
467 {
468 }
469 #endif
470
471 #ifdef CONFIG_KRETPROBES
472 int lttng_kretprobes_register(const char *name,
473 const char *symbol_name,
474 uint64_t offset,
475 uint64_t addr,
476 struct lttng_event *event_entry,
477 struct lttng_event *event_exit);
478 void lttng_kretprobes_unregister(struct lttng_event *event);
479 void lttng_kretprobes_destroy_private(struct lttng_event *event);
480 #else
481 static inline
482 int lttng_kretprobes_register(const char *name,
483 const char *symbol_name,
484 uint64_t offset,
485 uint64_t addr,
486 struct lttng_event *event_entry,
487 struct lttng_event *event_exit)
488 {
489 return -ENOSYS;
490 }
491
492 static inline
493 void lttng_kretprobes_unregister(struct lttng_event *event)
494 {
495 }
496
497 static inline
498 void lttng_kretprobes_destroy_private(struct lttng_event *event)
499 {
500 }
501 #endif
502
503 #ifdef CONFIG_DYNAMIC_FTRACE
504 int lttng_ftrace_register(const char *name,
505 const char *symbol_name,
506 struct lttng_event *event);
507 void lttng_ftrace_unregister(struct lttng_event *event);
508 void lttng_ftrace_destroy_private(struct lttng_event *event);
509 #else
510 static inline
511 int lttng_ftrace_register(const char *name,
512 const char *symbol_name,
513 struct lttng_event *event)
514 {
515 return -ENOSYS;
516 }
517
518 static inline
519 void lttng_ftrace_unregister(struct lttng_event *event)
520 {
521 }
522
523 static inline
524 void lttng_ftrace_destroy_private(struct lttng_event *event)
525 {
526 }
527 #endif
528
529 int lttng_calibrate(struct lttng_kernel_calibrate *calibrate);
530
531 extern const struct file_operations lttng_tracepoint_list_fops;
532
533 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
534 #define TRACEPOINT_HAS_DATA_ARG
535 #endif
536
537 #endif /* _LTTNG_EVENTS_H */
This page took 0.040716 seconds and 5 git commands to generate.