From 4847e9bbec5a73140193185073437d95fdf15f30 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Tue, 27 Sep 2011 21:16:19 -0400 Subject: [PATCH] Add procname context Signed-off-by: Mathieu Desnoyers --- include/ust/lttng-events.h | 1 + include/ust/lttng-ust-abi.h | 1 + libust/Makefile.am | 1 + libust/lttng-context-procname.c | 88 +++++++++++++++++++++ libust/lttng-ust-abi.c | 2 + tests/ust-basic-tracing/ust-basic-tracing.c | 2 + 6 files changed, 95 insertions(+) create mode 100644 libust/lttng-context-procname.c diff --git a/include/ust/lttng-events.h b/include/ust/lttng-events.h index 0439ea7a..e09443f1 100644 --- a/include/ust/lttng-events.h +++ b/include/ust/lttng-events.h @@ -323,6 +323,7 @@ void lttng_destroy_context(struct lttng_ctx *ctx); int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx); int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx); int lttng_add_pthread_id_to_ctx(struct lttng_ctx **ctx); +int lttng_add_procname_to_ctx(struct lttng_ctx **ctx); void lttng_context_vtid_reset(void); void lttng_context_vpid_reset(void); diff --git a/include/ust/lttng-ust-abi.h b/include/ust/lttng-ust-abi.h index c967fb11..303493ee 100644 --- a/include/ust/lttng-ust-abi.h +++ b/include/ust/lttng-ust-abi.h @@ -69,6 +69,7 @@ enum lttng_ust_context_type { LTTNG_UST_CONTEXT_VTID = 0, LTTNG_UST_CONTEXT_VPID = 1, LTTNG_UST_CONTEXT_PTHREAD_ID = 2, + LTTNG_UST_CONTEXT_PROCNAME = 3, }; struct lttng_ust_context { diff --git a/libust/Makefile.am b/libust/Makefile.am index cb00e179..9295d6b7 100644 --- a/libust/Makefile.am +++ b/libust/Makefile.am @@ -22,6 +22,7 @@ libust_la_SOURCES = \ lttng-context-vtid.c \ lttng-context-vpid.c \ lttng-context-pthread-id.c \ + lttng-context-procname.c \ ltt-context.c libust_la_LDFLAGS = -no-undefined -version-info 0:0:0 diff --git a/libust/lttng-context-procname.c b/libust/lttng-context-procname.c new file mode 100644 index 00000000..2056d194 --- /dev/null +++ b/libust/lttng-context-procname.c @@ -0,0 +1,88 @@ +/* + * (C) Copyright 2009-2011 - + * Mathieu Desnoyers + * + * LTTng UST procname context. + * + * Dual LGPL v2.1/GPL v2 license. + */ + +#include +#include +#include +#include +#include + +#define PROCNAME_LEN 17 /* includes \0 */ + +/* + * We cache the result to ensure we don't trigger a system call for + * each event. + * Upon exec, procname changes, but exec takes care of throwing away + * this cached version. + */ +static char cached_procname[17]; + +static inline +char *wrapper_getprocname(void) +{ + int ret; + + if (unlikely(!cached_procname[0])) { + ret = prctl(PR_GET_NAME, (unsigned long) cached_procname, + 0, 0, 0); + assert(!ret); + } + return cached_procname; +} + +void lttng_context_procname_reset(void) +{ + cached_procname[0] = '\0'; +} + +static +size_t procname_get_size(size_t offset) +{ + size_t size = 0; + + size += PROCNAME_LEN; + return size; +} + +static +void procname_record(struct lttng_ctx_field *field, + struct lib_ring_buffer_ctx *ctx, + struct ltt_channel *chan) +{ + char *procname; + + procname = wrapper_getprocname(); + chan->ops->event_write(ctx, procname, PROCNAME_LEN); +} + +int lttng_add_procname_to_ctx(struct lttng_ctx **ctx) +{ + struct lttng_ctx_field *field; + + field = lttng_append_context(ctx); + if (!field) + return -ENOMEM; + if (lttng_find_context(*ctx, "procname")) { + lttng_remove_context_field(ctx, field); + return -EEXIST; + } + field->event_field.name = "procname"; + field->event_field.type.atype = atype_array; + field->event_field.type.u.array.elem_type.atype = atype_integer; + field->event_field.type.u.array.elem_type.u.basic.integer.size = sizeof(char) * CHAR_BIT; + field->event_field.type.u.array.elem_type.u.basic.integer.alignment = lttng_alignof(char) * CHAR_BIT; + field->event_field.type.u.array.elem_type.u.basic.integer.signedness = lttng_is_signed_type(char); + field->event_field.type.u.array.elem_type.u.basic.integer.reverse_byte_order = 0; + field->event_field.type.u.array.elem_type.u.basic.integer.base = 10; + field->event_field.type.u.array.elem_type.u.basic.integer.encoding = lttng_encode_UTF8; + field->event_field.type.u.array.length = PROCNAME_LEN; + field->get_size = procname_get_size; + field->record = procname_record; + return 0; +} diff --git a/libust/lttng-ust-abi.c b/libust/lttng-ust-abi.c index e125ac6d..1d32e777 100644 --- a/libust/lttng-ust-abi.c +++ b/libust/lttng-ust-abi.c @@ -278,6 +278,8 @@ long lttng_abi_add_context(int objd, return lttng_add_vtid_to_ctx(ctx); case LTTNG_UST_CONTEXT_VPID: return lttng_add_vpid_to_ctx(ctx); + case LTTNG_UST_CONTEXT_PROCNAME: + return lttng_add_procname_to_ctx(ctx); default: return -EINVAL; } diff --git a/tests/ust-basic-tracing/ust-basic-tracing.c b/tests/ust-basic-tracing/ust-basic-tracing.c index d200b4f9..e97552a1 100644 --- a/tests/ust-basic-tracing/ust-basic-tracing.c +++ b/tests/ust-basic-tracing/ust-basic-tracing.c @@ -571,6 +571,8 @@ int send_app_msgs(int sock, const char *outputpath, lum.cmd = LTTNG_UST_CONTEXT; lum.u.context.ctx = LTTNG_UST_CONTEXT_VTID; //lum.u.context.ctx = LTTNG_UST_CONTEXT_PTHREAD_ID; + //lum.u.context.ctx = LTTNG_UST_CONTEXT_VPID; + //lum.u.context.ctx = LTTNG_UST_CONTEXT_PROCNAME; ret = send_app_cmd(sock, &lum, &lur); if (ret) return ret; -- 2.34.1