domain: add Python domain to list
[lttng-docs.git] / contents / using-lttng / instrumenting / c-application / probing-the-application-source-code.md
CommitLineData
5e0cbfb0
PP
1---
2id: probing-the-application-source-code
3---
4
5Once tracepoints are properly defined within a tracepoint provider,
6they may be inserted into the user application to be instrumented
7using the `tracepoint()` macro. Its first argument is the tracepoint
8provider name and its second is the tracepoint name. The next, optional
9arguments are defined by the `TP_ARGS()` part of the definition of
10the tracepoint to use.
11
12As an example, let us again take the following tracepoint definition:
13
14~~~ c
15TRACEPOINT_EVENT(
16 /* tracepoint provider name */
17 my_provider,
18
19 /* tracepoint/event name */
20 my_first_tracepoint,
21
22 /* list of tracepoint arguments */
23 TP_ARGS(
24 int, my_integer_arg,
25 char*, my_string_arg
26 ),
27
28 /* list of fields of eventual event */
29 TP_FIELDS(
30 ctf_string(my_string_field, my_string_arg)
31 ctf_integer(int, my_integer_field, my_integer_arg)
32 )
33)
34~~~
35
36Assuming this is part of a file named `tp.h` which defines the tracepoint
37provider and which is included by `tp.c`, here's a complete C application
38calling this tracepoint (multiple times):
39
40~~~ c
41#define TRACEPOINT_DEFINE
42#include "tp.h"
43
44int main(int argc, char* argv[])
45{
46 int i;
47
48 tracepoint(my_provider, my_first_tracepoint, 23, "Hello, World!");
49
50 for (i = 0; i < argc; ++i) {
51 tracepoint(my_provider, my_first_tracepoint, i, argv[i]);
52 }
53
54 return 0;
55}
56~~~
57
cd41f97c
PP
58For each tracepoint provider, `TRACEPOINT_DEFINE` must be defined into
59exactly one translation unit (C source file) of the user application,
60before including the tracepoint provider header file. In other words,
61for a given tracepoint provider, you cannot define `TRACEPOINT_DEFINE`,
62and then include its header file in two separate C source files of
63the same application. `TRACEPOINT_DEFINE` is discussed further in
5e0cbfb0
PP
64[Building/linking tracepoint providers and the user application](#doc-building-tracepoint-providers-and-user-application).
65
66As another example, remember this definition we wrote in a previous
67section (comments are stripped):
68
69~~~ c
70/* for struct stat */
71#include <sys/types.h>
72#include <sys/stat.h>
73#include <unistd.h>
74
75TRACEPOINT_EVENT(
76 my_provider,
77 my_tracepoint,
78 TP_ARGS(
79 int, my_int_arg,
80 char*, my_str_arg,
81 struct stat*, st
82 ),
83 TP_FIELDS(
84 ctf_integer(int, my_constant_field, 23 + 17)
85 ctf_integer(int, my_int_arg_field, my_int_arg)
86 ctf_integer(int, my_int_arg_field2, my_int_arg * my_int_arg)
87 ctf_integer(int, sum4_field, my_str_arg[0] + my_str_arg[1] +
88 my_str_arg[2] + my_str_arg[3])
89 ctf_string(my_str_arg_field, my_str_arg)
90 ctf_integer_hex(off_t, size_field, st->st_size)
91 ctf_float(double, size_dbl_field, (double) st->st_size)
92 ctf_sequence_text(char, half_my_str_arg_field, my_str_arg,
93 size_t, strlen(my_str_arg) / 2)
94 )
95)
96~~~
97
98Here's an example of calling it:
99
100~~~ c
101#define TRACEPOINT_DEFINE
102#include "tp.h"
103
104int main(void)
105{
106 struct stat s;
107
108 stat("/etc/fstab", &s);
109
110 tracepoint(my_provider, my_tracepoint, 23, "Hello, World!", &s);
111
112 return 0;
113}
114~~~
115
116When viewing the trace, assuming the file size of `/etc/fstab` is
117301&nbsp;bytes, the event generated by the execution of this tracepoint
118should have the following fields, in this order:
119
120~~~ text
121my_constant_field 40
122my_int_arg_field 23
123my_int_arg_field2 529
124sum4_field 389
125my_str_arg_field "Hello, World!"
126size_field 0x12d
127size_dbl_field 301.0
128half_my_str_arg_field "Hello,"
129~~~
This page took 0.028051 seconds and 4 git commands to generate.