| 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 main(int argc, char **argv) |
| 33 | { |
| 34 | struct lttng_handle *handle = NULL; |
| 35 | struct lttng_domain dom; |
| 36 | struct lttng_channel channel; |
| 37 | struct lttng_event sched_switch; |
| 38 | struct lttng_event sched_process_exit; |
| 39 | struct lttng_event sched_process_free; |
| 40 | |
| 41 | int ret = 0; |
| 42 | |
| 43 | char *session_name = "kernel_event"; |
| 44 | |
| 45 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 46 | |
| 47 | strcpy(channel.name, "mychan"); |
| 48 | channel.attr.overwrite = 0; |
| 49 | channel.attr.subbuf_size = 4096; |
| 50 | channel.attr.num_subbuf = 8; |
| 51 | channel.attr.switch_timer_interval = 0; |
| 52 | channel.attr.read_timer_interval = 200; |
| 53 | channel.attr.output = LTTNG_EVENT_SPLICE; |
| 54 | |
| 55 | strcpy(sched_switch.name, "sched_switch"); |
| 56 | sched_switch.type = LTTNG_EVENT_TRACEPOINT; |
| 57 | |
| 58 | strcpy(sched_process_exit.name, "sched_process_exit"); |
| 59 | sched_process_exit.type = LTTNG_EVENT_TRACEPOINT; |
| 60 | |
| 61 | strcpy(sched_process_free.name, "sched_process_free"); |
| 62 | sched_process_free.type = LTTNG_EVENT_TRACEPOINT; |
| 63 | |
| 64 | printf("\nTesting tracing kernel events:\n"); |
| 65 | printf("-----------\n"); |
| 66 | /* Check if root */ |
| 67 | if (getuid() != 0) { |
| 68 | printf("Root access is needed.\nPlease run 'sudo make check' -- Aborting!\n"); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | if (argc < 2) { |
| 73 | printf("Missing session trace path\n"); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | printf("Creating tracing session (%s): ", argv[1]); |
| 78 | if ((ret = lttng_create_session(session_name, argv[1])) < 0) { |
| 79 | printf("error creating the session : %s\n", lttng_strerror(ret)); |
| 80 | goto create_fail; |
| 81 | } |
| 82 | PRINT_OK(); |
| 83 | |
| 84 | printf("Creating session handle: "); |
| 85 | if ((handle = lttng_create_handle(session_name, &dom)) == NULL) { |
| 86 | printf("error creating handle: %s\n", lttng_strerror(ret)); |
| 87 | goto handle_fail; |
| 88 | } |
| 89 | PRINT_OK(); |
| 90 | |
| 91 | printf("Enabling %s kernel channel: ", channel.name); |
| 92 | if ((ret = lttng_enable_channel(handle, &channel)) < 0) { |
| 93 | printf("error enable channel: %s\n", lttng_strerror(ret)); |
| 94 | goto enable_fail; |
| 95 | } |
| 96 | |
| 97 | printf("Enabling %s kernel event: ", sched_switch.name); |
| 98 | if ((ret = lttng_enable_event(handle, &sched_switch, channel.name)) < 0) { |
| 99 | printf("error enabling event: %s\n", lttng_strerror(ret)); |
| 100 | goto enable_fail; |
| 101 | } |
| 102 | PRINT_OK(); |
| 103 | |
| 104 | printf("Enabling %s kernel event: ", sched_process_exit.name); |
| 105 | if ((ret = lttng_enable_event(handle, &sched_process_exit, channel.name)) < 0) { |
| 106 | printf("error enabling event: %s\n", lttng_strerror(ret)); |
| 107 | goto enable_fail; |
| 108 | } |
| 109 | PRINT_OK(); |
| 110 | |
| 111 | printf("Enabling %s kernel event: ", sched_process_free.name); |
| 112 | if ((ret = lttng_enable_event(handle, &sched_process_free, channel.name)) < 0) { |
| 113 | printf("error enabling event: %s\n", lttng_strerror(ret)); |
| 114 | goto enable_fail; |
| 115 | } |
| 116 | PRINT_OK(); |
| 117 | |
| 118 | printf("Disabling %s kernel event: ", sched_switch.name); |
| 119 | if ((ret = lttng_disable_event(handle, sched_switch.name, channel.name)) < 0) { |
| 120 | printf("error enabling event: %s\n", lttng_strerror(ret)); |
| 121 | goto enable_fail; |
| 122 | } |
| 123 | PRINT_OK(); |
| 124 | |
| 125 | printf("Disabling %s kernel event: ", sched_process_free.name); |
| 126 | if ((ret = lttng_disable_event(handle, sched_process_free.name, channel.name)) < 0) { |
| 127 | printf("error enabling event: %s\n", lttng_strerror(ret)); |
| 128 | goto enable_fail; |
| 129 | } |
| 130 | PRINT_OK(); |
| 131 | |
| 132 | printf("Renabling %s kernel event: ", sched_switch.name); |
| 133 | if ((ret = lttng_enable_event(handle, &sched_switch, channel.name)) < 0) { |
| 134 | printf("error enabling event: %s\n", lttng_strerror(ret)); |
| 135 | goto enable_fail; |
| 136 | } |
| 137 | PRINT_OK(); |
| 138 | |
| 139 | printf("Renabling %s kernel event: ", sched_process_free.name); |
| 140 | if ((ret = lttng_enable_event(handle, &sched_process_free, channel.name)) < 0) { |
| 141 | printf("error enabling event: %s\n", lttng_strerror(ret)); |
| 142 | goto enable_fail; |
| 143 | } |
| 144 | PRINT_OK(); |
| 145 | |
| 146 | printf("Start tracing: "); |
| 147 | if ((ret = lttng_start_tracing(handle)) < 0) { |
| 148 | printf("error starting tracing: %s\n", lttng_strerror(ret)); |
| 149 | goto start_fail; |
| 150 | } |
| 151 | PRINT_OK(); |
| 152 | |
| 153 | sleep(2); |
| 154 | |
| 155 | printf("Stop tracing: "); |
| 156 | if ((ret = lttng_stop_tracing(handle)) < 0) { |
| 157 | printf("error stopping tracing: %s\n", lttng_strerror(ret)); |
| 158 | goto stop_fail; |
| 159 | } |
| 160 | PRINT_OK(); |
| 161 | |
| 162 | printf("Destroy tracing session: "); |
| 163 | if ((ret = lttng_destroy_session(session_name)) < 0) { |
| 164 | printf("error destroying session: %s\n", lttng_strerror(ret)); |
| 165 | } |
| 166 | PRINT_OK(); |
| 167 | |
| 168 | return 0; |
| 169 | |
| 170 | create_fail: |
| 171 | assert(ret != 0); |
| 172 | handle_fail: |
| 173 | assert(handle != NULL); |
| 174 | |
| 175 | stop_fail: |
| 176 | start_fail: |
| 177 | enable_fail: |
| 178 | lttng_destroy_session(session_name); |
| 179 | lttng_destroy_handle(handle); |
| 180 | |
| 181 | return 1; |
| 182 | } |