Commit | Line | Data |
---|---|---|
2dccf128 MD |
1 | /* |
2 | * ltt-context.c | |
3 | * | |
4 | * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * LTTng trace/channel/event context management. | |
17baffe2 MD |
7 | * |
8 | * Dual LGPL v2.1/GPL v2 license. | |
2dccf128 MD |
9 | */ |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/list.h> | |
13 | #include <linux/mutex.h> | |
14 | #include <linux/slab.h> | |
15 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ | |
16 | #include "ltt-events.h" | |
17 | #include "ltt-tracer.h" | |
18 | ||
44252f0f MD |
19 | int lttng_find_context(struct lttng_ctx *ctx, const char *name) |
20 | { | |
21 | unsigned int i; | |
22 | ||
23 | for (i = 0; i < ctx->nr_fields; i++) { | |
4b58a8e4 MD |
24 | /* Skip allocated (but non-initialized) contexts */ |
25 | if (!ctx->fields[i].event_field.name) | |
26 | continue; | |
44252f0f MD |
27 | if (!strcmp(ctx->fields[i].event_field.name, name)) |
28 | return 1; | |
29 | } | |
30 | return 0; | |
31 | } | |
32 | EXPORT_SYMBOL_GPL(lttng_find_context); | |
33 | ||
2001023e MD |
34 | /* |
35 | * Note: as we append context information, the pointer location may change. | |
36 | */ | |
2dccf128 MD |
37 | struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p) |
38 | { | |
39 | struct lttng_ctx_field *field; | |
40 | struct lttng_ctx *ctx; | |
41 | ||
42 | if (!*ctx_p) { | |
43 | *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL); | |
44 | if (!*ctx_p) | |
45 | return NULL; | |
46 | } | |
47 | ctx = *ctx_p; | |
48 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { | |
49 | struct lttng_ctx_field *new_fields; | |
50 | ||
0f034e0f | 51 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
2dccf128 MD |
52 | new_fields = kzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL); |
53 | if (!new_fields) | |
54 | return NULL; | |
55 | if (ctx->fields) | |
77aefe99 | 56 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
2dccf128 MD |
57 | kfree(ctx->fields); |
58 | ctx->fields = new_fields; | |
59 | } | |
60 | field = &ctx->fields[ctx->nr_fields]; | |
61 | ctx->nr_fields++; | |
62 | return field; | |
63 | } | |
64 | EXPORT_SYMBOL_GPL(lttng_append_context); | |
65 | ||
4cae220c MD |
66 | /* |
67 | * Remove last context field. | |
68 | */ | |
8289661d MD |
69 | void lttng_remove_context_field(struct lttng_ctx **ctx_p, |
70 | struct lttng_ctx_field *field) | |
71 | { | |
72 | struct lttng_ctx *ctx; | |
73 | ||
74 | ctx = *ctx_p; | |
75 | ctx->nr_fields--; | |
4cae220c | 76 | WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field); |
8289661d MD |
77 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
78 | } | |
79 | EXPORT_SYMBOL_GPL(lttng_remove_context_field); | |
80 | ||
2dccf128 MD |
81 | void lttng_destroy_context(struct lttng_ctx *ctx) |
82 | { | |
83 | int i; | |
84 | ||
8070f5c0 MD |
85 | if (!ctx) |
86 | return; | |
9e7e4892 MD |
87 | for (i = 0; i < ctx->nr_fields; i++) { |
88 | if (ctx->fields[i].destroy) | |
89 | ctx->fields[i].destroy(&ctx->fields[i]); | |
90 | } | |
2dccf128 MD |
91 | kfree(ctx->fields); |
92 | kfree(ctx); | |
93 | } |