License text standardization, add missing licenses
[lttng-ust.git] / liblttng-ust / lttng-context-procname.c
CommitLineData
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
4c1ee94e 23#include <sys/prctl.h>
4318ae1b
MD
24#include <lttng/ust-events.h>
25#include <lttng/ust-tracer.h>
26#include <lttng/ringbuffer-config.h>
4847e9bb 27#include <assert.h>
4c1ee94e
MD
28
29#define PROCNAME_LEN 17 /* includes \0 */
4847e9bb
MD
30
31/*
32 * We cache the result to ensure we don't trigger a system call for
33 * each event.
34 * Upon exec, procname changes, but exec takes care of throwing away
35 * this cached version.
36 */
37static char cached_procname[17];
38
39static inline
40char *wrapper_getprocname(void)
41{
4c1ee94e
MD
42 int ret;
43
b5a3dfa5 44 if (caa_unlikely(!cached_procname[0])) {
4c1ee94e
MD
45 ret = prctl(PR_GET_NAME, (unsigned long) cached_procname,
46 0, 0, 0);
47 assert(!ret);
4847e9bb
MD
48 }
49 return cached_procname;
50}
51
52void lttng_context_procname_reset(void)
53{
54 cached_procname[0] = '\0';
55}
56
57static
58size_t procname_get_size(size_t offset)
59{
60 size_t size = 0;
61
4c1ee94e 62 size += PROCNAME_LEN;
4847e9bb
MD
63 return size;
64}
65
66static
67void procname_record(struct lttng_ctx_field *field,
4cfec15c 68 struct lttng_ust_lib_ring_buffer_ctx *ctx,
4847e9bb
MD
69 struct ltt_channel *chan)
70{
71 char *procname;
72
73 procname = wrapper_getprocname();
4c1ee94e 74 chan->ops->event_write(ctx, procname, PROCNAME_LEN);
4847e9bb
MD
75}
76
77int lttng_add_procname_to_ctx(struct lttng_ctx **ctx)
78{
79 struct lttng_ctx_field *field;
80
81 field = lttng_append_context(ctx);
82 if (!field)
83 return -ENOMEM;
84 if (lttng_find_context(*ctx, "procname")) {
85 lttng_remove_context_field(ctx, field);
86 return -EEXIST;
87 }
88 field->event_field.name = "procname";
89 field->event_field.type.atype = atype_array;
90 field->event_field.type.u.array.elem_type.atype = atype_integer;
91 field->event_field.type.u.array.elem_type.u.basic.integer.size = sizeof(char) * CHAR_BIT;
92 field->event_field.type.u.array.elem_type.u.basic.integer.alignment = lttng_alignof(char) * CHAR_BIT;
93 field->event_field.type.u.array.elem_type.u.basic.integer.signedness = lttng_is_signed_type(char);
94 field->event_field.type.u.array.elem_type.u.basic.integer.reverse_byte_order = 0;
95 field->event_field.type.u.array.elem_type.u.basic.integer.base = 10;
96 field->event_field.type.u.array.elem_type.u.basic.integer.encoding = lttng_encode_UTF8;
4c1ee94e 97 field->event_field.type.u.array.length = PROCNAME_LEN;
4847e9bb
MD
98 field->get_size = procname_get_size;
99 field->record = procname_record;
100 return 0;
101}
This page took 0.027688 seconds and 4 git commands to generate.