Tests: Rename helper functions to have consistent names
[lttng-tools.git] / tests / kernel / kernel_event_basic.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <time.h>
27
28 #include <lttng/lttng.h>
29
30 #include "utils.h"
31
32 int lttng_opt_quiet;
33
34 int main(int argc, char **argv)
35 {
36 struct lttng_handle *handle = NULL;
37 struct lttng_domain dom;
38 struct lttng_channel channel;
39 struct lttng_event sched_switch;
40 struct lttng_event sched_process_exit;
41 struct lttng_event sched_process_free;
42 char *session_name = "kernel_event_basic";
43 int ret = 0;
44
45 memset(&dom, 0, sizeof(dom));
46 memset(&channel, 0, sizeof(channel));
47 memset(&sched_switch, 0, sizeof(sched_switch));
48 memset(&sched_process_exit, 0, sizeof(sched_process_exit));
49 memset(&sched_process_free, 0, sizeof(sched_process_free));
50
51 dom.type = LTTNG_DOMAIN_KERNEL;
52
53 strcpy(channel.name, "mychan");
54 channel.attr.overwrite = 0;
55 channel.attr.subbuf_size = 4096;
56 channel.attr.num_subbuf = 8;
57 channel.attr.switch_timer_interval = 0;
58 channel.attr.read_timer_interval = 200;
59 channel.attr.output = LTTNG_EVENT_SPLICE;
60
61 strcpy(sched_switch.name, "sched_switch");
62 sched_switch.type = LTTNG_EVENT_TRACEPOINT;
63 sched_switch.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
64
65 strcpy(sched_process_exit.name, "sched_process_exit");
66 sched_process_exit.type = LTTNG_EVENT_TRACEPOINT;
67 sched_process_exit.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
68
69 strcpy(sched_process_free.name, "sched_process_free");
70 sched_process_free.type = LTTNG_EVENT_TRACEPOINT;
71 sched_process_free.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
72
73 printf("\nTesting tracing kernel events:\n");
74 printf("-----------\n");
75 /* Check if root */
76 if (getuid() != 0) {
77 printf("Root access is needed.\nPlease run 'sudo make check' -- Aborting!\n");
78 return 0;
79 }
80
81 if (argc < 2) {
82 printf("Missing session trace path\n");
83 return 1;
84 }
85
86 printf("Creating tracing session (%s): ", argv[1]);
87 if ((ret = lttng_create_session(session_name, argv[1])) < 0) {
88 printf("error creating the session : %s\n", lttng_strerror(ret));
89 goto create_fail;
90 }
91 PRINT_OK();
92
93 printf("Creating session handle: ");
94 if ((handle = lttng_create_handle(session_name, &dom)) == NULL) {
95 printf("error creating handle: %s\n", lttng_strerror(ret));
96 goto handle_fail;
97 }
98 PRINT_OK();
99
100 printf("Enabling %s kernel channel: ", channel.name);
101 if ((ret = lttng_enable_channel(handle, &channel)) < 0) {
102 printf("error enable channel: %s\n", lttng_strerror(ret));
103 goto enable_fail;
104 }
105
106 printf("Enabling %s kernel event: ", sched_switch.name);
107 if ((ret = lttng_enable_event(handle, &sched_switch, channel.name)) < 0) {
108 printf("error enabling event: %s\n", lttng_strerror(ret));
109 goto enable_fail;
110 }
111 PRINT_OK();
112
113 printf("Enabling %s kernel event: ", sched_process_exit.name);
114 if ((ret = lttng_enable_event(handle, &sched_process_exit, channel.name)) < 0) {
115 printf("error enabling event: %s\n", lttng_strerror(ret));
116 goto enable_fail;
117 }
118 PRINT_OK();
119
120 printf("Enabling %s kernel event: ", sched_process_free.name);
121 if ((ret = lttng_enable_event(handle, &sched_process_free, channel.name)) < 0) {
122 printf("error enabling event: %s\n", lttng_strerror(ret));
123 goto enable_fail;
124 }
125 PRINT_OK();
126
127 printf("Disabling %s kernel event: ", sched_switch.name);
128 if ((ret = lttng_disable_event(handle, sched_switch.name, channel.name)) < 0) {
129 printf("error enabling event: %s\n", lttng_strerror(ret));
130 goto enable_fail;
131 }
132 PRINT_OK();
133
134 printf("Disabling %s kernel event: ", sched_process_free.name);
135 if ((ret = lttng_disable_event(handle, sched_process_free.name, channel.name)) < 0) {
136 printf("error enabling event: %s\n", lttng_strerror(ret));
137 goto enable_fail;
138 }
139 PRINT_OK();
140
141 printf("Renabling %s kernel event: ", sched_switch.name);
142 if ((ret = lttng_enable_event(handle, &sched_switch, channel.name)) < 0) {
143 printf("error enabling event: %s\n", lttng_strerror(ret));
144 goto enable_fail;
145 }
146 PRINT_OK();
147
148 printf("Renabling %s kernel event: ", sched_process_free.name);
149 if ((ret = lttng_enable_event(handle, &sched_process_free, channel.name)) < 0) {
150 printf("error enabling event: %s\n", lttng_strerror(ret));
151 goto enable_fail;
152 }
153 PRINT_OK();
154
155 printf("Start tracing: ");
156 if ((ret = lttng_start_tracing(session_name)) < 0) {
157 printf("error starting tracing: %s\n", lttng_strerror(ret));
158 goto start_fail;
159 }
160 PRINT_OK();
161
162 sleep(2);
163
164 printf("Stop tracing: ");
165 if ((ret = lttng_stop_tracing(session_name)) < 0) {
166 printf("error stopping tracing: %s\n", lttng_strerror(ret));
167 goto stop_fail;
168 }
169 PRINT_OK();
170
171 printf("Destroy tracing session: ");
172 if ((ret = lttng_destroy_session(session_name)) < 0) {
173 printf("error destroying session: %s\n", lttng_strerror(ret));
174 }
175 PRINT_OK();
176
177 return 0;
178
179 handle_fail:
180 assert(handle != NULL);
181 create_fail:
182 assert(ret != 0);
183
184 stop_fail:
185 start_fail:
186 enable_fail:
187 lttng_destroy_session(session_name);
188 lttng_destroy_handle(handle);
189
190 return 1;
191 }
This page took 0.036463 seconds and 4 git commands to generate.