Clarify TRACEPOINT_DEFINE and multi TP
[lttng-docs.git] / contents / using-lttng / instrumenting / c-application / building-linking / dynamic-linking.md
CommitLineData
5e0cbfb0
PP
1---
2id: dynamic-linking
3---
4
5The second approach to package the tracepoint providers is to use
6dynamic linking: the library and its member functions are explicitly
7sought, loaded and unloaded at runtime using `libdl`.
8
9It has to be noted that, for a variety of reasons, the created shared
10library will be dynamically _loaded_, as opposed to dynamically
11_linked_. The tracepoint provider shared object is, however, linked
12with `liblttng-ust`, so that `liblttng-ust` is guaranteed to be loaded
13as soon as the tracepoint provider is. If the tracepoint provider is
14not loaded, since the application itself is not linked with
15`liblttng-ust`, the latter is not loaded at all and the tracepoint calls
16become inert.
17
18The process to create the tracepoint provider shared object is pretty
19much the same as the static library method, except that:
20
21 * since the tracepoint provider is not part of the application
cd41f97c
PP
22 anymore, `TRACEPOINT_DEFINE` _must_ be defined, for each tracepoint
23 provider, in exactly one translation unit (C source file) of the
24 _application_;
5e0cbfb0
PP
25 * `TRACEPOINT_PROBE_DYNAMIC_LINKAGE` must be defined next to
26 `TRACEPOINT_DEFINE`.
27
cd41f97c
PP
28Regarding `TRACEPOINT_DEFINE` and `TRACEPOINT_PROBE_DYNAMIC_LINKAGE`,
29the recommended practice is to use a separate C source file in your
30application to define them, and then include the tracepoint provider
31header files afterwards, e.g.:
32
33~~~ c
34#define TRACEPOINT_DEFINE
35#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
36
37/* include the header files of one or more tracepoint providers below */
38#include "tp1.h"
39#include "tp2.h"
40#include "tp3.h"
41~~~
42
5e0cbfb0
PP
43`TRACEPOINT_PROBE_DYNAMIC_LINKAGE` makes the macros included afterwards
44(by including the tracepoint provider header, which itself includes
45LTTng-UST headers) aware that the tracepoint provider is to be loaded
46dynamically and not part of the application's executable.
47
48The tracepoint provider object file used to create the shared library
49is built like it is using the static library method, only with the
50`-fpic` option added:
51
52<pre class="term">
53gcc -c <strong>-fpic</strong> -I. tp.c
54</pre>
55
56It is then linked as a shared library like this:
57
58<pre class="term">
59gcc <strong>-shared -Wl,--no-as-needed -o tp.so -llttng-ust</strong> tp.o
60</pre>
61
62As previously stated, this tracepoint provider shared object isn't
63linked with the user application: it will be loaded manually. This is
64why the application is built with no mention of this tracepoint
65provider, but still needs `libdl`:
66
67<pre class="term">
68gcc -o app other.o files.o of.o your.o app.o <strong>-ldl</strong>
69</pre>
70
71Now, to make LTTng-UST tracing available to the application,
72the `LD_PRELOAD` environment variable is used to preload the
73tracepoint provider shared library _before_ the application actually
74starts:
75
76<pre class="term">
77<strong>LD_PRELOAD=/path/to/tp.so</strong> ./app
78</pre>
79
0cc03080 80Your application will still work without this preloading, albeit without
5e0cbfb0
PP
81LTTng-UST tracing support:
82
83<pre class="term">
84./app
85</pre>
cd41f97c 86
This page took 0.025606 seconds and 4 git commands to generate.