трассировка пространства пользователя LTTng (LTTng user space tracing)
Примеры (Examples)
Note
A few examples are available in the doc/examples
<https://github.com/lttng/lttng-
ust/tree/v2.10.6/doc/examples> directory of LTTng-UST's
source tree.
This example shows all the features documented in the previous
sections. The static linking method is chosen here to link the
application with the tracepoint provider.
You can compile the source files and link them together
statically like this:
$ cc -c -I. tp.c
$ cc -c app.c
$ cc -o app tp.o app.o -llttng-ust -ldl
Using the lttng(1) tool, create an LTTng tracing session, enable
all the events of this tracepoint provider, and start tracing:
$ lttng create my-session
$ lttng enable-event --userspace 'my_provider:*'
$ lttng start
You may also enable specific events:
$ lttng enable-event --userspace my_provider:big_event
$ lttng enable-event --userspace my_provider:event_instance2
Run the application:
$ ./app some arguments
Stop the current tracing session and inspect the recorded events:
$ lttng stop
$ lttng view
Tracepoint provider header file
tp.h
:
#undef TRACEPOINT_PROVIDER
#define TRACEPOINT_PROVIDER my_provider
#undef TRACEPOINT_INCLUDE
#define TRACEPOINT_INCLUDE "./tp.h"
#if !defined(_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ)
#define _TP_H
#include <lttng/tracepoint.h>
#include <stdio.h>
#include "app.h"
TRACEPOINT_EVENT(
my_provider,
simple_event,
TP_ARGS(
int, my_integer_arg,
const char *, my_string_arg
),
TP_FIELDS(
ctf_string(argc, my_string_arg)
ctf_integer(int, argv, my_integer_arg)
)
)
TRACEPOINT_ENUM(
my_provider,
my_enum,
TP_ENUM_VALUES(
ctf_enum_value("ZERO", 0)
ctf_enum_value("ONE", 1)
ctf_enum_value("TWO", 2)
ctf_enum_range("A RANGE", 52, 125)
ctf_enum_value("ONE THOUSAND", 1000)
)
)
TRACEPOINT_EVENT(
my_provider,
big_event,
TP_ARGS(
int, my_integer_arg,
const char *, my_string_arg,
FILE *, stream,
double, flt_arg,
int *, array_arg
),
TP_FIELDS(
ctf_integer(int, int_field1, my_integer_arg * 2)
ctf_integer_hex(long int, stream_pos, ftell(stream))
ctf_float(double, float_field, flt_arg)
ctf_string(string_field, my_string_arg)
ctf_array(int, array_field, array_arg, 7)
ctf_array_text(char, array_text_field, array_arg, 5)
ctf_sequence(int, seq_field, array_arg, int,
my_integer_arg / 10)
ctf_sequence_text(char, seq_text_field, array_arg,
int, my_integer_arg / 5)
ctf_enum(my_provider, my_enum, int,
enum_field, array_arg[1])
)
)
TRACEPOINT_LOGLEVEL(my_provider, big_event, TRACE_WARNING)
TRACEPOINT_EVENT_CLASS(
my_provider,
my_tracepoint_class,
TP_ARGS(
int, my_integer_arg,
struct app_struct *, app_struct_arg
),
TP_FIELDS(
ctf_integer(int, a, my_integer_arg)
ctf_integer(unsigned long, b, app_struct_arg->b)
ctf_string(c, app_struct_arg->c)
)
)
TRACEPOINT_EVENT_INSTANCE(
my_provider,
my_tracepoint_class,
event_instance1,
TP_ARGS(
int, my_integer_arg,
struct app_struct *, app_struct_arg
)
)
TRACEPOINT_EVENT_INSTANCE(
my_provider,
my_tracepoint_class,
event_instance2,
TP_ARGS(
int, my_integer_arg,
struct app_struct *, app_struct_arg
)
)
TRACEPOINT_LOGLEVEL(my_provider, event_instance2, TRACE_INFO)
TRACEPOINT_EVENT_INSTANCE(
my_provider,
my_tracepoint_class,
event_instance3,
TP_ARGS(
int, my_integer_arg,
struct app_struct *, app_struct_arg
)
)
#endif /* _TP_H */
#include <lttng/tracepoint-event.h>
Tracepoint provider source file
tp.c
:
#define TRACEPOINT_CREATE_PROBES
#define TRACEPOINT_DEFINE
#include "tp.h"
Application header file
app.h
:
#ifndef _APP_H
#define _APP_H
struct app_struct {
unsigned long b;
const char *c;
double d;
};
#endif /* _APP_H */
Application source file
app.c
:
#include <stdlib.h>
#include <stdio.h>
#include "tp.h"
#include "app.h"
static int array_of_ints[] = {
100, -35, 1, 23, 14, -6, 28, 1001, -3000,
};
int main(int argc, char* argv[])
{
FILE *stream;
struct app_struct app_struct;
tracepoint(my_provider, simple_event, argc, argv[0]);
stream = fopen("/tmp/app.txt", "w");
if (!stream) {
fprintf(stderr,
"Error: Cannot open /tmp/app.txt for writing\n");
return EXIT_FAILURE;
}
if (fprintf(stream, "0123456789") != 10) {
fclose(stream);
fprintf(stderr, "Error: Cannot write to /tmp/app.txt\n");
return EXIT_FAILURE;
}
tracepoint(my_provider, big_event, 35, "hello tracepoint",
stream, -3.14, array_of_ints);
fclose(stream);
app_struct.b = argc;
app_struct.c = "[the string]";
tracepoint(my_provider, event_instance1, 23, &app_struct);
app_struct.b = argc * 5;
app_struct.c = "[other string]";
tracepoint(my_provider, event_instance2, 17, &app_struct);
app_struct.b = 23;
app_struct.c = "nothing";
tracepoint(my_provider, event_instance3, -52, &app_struct);
return EXIT_SUCCESS;
}