Merge remote-tracking branch 'gh-tuxology/patch-1'
[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
22 anymore, `TRACEPOINT_DEFINE` _must_ be defined in one translation
23 unit (C source file) of the _application_;
24 * `TRACEPOINT_PROBE_DYNAMIC_LINKAGE` must be defined next to
25 `TRACEPOINT_DEFINE`.
26
27`TRACEPOINT_PROBE_DYNAMIC_LINKAGE` makes the macros included afterwards
28(by including the tracepoint provider header, which itself includes
29LTTng-UST headers) aware that the tracepoint provider is to be loaded
30dynamically and not part of the application's executable.
31
32The tracepoint provider object file used to create the shared library
33is built like it is using the static library method, only with the
34`-fpic` option added:
35
36<pre class="term">
37gcc -c <strong>-fpic</strong> -I. tp.c
38</pre>
39
40It is then linked as a shared library like this:
41
42<pre class="term">
43gcc <strong>-shared -Wl,--no-as-needed -o tp.so -llttng-ust</strong> tp.o
44</pre>
45
46As previously stated, this tracepoint provider shared object isn't
47linked with the user application: it will be loaded manually. This is
48why the application is built with no mention of this tracepoint
49provider, but still needs `libdl`:
50
51<pre class="term">
52gcc -o app other.o files.o of.o your.o app.o <strong>-ldl</strong>
53</pre>
54
55Now, to make LTTng-UST tracing available to the application,
56the `LD_PRELOAD` environment variable is used to preload the
57tracepoint provider shared library _before_ the application actually
58starts:
59
60<pre class="term">
61<strong>LD_PRELOAD=/path/to/tp.so</strong> ./app
62</pre>
63
64You application will still work without this preloading, albeit without
65LTTng-UST tracing support:
66
67<pre class="term">
68./app
69</pre>
This page took 0.028375 seconds and 4 git commands to generate.