From: Philippe Proulx Date: Thu, 7 Apr 2016 03:46:45 +0000 (-0400) Subject: Add doc/examples/barectf-tracepoint X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=0960480cb984f48801d44c3a21586d55d78f8b27;p=deliverable%2Fbarectf.git Add doc/examples/barectf-tracepoint Signed-off-by: Philippe Proulx --- diff --git a/doc/examples/.gitignore b/doc/examples/.gitignore index 1cf05b4..70e234e 100644 --- a/doc/examples/.gitignore +++ b/doc/examples/.gitignore @@ -1,2 +1,3 @@ -barectf*.c -barectf*.h +barectf.c +barectf.h +barectf-bitfield.h diff --git a/doc/examples/barectf-tracepoint/Makefile b/doc/examples/barectf-tracepoint/Makefile new file mode 100644 index 0000000..0574b05 --- /dev/null +++ b/doc/examples/barectf-tracepoint/Makefile @@ -0,0 +1,9 @@ +.PHONY: all clean + +all: + make -f Makefile.barectf + make -f Makefile.lttng-ust + +clean: + make -f Makefile.barectf clean + make -f Makefile.lttng-ust clean diff --git a/doc/examples/barectf-tracepoint/Makefile.barectf b/doc/examples/barectf-tracepoint/Makefile.barectf new file mode 100644 index 0000000..74fb5b3 --- /dev/null +++ b/doc/examples/barectf-tracepoint/Makefile.barectf @@ -0,0 +1,36 @@ +BARECTF ?= barectf +RM = rm -rf +MKDIR = mkdir + +PLATFORM_DIR = ../../../platforms/linux-fs +EXTRA_DIR = ../../../extra +CFLAGS = -O2 -Wall -pedantic -std=gnu99 -I$(PLATFORM_DIR) -I$(EXTRA_DIR) -I. + +TARGET = barectf-tracepoint-barectf +OBJS = $(TARGET).o barectf.o barectf-platform-linux-fs.o + +.PHONY: all clean + +all: $(TARGET) + +ctf: + $(MKDIR) ctf + +$(TARGET): $(OBJS) + $(CC) -o $@ $^ + +ctf/metadata barectf-bitfield.h barectf.h barectf.c: config.yaml ctf + $(BARECTF) $< -m ctf + +barectf.o: barectf.c + $(CC) $(CFLAGS) -c $< + +barectf-platform-linux-fs.o: $(PLATFORM_DIR)/barectf-platform-linux-fs.c + $(CC) $(CFLAGS) -c $< + +$(TARGET).o: barectf-tracepoint.c barectf.h barectf-bitfield.h + $(CC) -o $@ $(CFLAGS) -c $< + +clean: + $(RM) $(TARGET) $(OBJS) ctf + $(RM) barectf.h barectf-bitfield.h barectf.c diff --git a/doc/examples/barectf-tracepoint/Makefile.lttng-ust b/doc/examples/barectf-tracepoint/Makefile.lttng-ust new file mode 100644 index 0000000..84ec055 --- /dev/null +++ b/doc/examples/barectf-tracepoint/Makefile.lttng-ust @@ -0,0 +1,23 @@ +RM = rm -rf + +CFLAGS := -O2 -Wall -pedantic -std=gnu99 $(CFLAGS) +LDFLAGS := -llttng-ust -ldl $(LDFLAGS) + +TARGET = barectf-tracepoint-lttng-ust +OBJS = $(TARGET).o tp.o + +.PHONY: all clean + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CC) -o $@ $^ $(LDFLAGS) + +tp.o: tp.c + $(CC) -c $(CFLAGS) -I. $< + +$(TARGET).o: barectf-tracepoint.c tp.h + $(CC) -c -o $@ $(CFLAGS) -DWITH_LTTNG_UST=1 $< + +clean: + $(RM) $(TARGET) $(OBJS) diff --git a/doc/examples/barectf-tracepoint/README.md b/doc/examples/barectf-tracepoint/README.md new file mode 100644 index 0000000..c26a2a9 --- /dev/null +++ b/doc/examples/barectf-tracepoint/README.md @@ -0,0 +1,68 @@ +# Example using `barectf-tracepoint.h` + +This example is based on the [linux-fs-simple example](../linux-fs-simple), +but it uses the `tracepoint()` macro defined in +[`barectf-tracepoint.h`](../../../extra/barectf-tracepoint.h) instead of +calling the generated tracing functions directly. + +This example also shows the compatibility with +[LTTng-UST](http://lttng.org/) that this `barectf-tracepoint.h` allows. + + +## Building + +To build both barectf and LTTng-UST targets, make sure both tools are +installed, and do: + + make + +To build only the example using barectf: + + make -f Makefile.barectf + +To build only the example using LTTng-UST: + + make -f Makefile.lttng-ust + + +## barectf tracing + +Run this example: + + ./barectf-tracepoint-barectf + +The complete CTF trace is written to the `ctf` directory. + +You may run the example with any arguments; they will be recorded, +as string fields in the events of the binary stream, e.g.: + + ./barectf-tracepoint-barectf this argument and this one will be recorded + + +## LTTng-UST tracing + +Create an LTTng tracing session: + + lttng create my-session + +Enable the events of this example: + + lttng enable-event --userspace 'barectf_tp:*' + +Start tracing: + + lttng start + +Run this example: + + ./barectf-tracepoint-lttng-ust + +You may run the example with any arguments; they will be recorded, +as string fields in the events of the binary stream, e.g.: + + ./barectf-tracepoint-lttng-ust this argument and this one will be recorded + +Stop tracing and inspect the recorded events: + + lttng stop + lttng view diff --git a/doc/examples/barectf-tracepoint/barectf-tracepoint.c b/doc/examples/barectf-tracepoint/barectf-tracepoint.c new file mode 100644 index 0000000..6b94670 --- /dev/null +++ b/doc/examples/barectf-tracepoint/barectf-tracepoint.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include + +#ifdef WITH_LTTNG_UST +#include "tp.h" +#else /* #ifdef WITH_LTTNG_UST */ +#include + +/* + * Include generated barectf header file: this contains the prefix and + * default stream name to be used by the tracepoint() macro. + */ +#include "barectf.h" + +/* define how the context is to be found by tracepoint() calls */ +#define BARECTF_TRACEPOINT_CTX (global_barectf_ctx) + +/* then include this: */ +#include + +/* global barectf context (default stream) */ +static struct barectf_default_ctx *global_barectf_ctx; + +/* global barectf platform context */ +struct barectf_platform_linux_fs_ctx *global_barectf_platform_ctx; +#endif /* #ifdef WITH_LTTNG_UST */ + +enum state_t { + NEW, + TERMINATED, + READY, + RUNNING, + WAITING, +}; + +static void trace_stuff(int argc, char *argv[]) +{ + int i; + const char *str; + + /* record 40000 events */ + for (i = 0; i < 5000; ++i) { + tracepoint(barectf_tp, simple_uint32, i * 1500); + tracepoint(barectf_tp, simple_int16, -i * 2); + tracepoint(barectf_tp, simple_float, (float) i / 1.23); + + if (argc > 0) { + str = argv[i % argc]; + } else { + str = "hello there!"; + } + + tracepoint(barectf_tp, simple_string, str); + tracepoint(barectf_tp, simple_enum, RUNNING); + tracepoint(barectf_tp, a_few_fields, -1, 301, -3.14159, + str, NEW); + tracepoint(barectf_tp, bit_packed_integers, 1, -1, 3, + -2, 2, 7, 23, -55, 232); + tracepoint(barectf_tp, simple_enum, TERMINATED); + } +} + +#ifdef WITH_LTTNG_UST +#define init_barectf() +#define fini_barectf() +#else /* #ifdef WITH_LTTNG_UST */ +static void init_barectf(void) +{ + /* initialize platform */ + global_barectf_platform_ctx = + barectf_platform_linux_fs_init(512, "ctf", 1, 2, 7); + + if (!global_barectf_platform_ctx) { + fprintf(stderr, "Error: could not initialize platform\n"); + exit(1); + } + + global_barectf_ctx = barectf_platform_linux_fs_get_barectf_ctx( + global_barectf_platform_ctx); +} + +static void fini_barectf(void) +{ + /* finalize platform */ + barectf_platform_linux_fs_fini(global_barectf_platform_ctx); +} +#endif /* #ifdef WITH_LTTNG_UST */ + +int main(int argc, char *argv[]) +{ + init_barectf(); + trace_stuff(argc, argv); + fini_barectf(); + + return 0; +} diff --git a/doc/examples/barectf-tracepoint/config.yaml b/doc/examples/barectf-tracepoint/config.yaml new file mode 100644 index 0000000..4624a3e --- /dev/null +++ b/doc/examples/barectf-tracepoint/config.yaml @@ -0,0 +1,131 @@ +version: '2.2' +options: + gen-prefix-def: true + gen-default-stream-def: true +metadata: + $include: + - stdint.yaml + - stdfloat.yaml + - stdmisc.yaml + - lttng-ust-log-levels.yaml + type-aliases: + clock-int: + $inherit: uint64 + property-mappings: + - type: clock + name: default + property: value + state: + class: enum + value-type: uint8 + members: + - NEW + - TERMINATED + - READY + - RUNNING + - WAITING + clocks: + default: + freq: 1000000000 + offset: + seconds: 1434072888 + $return-ctype: uint64_t + trace: + $include: trace-basic.yaml + byte-order: le + streams: + default: + $default: true + packet-context-type: + class: struct + fields: + timestamp_begin: clock-int + timestamp_end: clock-int + packet_size: uint32 + content_size: uint32 + events_discarded: uint32 + event-header-type: + class: struct + fields: + timestamp: clock-int + id: uint16 + events: + barectf_tp_simple_uint32: + log-level: CRIT + payload-type: + class: struct + fields: + value: uint32 + barectf_tp_simple_int16: + payload-type: + class: struct + fields: + value: int16 + barectf_tp_simple_float: + payload-type: + class: struct + fields: + value: float + barectf_tp_simple_string: + log-level: WARNING + payload-type: + class: struct + fields: + value: + class: string + barectf_tp_simple_enum: + payload-type: + class: struct + fields: + value: state + barectf_tp_a_few_fields: + payload-type: + class: struct + fields: + int32: int32 + uint16: uint16 + dbl: double + str: + class: string + state: state + barectf_tp_bit_packed_integers: + log-level: 513 + payload-type: + class: struct + min-align: 8 + fields: + uint1: + $inherit: uint8 + size: 1 + align: 1 + int1: + $inherit: int8 + size: 1 + align: 1 + uint2: + $inherit: uint8 + size: 2 + align: 1 + int3: + $inherit: int8 + size: 3 + align: 1 + uint4: + $inherit: uint8 + size: 4 + align: 1 + int5: + $inherit: int8 + size: 5 + align: 1 + uint6: + $inherit: uint8 + size: 6 + align: 1 + int7: + $inherit: int8 + size: 7 + align: 1 + uint8: + $inherit: uint8 + align: 1 diff --git a/doc/examples/barectf-tracepoint/tp.c b/doc/examples/barectf-tracepoint/tp.c new file mode 100644 index 0000000..6edd6bf --- /dev/null +++ b/doc/examples/barectf-tracepoint/tp.c @@ -0,0 +1,4 @@ +#define TRACEPOINT_CREATE_PROBES +#define TRACEPOINT_DEFINE + +#include "tp.h" diff --git a/doc/examples/barectf-tracepoint/tp.h b/doc/examples/barectf-tracepoint/tp.h new file mode 100644 index 0000000..4cc1a66 --- /dev/null +++ b/doc/examples/barectf-tracepoint/tp.h @@ -0,0 +1,134 @@ +#undef TRACEPOINT_PROVIDER +#define TRACEPOINT_PROVIDER barectf_tp + +#undef TRACEPOINT_INCLUDE +#define TRACEPOINT_INCLUDE "./tp.h" + +#if !defined(_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ) +#define _TP_H + +#include +#include + +TRACEPOINT_EVENT( + barectf_tp, + simple_uint32, + TP_ARGS( + uint32_t, value + ), + TP_FIELDS( + ctf_integer(uint32_t, value, value) + ) +) + +TRACEPOINT_LOGLEVEL(barectf_tp, simple_uint32, TRACE_CRIT) + +TRACEPOINT_EVENT( + barectf_tp, + simple_int16, + TP_ARGS( + int16_t, value + ), + TP_FIELDS( + ctf_integer(int16_t, value, value) + ) +) + +TRACEPOINT_EVENT( + barectf_tp, + simple_float, + TP_ARGS( + float, value + ), + TP_FIELDS( + ctf_float(float, value, value) + ) +) + +TRACEPOINT_EVENT( + barectf_tp, + simple_string, + TP_ARGS( + const char *, value + ), + TP_FIELDS( + ctf_string(value, value) + ) +) + +TRACEPOINT_LOGLEVEL(barectf_tp, simple_string, TRACE_WARNING) + +TRACEPOINT_ENUM( + barectf_tp, + state, + TP_ENUM_VALUES( + ctf_enum_value("NEW", 0) + ctf_enum_value("TERMINATED", 1) + ctf_enum_value("READY", 2) + ctf_enum_value("RUNNING", 3) + ctf_enum_value("WAITING", 4) + ) +) + +TRACEPOINT_EVENT( + barectf_tp, + simple_enum, + TP_ARGS( + int, value + ), + TP_FIELDS( + ctf_enum(barectf_tp, state, int, value, value) + ) +) + +TRACEPOINT_EVENT( + barectf_tp, + a_few_fields, + TP_ARGS( + int32_t, int32, + uint16_t, int16, + double, dbl, + const char *, str, + int, state + ), + TP_FIELDS( + ctf_integer(int32_t, int32, int32) + ctf_integer(int16_t, int16, int16) + ctf_float(double, dbl, dbl) + ctf_string(str, str) + ctf_enum(barectf_tp, state, int, state, state) + ) +) + +TRACEPOINT_EVENT( + barectf_tp, + bit_packed_integers, + TP_ARGS( + uint8_t, uint1, + int8_t, int1, + uint8_t, uint2, + int8_t, int3, + uint8_t, uint4, + int8_t, int5, + uint8_t, uint6, + int8_t, int7, + uint8_t, uint8 + ), + TP_FIELDS( + ctf_integer(uint8_t, uint1, uint1) + ctf_integer(int8_t, int1, int1) + ctf_integer(uint8_t, uint2, uint2) + ctf_integer(int8_t, int3, int3) + ctf_integer(uint8_t, uint4, uint4) + ctf_integer(int8_t, int5, int5) + ctf_integer(uint8_t, uint6, uint6) + ctf_integer(int8_t, int7, int7) + ctf_integer(uint8_t, uint8, uint8) + ) +) + +TRACEPOINT_LOGLEVEL(barectf_tp, bit_packed_integers, 513) + +#endif /* _TP_H */ + +#include