Commit | Line | Data |
---|---|---|
4847e9bb | 1 | /* |
e92f3e28 | 2 | * lttng-context-procname.c |
4847e9bb MD |
3 | * |
4 | * LTTng UST procname context. | |
5 | * | |
e92f3e28 MD |
6 | * Copyright (C) 2009-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
4847e9bb MD |
21 | */ |
22 | ||
4318ae1b MD |
23 | #include <lttng/ust-events.h> |
24 | #include <lttng/ust-tracer.h> | |
25 | #include <lttng/ringbuffer-config.h> | |
4847e9bb | 26 | #include <assert.h> |
08114193 | 27 | #include "compat.h" |
4847e9bb MD |
28 | |
29 | /* | |
30 | * We cache the result to ensure we don't trigger a system call for | |
31 | * each event. | |
32 | * Upon exec, procname changes, but exec takes care of throwing away | |
33 | * this cached version. | |
34 | */ | |
35 | static char cached_procname[17]; | |
36 | ||
37 | static inline | |
38 | char *wrapper_getprocname(void) | |
39 | { | |
b5a3dfa5 | 40 | if (caa_unlikely(!cached_procname[0])) { |
08114193 MD |
41 | lttng_ust_getprocname(cached_procname); |
42 | cached_procname[LTTNG_UST_PROCNAME_LEN - 1] = '\0'; | |
4847e9bb MD |
43 | } |
44 | return cached_procname; | |
45 | } | |
46 | ||
47 | void lttng_context_procname_reset(void) | |
48 | { | |
49 | cached_procname[0] = '\0'; | |
50 | } | |
51 | ||
52 | static | |
53 | size_t procname_get_size(size_t offset) | |
54 | { | |
55 | size_t size = 0; | |
56 | ||
08114193 | 57 | size += LTTNG_UST_PROCNAME_LEN; |
4847e9bb MD |
58 | return size; |
59 | } | |
60 | ||
61 | static | |
62 | void procname_record(struct lttng_ctx_field *field, | |
4cfec15c | 63 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
4847e9bb MD |
64 | struct ltt_channel *chan) |
65 | { | |
66 | char *procname; | |
67 | ||
68 | procname = wrapper_getprocname(); | |
08114193 | 69 | chan->ops->event_write(ctx, procname, LTTNG_UST_PROCNAME_LEN); |
4847e9bb MD |
70 | } |
71 | ||
72 | int lttng_add_procname_to_ctx(struct lttng_ctx **ctx) | |
73 | { | |
74 | struct lttng_ctx_field *field; | |
75 | ||
76 | field = lttng_append_context(ctx); | |
77 | if (!field) | |
78 | return -ENOMEM; | |
79 | if (lttng_find_context(*ctx, "procname")) { | |
80 | lttng_remove_context_field(ctx, field); | |
81 | return -EEXIST; | |
82 | } | |
83 | field->event_field.name = "procname"; | |
84 | field->event_field.type.atype = atype_array; | |
85 | field->event_field.type.u.array.elem_type.atype = atype_integer; | |
86 | field->event_field.type.u.array.elem_type.u.basic.integer.size = sizeof(char) * CHAR_BIT; | |
87 | field->event_field.type.u.array.elem_type.u.basic.integer.alignment = lttng_alignof(char) * CHAR_BIT; | |
88 | field->event_field.type.u.array.elem_type.u.basic.integer.signedness = lttng_is_signed_type(char); | |
89 | field->event_field.type.u.array.elem_type.u.basic.integer.reverse_byte_order = 0; | |
90 | field->event_field.type.u.array.elem_type.u.basic.integer.base = 10; | |
91 | field->event_field.type.u.array.elem_type.u.basic.integer.encoding = lttng_encode_UTF8; | |
08114193 | 92 | field->event_field.type.u.array.length = LTTNG_UST_PROCNAME_LEN; |
4847e9bb MD |
93 | field->get_size = procname_get_size; |
94 | field->record = procname_record; | |
95 | return 0; | |
96 | } |