manpage -> man pages
[lttng-docs.git] / contents / getting-started / tracing-your-own-user-application.md
CommitLineData
5e0cbfb0
PP
1---
2id: tracing-your-own-user-application
3---
4
5The previous section helped you create a trace out of Linux kernel events.
6This section steps you through a simple example showing you how to trace
7a _Hello world_ program written in C.
8
9Make sure LTTng-tools and LTTng-UST packages
10[are installed](#doc-installing-lttng).
11
12Tracing is just like having `printf()` calls at specific locations of
13your source code, albeit LTTng is much more faster and flexible than
14`printf()`. In the LTTng realm, **`tracepoint()`** is analogous to
15`printf()`.
16
17Unlike `printf()`, though, `tracepoint()` does not use a format string to
18know the types of its arguments: the formats of all tracepoints must be
19defined before using them. So before even writing our _Hello world_ program,
20we need to define the format of our tracepoint. This is done by writing a
066f96ac 21**template file**, with a name usually ending with the `.tp` extension (for **t**race**p**oint),
47bfcb75 22which the `lttng-gen-tp` tool (shipped with LTTng-UST) uses to generate
066f96ac 23an object file (along with a `.c` file) and a header to be included in our application source code.
5e0cbfb0
PP
24
25Here's the whole flow:
26
27<div class="img img-80">
4280f592
PP
28 <object data="/images/docs26/lttng-lttng-gen-tp.svg" type="image/svg+xml">
29 <img src="/images/docs26/lttng-lttng-gen-tp.svg">
5e0cbfb0
PP
30 </object>
31</div>
32
33The template file format is a list of tracepoint definitions
47bfcb75 34and other optional definition entries which we skip for
5e0cbfb0
PP
35this quickstart. Each tracepoint is defined using the
36`TRACEPOINT_EVENT()` macro. For each tracepoint, you must provide:
37
38 * a **provider name**, which is the "scope" of this tracepoint (this usually
39 includes the company and project names)
40 * a **tracepoint name**
41 * a **list of arguments** for the eventual `tracepoint()` call, each item being:
42 * the argument C type
43 * the argument name
47bfcb75
PP
44 * a **list of fields**, which correspond to the actual fields of the
45 recorded events for this tracepoint
5e0cbfb0
PP
46
47Here's a simple tracepoint definition example with two arguments: an integer
48and a string:
49
50~~~ c
51TRACEPOINT_EVENT(
52 hello_world,
53 my_first_tracepoint,
54 TP_ARGS(
55 int, my_integer_arg,
56 char*, my_string_arg
57 ),
58 TP_FIELDS(
59 ctf_string(my_string_field, my_string_arg)
60 ctf_integer(int, my_integer_field, my_integer_arg)
61 )
62)
63~~~
64
65The exact syntax is well explained in the
66[C application](#doc-c-application) instrumenting guide of the
67[Using LTTng](#doc-using-lttng) chapter, as well as in the
5037eb52 68<a href="/man/3/lttng-ust" class="ext">LTTng-UST man page</a>.
5e0cbfb0
PP
69
70Save the above snippet as `hello-tp.tp` and run:
71
72<pre class="term">
73lttng-gen-tp hello-tp.tp
74</pre>
75
47bfcb75 76The following files are created next to `hello-tp.tp`:
5e0cbfb0
PP
77
78 * `hello-tp.c`
79 * `hello-tp.o`
80 * `hello-tp.h`
81
82`hello-tp.o` is the compiled object file of `hello-tp.c`.
83
84Now, by including `hello-tp.h` in your own application, you may use the
85tracepoint defined above by properly refering to it when calling
86`tracepoint()`:
87
88~~~ c
89#include <stdio.h>
90#include "hello-tp.h"
91
92int main(int argc, char* argv[])
93{
94 int x;
95
96 puts("Hello, World!\nPress Enter to continue...");
97
98 /* The following getchar() call is only placed here for the purpose
99 * of this demonstration, for pausing the application in order for
100 * you to have time to list its events. It's not needed otherwise.
101 */
102 getchar();
103
104 /* A tracepoint() call. Arguments, as defined in hello-tp.tp:
105 *
106 * 1st: provider name (always)
107 * 2nd: tracepoint name (always)
108 * 3rd: my_integer_arg (first user-defined argument)
109 * 4th: my_string_arg (second user-defined argument)
110 *
111 * Notice the provider and tracepoint names are NOT strings;
112 * they are in fact parts of variables created by macros in
113 * hello-tp.h.
114 */
115 tracepoint(hello_world, my_first_tracepoint, 23, "hi there!");
116
117 for (x = 0; x < argc; ++x) {
118 tracepoint(hello_world, my_first_tracepoint, x, argv[x]);
119 }
120
121 puts("Quitting now!");
122
123 tracepoint(hello_world, my_first_tracepoint, x * x, "x^2");
124
125 return 0;
126}
127~~~
128
129Save this as `hello.c`, next to `hello-tp.tp`.
130
131Notice `hello-tp.h`, the header file generated by `lttng-gen-tp` from
132our template file `hello-tp.tp`, is included by `hello.c`.
133
134You are now ready to compile the application with LTTng-UST support:
135
136<pre class="term">
137gcc -o hello hello.c <strong>hello-tp.o -llttng-ust -ldl</strong>
138</pre>
139
140If you followed the
141[Tracing the Linux kernel](#doc-tracing-the-linux-kernel) section, the
47bfcb75 142following steps should look familiar.
5e0cbfb0
PP
143
144First, run the application with a few arguments:
145
146<pre class="term">
147./hello world and beyond
148</pre>
149
150You should see
151
152~~~ text
153Hello, World!
154Press Enter to continue...
155~~~
156
157Use the `lttng` tool to list all available user space events:
158
159<pre class="term">
160lttng list --userspace
161</pre>
162
163You should see the `hello_world:my_first_tracepoint` tracepoint listed
164under the `./hello` process.
165
166Create a tracing session:
167
168<pre class="term">
169lttng create my-userspace-session
170</pre>
171
172Enable the `hello_world:my_first_tracepoint` tracepoint:
173
174<pre class="term">
175lttng enable-event --userspace hello_world:my_first_tracepoint
176</pre>
177
178Start tracing:
179
180<pre class="term">
181lttng start
182</pre>
183
184Go back to the running `hello` application and press Enter. All `tracepoint()`
47bfcb75 185calls are executed and the program finally exits.
5e0cbfb0 186
8eb0513d 187Stop tracing:
5e0cbfb0
PP
188
189<pre class="term">
190lttng stop
5e0cbfb0
PP
191</pre>
192
193Done! You may use `lttng view` to list the recorded events. This command
194starts
195<a href="http://www.efficios.com/babeltrace" class="ext"><code>babeltrace</code></a>
196in the background, if it is installed:
197
198<pre class="term">
199lttng view
200</pre>
201
202should output something like:
203
204~~~ text
205[18:10:27.684304496] (+?.?????????) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "hi there!", my_integer_field = 23 }
206[18:10:27.684338440] (+0.000033944) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "./hello", my_integer_field = 0 }
207[18:10:27.684340692] (+0.000002252) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "world", my_integer_field = 1 }
208[18:10:27.684342616] (+0.000001924) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "and", my_integer_field = 2 }
209[18:10:27.684343518] (+0.000000902) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "beyond", my_integer_field = 3 }
210[18:10:27.684357978] (+0.000014460) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "x^2", my_integer_field = 16 }
211~~~
212
8eb0513d
PP
213When you're done, you may destroy the tracing session, which does _not_
214destroy the generated trace files, leaving them available for further
215analysis:
216
217<pre class="term">
218lttng destroy my-userspace-session
219</pre>
220
5e0cbfb0
PP
221The next section presents other alternatives to view and analyze your
222LTTng traces.
This page took 0.030843 seconds and 4 git commands to generate.