Commit | Line | Data |
---|---|---|
f3ed775e | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
f3ed775e | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
f3ed775e | 5 | * |
f3ed775e DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
28ab034a JG |
9 | #include "command.hpp" |
10 | #include "conf.hpp" | |
11 | #include "utils.hpp" | |
f3ed775e | 12 | |
28ab034a | 13 | #include <common/defaults.hpp> |
c9e313bc | 14 | #include <common/error.hpp> |
ca938aa5 OD |
15 | #include <common/exception.hpp> |
16 | #include <common/make-unique-wrapper.hpp> | |
c9e313bc | 17 | #include <common/utils.hpp> |
f3ed775e | 18 | |
28ab034a JG |
19 | #include <arpa/inet.h> |
20 | #include <ctype.h> | |
ca938aa5 | 21 | #include <fnmatch.h> |
28ab034a | 22 | #include <inttypes.h> |
4d4c8b8e | 23 | #include <iostream> |
28ab034a JG |
24 | #include <limits.h> |
25 | #include <netinet/in.h> | |
26 | #include <signal.h> | |
27 | #include <stdlib.h> | |
28 | #include <sys/socket.h> | |
29 | #include <sys/types.h> | |
30 | #include <unistd.h> | |
f3ed775e | 31 | |
4fd2697f FD |
32 | static const char *str_all = "ALL"; |
33 | static const char *str_tracepoint = "Tracepoint"; | |
34 | static const char *str_syscall = "Syscall"; | |
35 | static const char *str_probe = "Probe"; | |
36 | static const char *str_userspace_probe = "Userspace Probe"; | |
37 | static const char *str_function = "Function"; | |
b9dfb167 | 38 | |
28ab034a | 39 | static char *_get_session_name(int quiet) |
f3ed775e | 40 | { |
4f00620d | 41 | const char *path; |
cd9adb8b | 42 | char *session_name = nullptr; |
f3ed775e DG |
43 | |
44 | /* Get path to config file */ | |
feb0f3e5 | 45 | path = utils_get_home_dir(); |
cd9adb8b | 46 | if (path == nullptr) { |
f3ed775e DG |
47 | goto error; |
48 | } | |
49 | ||
50 | /* Get session name from config */ | |
1dac0189 | 51 | session_name = quiet ? config_read_session_name_quiet(path) : |
28ab034a | 52 | config_read_session_name(path); |
cd9adb8b | 53 | if (session_name == nullptr) { |
58a97671 | 54 | goto error; |
f3ed775e DG |
55 | } |
56 | ||
3183dbb0 | 57 | DBG2("Config file path found: %s", path); |
cd80958d | 58 | DBG("Session name found: %s", session_name); |
f3ed775e | 59 | return session_name; |
3183dbb0 DG |
60 | |
61 | error: | |
cd9adb8b | 62 | return nullptr; |
f3ed775e | 63 | } |
679b4943 | 64 | |
1dac0189 PPM |
65 | /* |
66 | * get_session_name | |
67 | * | |
68 | * Return allocated string with the session name found in the config | |
69 | * directory. | |
70 | */ | |
cd9adb8b | 71 | char *get_session_name() |
1dac0189 PPM |
72 | { |
73 | return _get_session_name(0); | |
74 | } | |
75 | ||
76 | /* | |
77 | * get_session_name_quiet (no warnings/errors emitted) | |
78 | * | |
79 | * Return allocated string with the session name found in the config | |
80 | * directory. | |
81 | */ | |
cd9adb8b | 82 | char *get_session_name_quiet() |
1dac0189 PPM |
83 | { |
84 | return _get_session_name(1); | |
85 | } | |
86 | ||
3c9bd23c SM |
87 | /* |
88 | * list_commands | |
89 | * | |
90 | * List commands line by line. This is mostly for bash auto completion and to | |
91 | * avoid difficult parsing. | |
92 | */ | |
93 | void list_commands(struct cmd_struct *commands, FILE *ofp) | |
94 | { | |
95 | int i = 0; | |
cd9adb8b | 96 | struct cmd_struct *cmd = nullptr; |
3c9bd23c SM |
97 | |
98 | cmd = &commands[i]; | |
cd9adb8b | 99 | while (cmd->name != nullptr) { |
3c9bd23c SM |
100 | fprintf(ofp, "%s\n", cmd->name); |
101 | i++; | |
102 | cmd = &commands[i]; | |
103 | } | |
104 | } | |
679b4943 SM |
105 | |
106 | /* | |
107 | * list_cmd_options | |
108 | * | |
109 | * Prints a simple list of the options available to a command. This is intended | |
110 | * to be easily parsed for bash completion. | |
111 | */ | |
112 | void list_cmd_options(FILE *ofp, struct poptOption *options) | |
113 | { | |
114 | int i; | |
cd9adb8b | 115 | struct poptOption *option = nullptr; |
679b4943 | 116 | |
cd9adb8b | 117 | for (i = 0; options[i].longName != nullptr; i++) { |
679b4943 SM |
118 | option = &options[i]; |
119 | ||
120 | fprintf(ofp, "--%s\n", option->longName); | |
121 | ||
122 | if (isprint(option->shortName)) { | |
123 | fprintf(ofp, "-%c\n", option->shortName); | |
124 | } | |
125 | } | |
126 | } | |
8ce58bad | 127 | |
b083f028 JR |
128 | /* |
129 | * Same as list_cmd_options, but for options specified for argpar. | |
130 | */ | |
131 | void list_cmd_options_argpar(FILE *ofp, const struct argpar_opt_descr *options) | |
132 | { | |
133 | int i; | |
134 | ||
cd9adb8b | 135 | for (i = 0; options[i].long_name != nullptr; i++) { |
b083f028 JR |
136 | const struct argpar_opt_descr *option = &options[i]; |
137 | ||
138 | fprintf(ofp, "--%s\n", option->long_name); | |
139 | ||
140 | if (isprint(option->short_name)) { | |
141 | fprintf(ofp, "-%c\n", option->short_name); | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
8ce58bad MD |
146 | /* |
147 | * fls: returns the position of the most significant bit. | |
148 | * Returns 0 if no bit is set, else returns the position of the most | |
149 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). | |
150 | */ | |
151 | #if defined(__i386) || defined(__x86_64) | |
28ab034a | 152 | static inline unsigned int fls_u32(uint32_t x) |
8ce58bad MD |
153 | { |
154 | int r; | |
155 | ||
156 | asm("bsrl %1,%0\n\t" | |
157 | "jnz 1f\n\t" | |
158 | "movl $-1,%0\n\t" | |
159 | "1:\n\t" | |
28ab034a JG |
160 | : "=r"(r) |
161 | : "rm"(x)); | |
8ce58bad MD |
162 | return r + 1; |
163 | } | |
164 | #define HAS_FLS_U32 | |
165 | #endif | |
166 | ||
a1e4ab8b | 167 | #if defined(__x86_64) && defined(__LP64__) |
28ab034a | 168 | static inline unsigned int fls_u64(uint64_t x) |
8ce58bad MD |
169 | { |
170 | long r; | |
171 | ||
172 | asm("bsrq %1,%0\n\t" | |
173 | "jnz 1f\n\t" | |
174 | "movq $-1,%0\n\t" | |
175 | "1:\n\t" | |
28ab034a JG |
176 | : "=r"(r) |
177 | : "rm"(x)); | |
8ce58bad MD |
178 | return r + 1; |
179 | } | |
180 | #define HAS_FLS_U64 | |
181 | #endif | |
182 | ||
183 | #ifndef HAS_FLS_U64 | |
28ab034a | 184 | static __attribute__((unused)) unsigned int fls_u64(uint64_t x) |
8ce58bad MD |
185 | { |
186 | unsigned int r = 64; | |
187 | ||
188 | if (!x) | |
189 | return 0; | |
190 | ||
191 | if (!(x & 0xFFFFFFFF00000000ULL)) { | |
192 | x <<= 32; | |
193 | r -= 32; | |
194 | } | |
195 | if (!(x & 0xFFFF000000000000ULL)) { | |
196 | x <<= 16; | |
197 | r -= 16; | |
198 | } | |
199 | if (!(x & 0xFF00000000000000ULL)) { | |
200 | x <<= 8; | |
201 | r -= 8; | |
202 | } | |
203 | if (!(x & 0xF000000000000000ULL)) { | |
204 | x <<= 4; | |
205 | r -= 4; | |
206 | } | |
207 | if (!(x & 0xC000000000000000ULL)) { | |
208 | x <<= 2; | |
209 | r -= 2; | |
210 | } | |
211 | if (!(x & 0x8000000000000000ULL)) { | |
212 | x <<= 1; | |
213 | r -= 1; | |
214 | } | |
215 | return r; | |
216 | } | |
217 | #endif | |
218 | ||
219 | #ifndef HAS_FLS_U32 | |
28ab034a | 220 | static __attribute__((unused)) unsigned int fls_u32(uint32_t x) |
8ce58bad MD |
221 | { |
222 | unsigned int r = 32; | |
223 | ||
224 | if (!x) | |
225 | return 0; | |
226 | if (!(x & 0xFFFF0000U)) { | |
227 | x <<= 16; | |
228 | r -= 16; | |
229 | } | |
230 | if (!(x & 0xFF000000U)) { | |
231 | x <<= 8; | |
232 | r -= 8; | |
233 | } | |
234 | if (!(x & 0xF0000000U)) { | |
235 | x <<= 4; | |
236 | r -= 4; | |
237 | } | |
238 | if (!(x & 0xC0000000U)) { | |
239 | x <<= 2; | |
240 | r -= 2; | |
241 | } | |
242 | if (!(x & 0x80000000U)) { | |
243 | x <<= 1; | |
244 | r -= 1; | |
245 | } | |
246 | return r; | |
247 | } | |
248 | #endif | |
249 | ||
28ab034a | 250 | static unsigned int fls_ulong(unsigned long x) |
8ce58bad MD |
251 | { |
252 | #if (CAA_BITS_PER_LONG == 32) | |
253 | return fls_u32(x); | |
254 | #else | |
255 | return fls_u64(x); | |
256 | #endif | |
257 | } | |
258 | ||
259 | /* | |
260 | * Return the minimum order for which x <= (1UL << order). | |
261 | * Return -1 if x is 0. | |
262 | */ | |
263 | int get_count_order_u32(uint32_t x) | |
264 | { | |
265 | if (!x) | |
266 | return -1; | |
267 | ||
268 | return fls_u32(x - 1); | |
269 | } | |
270 | ||
271 | /* | |
272 | * Return the minimum order for which x <= (1UL << order). | |
273 | * Return -1 if x is 0. | |
274 | */ | |
275 | int get_count_order_u64(uint64_t x) | |
276 | { | |
277 | if (!x) | |
278 | return -1; | |
279 | ||
280 | return fls_u64(x - 1); | |
281 | } | |
282 | ||
283 | /* | |
284 | * Return the minimum order for which x <= (1UL << order). | |
285 | * Return -1 if x is 0. | |
286 | */ | |
287 | int get_count_order_ulong(unsigned long x) | |
288 | { | |
289 | if (!x) | |
290 | return -1; | |
291 | ||
292 | return fls_ulong(x - 1); | |
293 | } | |
b9dfb167 | 294 | |
4fd2697f FD |
295 | const char *get_event_type_str(enum lttng_event_type type) |
296 | { | |
297 | const char *str_event_type; | |
298 | ||
299 | switch (type) { | |
300 | case LTTNG_EVENT_ALL: | |
301 | str_event_type = str_all; | |
302 | break; | |
303 | case LTTNG_EVENT_TRACEPOINT: | |
304 | str_event_type = str_tracepoint; | |
305 | break; | |
306 | case LTTNG_EVENT_SYSCALL: | |
307 | str_event_type = str_syscall; | |
308 | break; | |
309 | case LTTNG_EVENT_PROBE: | |
310 | str_event_type = str_probe; | |
311 | break; | |
312 | case LTTNG_EVENT_USERSPACE_PROBE: | |
313 | str_event_type = str_userspace_probe; | |
314 | break; | |
315 | case LTTNG_EVENT_FUNCTION: | |
316 | str_event_type = str_function; | |
317 | break; | |
318 | default: | |
319 | /* Should not have an unknown event type or else define it. */ | |
a0377dfe | 320 | abort(); |
4fd2697f FD |
321 | } |
322 | ||
323 | return str_event_type; | |
324 | } | |
325 | ||
8960e9cd DG |
326 | /* |
327 | * Spawn a lttng relayd daemon by forking and execv. | |
328 | */ | |
329 | int spawn_relayd(const char *pathname, int port) | |
330 | { | |
331 | int ret = 0; | |
332 | pid_t pid; | |
333 | char url[255]; | |
334 | ||
335 | if (!port) { | |
336 | port = DEFAULT_NETWORK_VIEWER_PORT; | |
337 | } | |
338 | ||
339 | ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port); | |
340 | if (ret < 0) { | |
341 | goto end; | |
342 | } | |
343 | ||
344 | MSG("Spawning a relayd daemon"); | |
345 | pid = fork(); | |
346 | if (pid == 0) { | |
347 | /* | |
348 | * Spawn session daemon and tell | |
349 | * it to signal us when ready. | |
350 | */ | |
351 | execlp(pathname, "lttng-relayd", "-L", url, NULL); | |
352 | /* execlp only returns if error happened */ | |
353 | if (errno == ENOENT) { | |
354 | ERR("No relayd found. Use --relayd-path."); | |
355 | } else { | |
6f04ed72 | 356 | PERROR("execlp"); |
8960e9cd | 357 | } |
28ab034a | 358 | kill(getppid(), SIGTERM); /* wake parent */ |
8960e9cd DG |
359 | exit(EXIT_FAILURE); |
360 | } else if (pid > 0) { | |
361 | goto end; | |
362 | } else { | |
6f04ed72 | 363 | PERROR("fork"); |
8960e9cd DG |
364 | ret = -1; |
365 | goto end; | |
366 | } | |
367 | ||
368 | end: | |
369 | return ret; | |
370 | } | |
371 | ||
372 | /* | |
373 | * Check if relayd is alive. | |
374 | * | |
375 | * Return 1 if found else 0 if NOT found. Negative value on error. | |
376 | */ | |
cd9adb8b | 377 | int check_relayd() |
8960e9cd DG |
378 | { |
379 | int ret, fd; | |
380 | struct sockaddr_in sin; | |
381 | ||
382 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
383 | if (fd < 0) { | |
6f04ed72 | 384 | PERROR("socket check relayd"); |
dd02a4c1 DG |
385 | ret = -1; |
386 | goto error_socket; | |
8960e9cd DG |
387 | } |
388 | ||
389 | sin.sin_family = AF_INET; | |
390 | sin.sin_port = htons(DEFAULT_NETWORK_VIEWER_PORT); | |
391 | ret = inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr); | |
392 | if (ret < 1) { | |
6f04ed72 | 393 | PERROR("inet_pton check relayd"); |
dd02a4c1 | 394 | ret = -1; |
8960e9cd DG |
395 | goto error; |
396 | } | |
397 | ||
398 | /* | |
399 | * A successful connect means the relayd exists thus returning 0 else a | |
400 | * negative value means it does NOT exists. | |
401 | */ | |
56efeab3 | 402 | ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin)); |
8960e9cd DG |
403 | if (ret < 0) { |
404 | /* Not found. */ | |
405 | ret = 0; | |
406 | } else { | |
407 | /* Already spawned. */ | |
408 | ret = 1; | |
409 | } | |
410 | ||
8960e9cd | 411 | error: |
dd02a4c1 | 412 | if (close(fd) < 0) { |
6f04ed72 | 413 | PERROR("close relayd fd"); |
dd02a4c1 DG |
414 | } |
415 | error_socket: | |
416 | return ret; | |
8960e9cd | 417 | } |
3ecec76a | 418 | |
28ab034a | 419 | int print_missing_or_multiple_domains(unsigned int domain_count, bool include_agent_domains) |
3ecec76a PP |
420 | { |
421 | int ret = 0; | |
422 | ||
3533d06b JG |
423 | if (domain_count == 0) { |
424 | ERR("Please specify a domain (--kernel/--userspace%s).", | |
28ab034a | 425 | include_agent_domains ? "/--jul/--log4j/--python" : ""); |
3ecec76a | 426 | ret = -1; |
3533d06b JG |
427 | } else if (domain_count > 1) { |
428 | ERR("Only one domain must be specified."); | |
3ecec76a PP |
429 | ret = -1; |
430 | } | |
431 | ||
432 | return ret; | |
433 | } | |
20fb9e02 JD |
434 | |
435 | /* | |
436 | * Get the discarded events and lost packet counts. | |
437 | */ | |
438 | void print_session_stats(const char *session_name) | |
439 | { | |
58f237ca JG |
440 | char *str; |
441 | const int ret = get_session_stats_str(session_name, &str); | |
442 | ||
443 | if (ret >= 0 && str) { | |
444 | MSG("%s", str); | |
445 | free(str); | |
446 | } | |
447 | } | |
448 | ||
449 | int get_session_stats_str(const char *session_name, char **out_str) | |
450 | { | |
451 | int count, nb_domains, domain_idx, channel_idx, session_idx, ret; | |
cd9adb8b JG |
452 | struct lttng_domain *domains = nullptr; |
453 | struct lttng_channel *channels = nullptr; | |
3e8f2238 | 454 | uint64_t discarded_events_total = 0, lost_packets_total = 0; |
cd9adb8b JG |
455 | struct lttng_session *sessions = nullptr; |
456 | const struct lttng_session *selected_session = nullptr; | |
457 | char *stats_str = nullptr; | |
58f237ca | 458 | bool print_discarded_events = false, print_lost_packets = false; |
3e8f2238 JG |
459 | |
460 | count = lttng_list_sessions(&sessions); | |
461 | if (count < 1) { | |
462 | ERR("Failed to retrieve session descriptions while printing session statistics."); | |
58f237ca | 463 | ret = -1; |
3e8f2238 JG |
464 | goto end; |
465 | } | |
466 | ||
467 | /* Identify the currently-selected sessions. */ | |
468 | for (session_idx = 0; session_idx < count; session_idx++) { | |
469 | if (!strcmp(session_name, sessions[session_idx].name)) { | |
470 | selected_session = &sessions[session_idx]; | |
471 | break; | |
472 | } | |
473 | } | |
474 | if (!selected_session) { | |
28ab034a JG |
475 | ERR("Failed to retrieve session \"%s\" description while printing session statistics.", |
476 | session_name); | |
58f237ca | 477 | ret = -1; |
3e8f2238 JG |
478 | goto end; |
479 | } | |
20fb9e02 JD |
480 | |
481 | nb_domains = lttng_list_domains(session_name, &domains); | |
482 | if (nb_domains < 0) { | |
58f237ca | 483 | ret = -1; |
20fb9e02 JD |
484 | goto end; |
485 | } | |
486 | for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) { | |
28ab034a JG |
487 | struct lttng_handle *handle = |
488 | lttng_create_handle(session_name, &domains[domain_idx]); | |
20fb9e02 JD |
489 | |
490 | if (!handle) { | |
3e8f2238 | 491 | ERR("Failed to create session handle while printing session statistics."); |
58f237ca | 492 | ret = -1; |
20fb9e02 JD |
493 | goto end; |
494 | } | |
495 | ||
092545c3 | 496 | free(channels); |
cd9adb8b | 497 | channels = nullptr; |
20fb9e02 JD |
498 | count = lttng_list_channels(handle, &channels); |
499 | for (channel_idx = 0; channel_idx < count; channel_idx++) { | |
3e8f2238 | 500 | uint64_t discarded_events = 0, lost_packets = 0; |
20fb9e02 JD |
501 | struct lttng_channel *channel = &channels[channel_idx]; |
502 | ||
28ab034a | 503 | ret = lttng_channel_get_discarded_event_count(channel, &discarded_events); |
20fb9e02 JD |
504 | if (ret) { |
505 | ERR("Failed to retrieve discarded event count from channel %s", | |
28ab034a | 506 | channel->name); |
20fb9e02 JD |
507 | } |
508 | ||
28ab034a | 509 | ret = lttng_channel_get_lost_packet_count(channel, &lost_packets); |
20fb9e02 JD |
510 | if (ret) { |
511 | ERR("Failed to retrieve lost packet count from channel %s", | |
28ab034a | 512 | channel->name); |
20fb9e02 JD |
513 | } |
514 | ||
3e8f2238 JG |
515 | discarded_events_total += discarded_events; |
516 | lost_packets_total += lost_packets; | |
20fb9e02 JD |
517 | } |
518 | lttng_destroy_handle(handle); | |
519 | } | |
58f237ca | 520 | |
28ab034a JG |
521 | print_discarded_events = discarded_events_total > 0 && !selected_session->snapshot_mode; |
522 | print_lost_packets = lost_packets_total > 0 && !selected_session->snapshot_mode; | |
58f237ca JG |
523 | |
524 | if (print_discarded_events && print_lost_packets) { | |
525 | ret = asprintf(&stats_str, | |
28ab034a JG |
526 | "Warning: %" PRIu64 " events were discarded and %" PRIu64 |
527 | " packets were lost, please refer to " | |
528 | "the documentation on channel configuration.", | |
529 | discarded_events_total, | |
530 | lost_packets_total); | |
58f237ca JG |
531 | } else if (print_discarded_events) { |
532 | ret = asprintf(&stats_str, | |
28ab034a JG |
533 | "Warning: %" PRIu64 " events were discarded, please refer to " |
534 | "the documentation on channel configuration.", | |
535 | discarded_events_total); | |
58f237ca JG |
536 | } else if (print_lost_packets) { |
537 | ret = asprintf(&stats_str, | |
28ab034a JG |
538 | "Warning: %" PRIu64 " packets were lost, please refer to " |
539 | "the documentation on channel configuration.", | |
540 | lost_packets_total); | |
58f237ca JG |
541 | } else { |
542 | ret = 0; | |
20fb9e02 JD |
543 | } |
544 | ||
58f237ca JG |
545 | if (ret < 0) { |
546 | ERR("Failed to format lost packet and discarded events statistics"); | |
547 | } else { | |
548 | *out_str = stats_str; | |
549 | ret = 0; | |
550 | } | |
20fb9e02 | 551 | end: |
3e8f2238 | 552 | free(sessions); |
092545c3 SM |
553 | free(channels); |
554 | free(domains); | |
58f237ca | 555 | return ret; |
20fb9e02 | 556 | } |
4ba92f18 | 557 | |
4fc83d94 | 558 | int show_cmd_help(const char *cmd_name, const char *help_msg) |
4ba92f18 PP |
559 | { |
560 | int ret; | |
561 | char page_name[32]; | |
562 | ||
563 | ret = sprintf(page_name, "lttng-%s", cmd_name); | |
a0377dfe | 564 | LTTNG_ASSERT(ret > 0 && ret < 32); |
4fc83d94 PP |
565 | ret = utils_show_help(1, page_name, help_msg); |
566 | if (ret && !help_msg) { | |
567 | ERR("Cannot view man page `lttng-%s(1)`", cmd_name); | |
568 | perror("exec"); | |
569 | } | |
4ba92f18 | 570 | |
4fc83d94 | 571 | return ret; |
4ba92f18 | 572 | } |
bbbfd849 | 573 | |
28ab034a JG |
574 | int print_trace_archive_location(const struct lttng_trace_archive_location *location, |
575 | const char *session_name) | |
bbbfd849 JG |
576 | { |
577 | int ret = 0; | |
578 | enum lttng_trace_archive_location_type location_type; | |
579 | enum lttng_trace_archive_location_status status; | |
580 | bool printed_location = false; | |
581 | ||
582 | location_type = lttng_trace_archive_location_get_type(location); | |
583 | ||
28ab034a | 584 | _MSG("Trace chunk archive for session %s is now readable", session_name); |
bbbfd849 JG |
585 | switch (location_type) { |
586 | case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL: | |
587 | { | |
588 | const char *absolute_path; | |
589 | ||
28ab034a JG |
590 | status = lttng_trace_archive_location_local_get_absolute_path(location, |
591 | &absolute_path); | |
bbbfd849 JG |
592 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { |
593 | ret = -1; | |
594 | goto end; | |
595 | } | |
596 | MSG(" at %s", absolute_path); | |
597 | printed_location = true; | |
598 | break; | |
599 | } | |
600 | case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY: | |
601 | { | |
602 | uint16_t control_port, data_port; | |
603 | const char *host, *relative_path, *protocol_str; | |
604 | enum lttng_trace_archive_location_relay_protocol_type protocol; | |
605 | ||
606 | /* Fetch all relay location parameters. */ | |
28ab034a | 607 | status = lttng_trace_archive_location_relay_get_protocol_type(location, &protocol); |
bbbfd849 JG |
608 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { |
609 | ret = -1; | |
610 | goto end; | |
611 | } | |
612 | ||
28ab034a | 613 | status = lttng_trace_archive_location_relay_get_host(location, &host); |
bbbfd849 JG |
614 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { |
615 | ret = -1; | |
616 | goto end; | |
617 | } | |
618 | ||
28ab034a JG |
619 | status = lttng_trace_archive_location_relay_get_control_port(location, |
620 | &control_port); | |
bbbfd849 JG |
621 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { |
622 | ret = -1; | |
623 | goto end; | |
624 | } | |
625 | ||
28ab034a | 626 | status = lttng_trace_archive_location_relay_get_data_port(location, &data_port); |
bbbfd849 JG |
627 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { |
628 | ret = -1; | |
629 | goto end; | |
630 | } | |
631 | ||
28ab034a JG |
632 | status = lttng_trace_archive_location_relay_get_relative_path(location, |
633 | &relative_path); | |
bbbfd849 JG |
634 | if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { |
635 | ret = -1; | |
636 | goto end; | |
637 | } | |
638 | ||
639 | switch (protocol) { | |
640 | case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP: | |
641 | protocol_str = "tcp"; | |
642 | break; | |
643 | default: | |
644 | protocol_str = "unknown"; | |
645 | break; | |
646 | } | |
647 | ||
28ab034a JG |
648 | MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %" PRIu16 "]", |
649 | protocol_str, | |
650 | host, | |
651 | relative_path, | |
652 | control_port, | |
653 | data_port); | |
bbbfd849 JG |
654 | printed_location = true; |
655 | break; | |
656 | } | |
657 | default: | |
658 | break; | |
659 | } | |
660 | end: | |
661 | if (!printed_location) { | |
662 | MSG(" at an unknown location"); | |
663 | } | |
664 | return ret; | |
665 | } | |
ca938aa5 OD |
666 | |
667 | namespace { | |
668 | template <typename FilterFunctionType> | |
6e11909e JG |
669 | lttng::cli::session_list get_sessions(const FilterFunctionType& filter, |
670 | bool return_first_match_only = false) | |
ca938aa5 | 671 | { |
f74e820c | 672 | lttng::cli::session_list list = []() { |
ca938aa5 OD |
673 | int list_ret; |
674 | struct lttng_session *psessions; | |
675 | ||
676 | list_ret = lttng_list_sessions(&psessions); | |
677 | ||
678 | if (list_ret < 0) { | |
679 | LTTNG_THROW_CTL("Failed to list sessions", | |
680 | static_cast<lttng_error_code>(list_ret)); | |
681 | } | |
682 | ||
f74e820c JG |
683 | return lttng::cli::session_list(psessions, list_ret); |
684 | }(); | |
ca938aa5 OD |
685 | |
686 | std::size_t write_to = 0; | |
687 | for (std::size_t read_from = 0; read_from < list.size(); ++read_from) { | |
688 | if (!filter(list[read_from])) { | |
689 | continue; | |
690 | } | |
691 | ||
692 | if (read_from != write_to) { | |
693 | list[write_to] = list[read_from]; | |
694 | } | |
695 | ||
696 | ++write_to; | |
697 | ||
698 | if (return_first_match_only) { | |
6e11909e | 699 | return lttng::cli::session_list(std::move(list), 1); |
ca938aa5 OD |
700 | } |
701 | } | |
702 | ||
703 | list.resize(write_to); | |
704 | ||
705 | return list; | |
706 | } | |
707 | } /* namespace */ | |
708 | ||
6e11909e | 709 | lttng::cli::session_list lttng::cli::list_sessions(const struct session_spec& spec) |
ca938aa5 | 710 | { |
42a11b8f | 711 | switch (spec.type_) { |
6e11909e | 712 | case lttng::cli::session_spec::type::NAME: |
ca938aa5 OD |
713 | if (spec.value == nullptr) { |
714 | const auto configured_name = | |
715 | lttng::make_unique_wrapper<char, lttng::free>(get_session_name()); | |
716 | ||
717 | if (configured_name) { | |
6e11909e JG |
718 | const struct lttng::cli::session_spec new_spec( |
719 | lttng::cli::session_spec::type::NAME, | |
720 | configured_name.get()); | |
ca938aa5 OD |
721 | |
722 | return list_sessions(new_spec); | |
723 | } | |
724 | ||
6e11909e | 725 | return lttng::cli::session_list(); |
ca938aa5 OD |
726 | } |
727 | ||
728 | return get_sessions( | |
729 | [&spec](const lttng_session& session) { | |
730 | return strcmp(session.name, spec.value) == 0; | |
731 | }, | |
732 | true); | |
6e11909e | 733 | case lttng::cli::session_spec::type::GLOB_PATTERN: |
ca938aa5 OD |
734 | return get_sessions([&spec](const lttng_session& session) { |
735 | return fnmatch(spec.value, session.name, 0) == 0; | |
736 | }); | |
6e11909e | 737 | case lttng::cli::session_spec::type::ALL: |
ca938aa5 OD |
738 | return get_sessions([](const lttng_session&) { return true; }); |
739 | } | |
740 | ||
6e11909e | 741 | return lttng::cli::session_list(); |
ca938aa5 | 742 | } |
4d4c8b8e JG |
743 | |
744 | void print_kernel_tracer_status_error() | |
745 | { | |
746 | if (lttng_opt_mi) { | |
747 | return; | |
748 | } | |
749 | ||
750 | enum lttng_kernel_tracer_status kernel_tracer_status; | |
751 | const auto ret = lttng_get_kernel_tracer_status(&kernel_tracer_status); | |
752 | ||
753 | if (ret < 0) { | |
754 | ERR("Failed to get kernel tracer status: %s", lttng_strerror(ret)); | |
755 | } else { | |
756 | switch (kernel_tracer_status) { | |
757 | case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED: | |
758 | return; | |
759 | case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN: | |
760 | std::cerr << "\tKernel module loading failed" << std::endl; | |
761 | break; | |
762 | case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING: | |
763 | std::cerr << "\tMissing one or more required kernel modules" << std::endl; | |
764 | break; | |
765 | case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE: | |
766 | std::cerr | |
767 | << "\tKernel module signature error prevented loading of one or more required kernel modules" | |
768 | << std::endl; | |
769 | break; | |
770 | case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT: | |
771 | std::cerr << "\tlttng-sessiond isn't running as root" << std::endl; | |
772 | break; | |
773 | case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER: | |
774 | std::cerr << "\tFailed to setup notifiers" << std::endl; | |
775 | break; | |
776 | case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG: | |
777 | std::cerr << "\tlttng-sessiond failed to open /proc/lttng" << std::endl; | |
778 | break; | |
779 | case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH: | |
11927a78 JG |
780 | std::cerr << "\tVersion mismatch between kernel tracer and kernel tracer ABI" |
781 | << std::endl; | |
4d4c8b8e JG |
782 | break; |
783 | default: | |
784 | std::cerr << lttng::format("\t\tUnknown kernel tracer status (%d)", | |
785 | static_cast<int>(kernel_tracer_status)) | |
786 | << std::endl; | |
787 | break; | |
788 | } | |
789 | ||
790 | std::cerr << "\tConsult lttng-sessiond logs for more information" << std::endl; | |
791 | } | |
792 | } |