libustctl: use direct socket communication
[ust.git] / libustctl / libustctl.c
CommitLineData
ab33e65c 1/* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette
8b26d56b 2 * Copyright (C) 2011 Ericsson AB
ab33e65c
PP
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
ef290fca 19#define _GNU_SOURCE
ab33e65c
PP
20#include <stdio.h>
21#include <unistd.h>
22#include <getopt.h>
23#include <stdlib.h>
24#include <fcntl.h>
25#include <string.h>
26#include <dirent.h>
ab33e65c
PP
27
28#include "ustcomm.h"
2298f329 29#include "ust/ustctl.h"
2a79ceeb 30#include "usterr.h"
ab33e65c 31
8b26d56b 32static int do_cmd(int sock,
72098143
NC
33 const struct ustcomm_header *req_header,
34 const char *req_data,
35 struct ustcomm_header *res_header,
36 char **res_data)
37{
8b26d56b 38 int result, saved_errno = 0;
72098143
NC
39 char *recv_buf;
40
72098143
NC
41 recv_buf = zmalloc(USTCOMM_BUFFER_SIZE);
42 if (!recv_buf) {
43 saved_errno = ENOMEM;
8b26d56b 44 goto out;
72098143
NC
45 }
46
8b26d56b 47 result = ustcomm_req(sock, req_header, req_data, res_header, recv_buf);
72098143
NC
48 if (result > 0) {
49 saved_errno = -res_header->result;
50 if (res_header->size == 0 || saved_errno > 0) {
51 free(recv_buf);
52 } else {
53 if (res_data) {
54 *res_data = recv_buf;
55 } else {
56 free(recv_buf);
57 }
58 }
59 } else {
60 ERR("ustcomm req failed");
61 if (result == 0) {
62 saved_errno = ENOTCONN;
63 } else {
64 saved_errno = -result;
65 }
66 free(recv_buf);
67 }
68
8b26d56b 69out:
72098143 70 errno = saved_errno;
72098143
NC
71 if (errno) {
72 return -1;
73 }
74
75 return 0;
76}
77
8b26d56b
NC
78int ustctl_connect_pid(pid_t pid)
79{
80 int sock;
81
82 if (ustcomm_connect_app(pid, &sock)) {
83 ERR("could not connect to PID %u", (unsigned int) pid);
84 errno = ENOTCONN;
85 return -1;
86 }
87
88 return sock;
89}
90
2298f329 91pid_t *ustctl_get_online_pids(void)
772030fe 92{
08230db7
PMF
93 struct dirent *dirent;
94 DIR *dir;
ef290fca 95 unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
ab33e65c
PP
96
97 dir = opendir(SOCK_DIR);
98 if (!dir) {
99 return NULL;
100 }
101
08230db7 102 pid_t *ret = (pid_t *) malloc(ret_size);
ab33e65c 103
772030fe 104 while ((dirent = readdir(dir))) {
ab33e65c 105 if (!strcmp(dirent->d_name, ".") ||
72098143 106 !strcmp(dirent->d_name, "..")) {
ab33e65c
PP
107
108 continue;
109 }
110
111 if (dirent->d_type != DT_DIR &&
9dc7b7ff 112 !!strcmp(dirent->d_name, "ust-consumer")) {
ab33e65c 113
08230db7 114 sscanf(dirent->d_name, "%u", (unsigned int *) &ret[i]);
4723ca09
NC
115 /* FIXME: Here we previously called pid_is_online, which
116 * always returned 1, now I replaced it with just 1.
117 * We need to figure out an intelligent way of solving
118 * this, maybe connect-disconnect.
119 */
120 if (1) {
ab33e65c 121 ret_size += sizeof(pid_t);
08230db7 122 ret = (pid_t *) realloc(ret, ret_size);
ab33e65c
PP
123 ++i;
124 }
125 }
126 }
127
77957c95 128 ret[i] = 0; /* Array end */
ab33e65c 129
08230db7 130 if (ret[0] == 0) {
72098143 131 /* No PID at all */
ab33e65c
PP
132 free(ret);
133 return NULL;
134 }
135
136 closedir(dir);
137 return ret;
138}
139
140/**
2298f329 141 * Sets marker state (USTCTL_MS_ON or USTCTL_MS_OFF).
ab33e65c
PP
142 *
143 * @param mn Marker name
144 * @param state Marker's new state
145 * @param pid Traced process ID
2298f329 146 * @return 0 if successful, or errors {USTCTL_ERR_GEN, USTCTL_ERR_ARG}
ab33e65c 147 */
8b26d56b
NC
148int ustctl_set_marker_state(int sock, const char *trace, const char *channel,
149 const char *marker, int state)
ef290fca 150{
72098143
NC
151 struct ustcomm_header req_header, res_header;
152 struct ustcomm_marker_info marker_inf;
ef290fca
PMF
153 int result;
154
72098143
NC
155 result = ustcomm_pack_marker_info(&req_header,
156 &marker_inf,
d89b8191 157 trace,
72098143
NC
158 channel,
159 marker);
160 if (result < 0) {
161 errno = -result;
162 return -1;
08b8805e 163 }
ab33e65c 164
72098143 165 req_header.command = state ? ENABLE_MARKER : DISABLE_MARKER;
ab33e65c 166
8b26d56b 167 return do_cmd(sock, &req_header, (char *)&marker_inf,
72098143 168 &res_header, NULL);
ab33e65c
PP
169}
170
763f41e5
DS
171/**
172 * Set subbuffer size.
173 *
174 * @param channel_size Channel name and size
175 * @param pid Traced process ID
176 * @return 0 if successful, or error
177 */
8b26d56b
NC
178int ustctl_set_subbuf_size(int sock, const char *trace, const char *channel,
179 unsigned int subbuf_size)
763f41e5 180{
72098143
NC
181 struct ustcomm_header req_header, res_header;
182 struct ustcomm_channel_info ch_inf;
763f41e5
DS
183 int result;
184
72098143
NC
185 result = ustcomm_pack_channel_info(&req_header,
186 &ch_inf,
d89b8191 187 trace,
72098143
NC
188 channel);
189 if (result < 0) {
190 errno = -result;
08b8805e
DG
191 return -1;
192 }
763f41e5 193
72098143
NC
194 req_header.command = SET_SUBBUF_SIZE;
195 ch_inf.subbuf_size = subbuf_size;
763f41e5 196
8b26d56b 197 return do_cmd(sock, &req_header, (char *)&ch_inf,
72098143 198 &res_header, NULL);
763f41e5
DS
199}
200
201/**
202 * Set subbuffer num.
203 *
204 * @param channel_num Channel name and num
205 * @param pid Traced process ID
206 * @return 0 if successful, or error
207 */
8b26d56b
NC
208int ustctl_set_subbuf_num(int sock, const char *trace, const char *channel,
209 unsigned int num)
763f41e5 210{
72098143
NC
211 struct ustcomm_header req_header, res_header;
212 struct ustcomm_channel_info ch_inf;
763f41e5
DS
213 int result;
214
72098143
NC
215 result = ustcomm_pack_channel_info(&req_header,
216 &ch_inf,
d89b8191 217 trace,
72098143
NC
218 channel);
219 if (result < 0) {
220 errno = -result;
08b8805e
DG
221 return -1;
222 }
763f41e5 223
72098143
NC
224 req_header.command = SET_SUBBUF_NUM;
225 ch_inf.subbuf_num = num;
226
8b26d56b 227 return do_cmd(sock, &req_header, (char *)&ch_inf,
72098143 228 &res_header, NULL);
763f41e5 229
763f41e5
DS
230}
231
8b26d56b
NC
232
233static int ustctl_get_subbuf_num_size(int sock, const char *trace, const char *channel,
234 int *num, int *size)
e77b8e8e 235{
72098143
NC
236 struct ustcomm_header req_header, res_header;
237 struct ustcomm_channel_info ch_inf, *ch_inf_res;
e77b8e8e
DS
238 int result;
239
72098143
NC
240
241 result = ustcomm_pack_channel_info(&req_header,
242 &ch_inf,
d89b8191 243 trace,
72098143
NC
244 channel);
245 if (result < 0) {
246 errno = -result;
08b8805e
DG
247 return -1;
248 }
e77b8e8e 249
72098143
NC
250 req_header.command = GET_SUBBUF_NUM_SIZE;
251
8b26d56b 252 result = do_cmd(sock, &req_header, (char *)&ch_inf,
72098143
NC
253 &res_header, (char **)&ch_inf_res);
254 if (result < 0) {
e77b8e8e
DS
255 return -1;
256 }
257
72098143
NC
258 *num = ch_inf_res->subbuf_num;
259 *size = ch_inf_res->subbuf_size;
260
261 free(ch_inf_res);
262
263 return 0;
e77b8e8e
DS
264}
265
266/**
267 * Get subbuffer num.
268 *
269 * @param channel Channel name
270 * @param pid Traced process ID
271 * @return subbuf cnf if successful, or error
272 */
8b26d56b 273int ustctl_get_subbuf_num(int sock, const char *trace, const char *channel)
e77b8e8e 274{
72098143 275 int num, size, result;
e77b8e8e 276
8b26d56b 277 result = ustctl_get_subbuf_num_size(sock, trace, channel,
72098143
NC
278 &num, &size);
279 if (result < 0) {
280 errno = -result;
08b8805e
DG
281 return -1;
282 }
e77b8e8e 283
72098143
NC
284 return num;
285}
286
287/**
288 * Get subbuffer size.
289 *
290 * @param channel Channel name
291 * @param pid Traced process ID
292 * @return subbuf size if successful, or error
293 */
8b26d56b 294int ustctl_get_subbuf_size(int sock, const char *trace, const char *channel)
72098143
NC
295{
296 int num, size, result;
297
8b26d56b 298 result = ustctl_get_subbuf_num_size(sock, trace, channel,
72098143
NC
299 &num, &size);
300 if (result < 0) {
301 errno = -result;
e77b8e8e
DS
302 return -1;
303 }
304
72098143 305 return size;
e77b8e8e 306}
763f41e5 307
8b26d56b 308static int do_trace_cmd(int sock, const char *trace, int command)
d89b8191
NC
309{
310 struct ustcomm_header req_header, res_header;
28c1bb40 311 struct ustcomm_single_field trace_inf;
d89b8191
NC
312 int result;
313
28c1bb40
NC
314 result = ustcomm_pack_single_field(&req_header,
315 &trace_inf,
316 trace);
d89b8191
NC
317 if (result < 0) {
318 errno = -result;
319 return -1;
320 }
321
322 req_header.command = command;
323
8b26d56b 324 return do_cmd(sock, &req_header, (char *)&trace_inf, &res_header, NULL);
d89b8191
NC
325}
326
ab33e65c
PP
327/**
328 * Destroys an UST trace according to a PID.
329 *
330 * @param pid Traced process ID
2298f329 331 * @return 0 if successful, or error USTCTL_ERR_GEN
ab33e65c 332 */
8b26d56b 333int ustctl_destroy_trace(int sock, const char *trace)
772030fe 334{
8b26d56b 335 return do_trace_cmd(sock, trace, DESTROY_TRACE);
ab33e65c
PP
336}
337
338/**
339 * Starts an UST trace (and setups it) according to a PID.
340 *
341 * @param pid Traced process ID
2298f329 342 * @return 0 if successful, or error USTCTL_ERR_GEN
ab33e65c 343 */
8b26d56b 344int ustctl_setup_and_start(int sock, const char *trace)
772030fe 345{
8b26d56b 346 return do_trace_cmd(sock, trace, START);
ab33e65c
PP
347}
348
62ec620f
PMF
349/**
350 * Creates an UST trace according to a PID.
351 *
352 * @param pid Traced process ID
2298f329 353 * @return 0 if successful, or error USTCTL_ERR_GEN
62ec620f 354 */
8b26d56b 355int ustctl_create_trace(int sock, const char *trace)
62ec620f 356{
8b26d56b 357 return do_trace_cmd(sock, trace, CREATE_TRACE);
62ec620f
PMF
358}
359
ab33e65c
PP
360/**
361 * Starts an UST trace according to a PID.
362 *
363 * @param pid Traced process ID
2298f329 364 * @return 0 if successful, or error USTCTL_ERR_GEN
ab33e65c 365 */
8b26d56b 366int ustctl_start_trace(int sock, const char *trace)
772030fe 367{
8b26d56b 368 return do_trace_cmd(sock, trace, START_TRACE);
ab33e65c
PP
369}
370
763f41e5
DS
371/**
372 * Alloc an UST trace according to a PID.
373 *
374 * @param pid Traced process ID
2298f329 375 * @return 0 if successful, or error USTCTL_ERR_GEN
763f41e5 376 */
8b26d56b 377int ustctl_alloc_trace(int sock, const char *trace)
763f41e5 378{
8b26d56b 379 return do_trace_cmd(sock, trace, ALLOC_TRACE);
763f41e5
DS
380}
381
ab33e65c
PP
382/**
383 * Stops an UST trace according to a PID.
384 *
385 * @param pid Traced process ID
2298f329 386 * @return 0 if successful, or error USTCTL_ERR_GEN
ab33e65c 387 */
8b26d56b 388int ustctl_stop_trace(int sock, const char *trace)
772030fe 389{
8b26d56b 390 return do_trace_cmd(sock, trace, STOP_TRACE);
ab33e65c
PP
391}
392
393/**
394 * Counts newlines ('\n') in a string.
395 *
396 * @param str String to search in
397 * @return Total newlines count
398 */
2298f329 399unsigned int ustctl_count_nl(const char *str)
772030fe 400{
ab33e65c
PP
401 unsigned int i = 0, tot = 0;
402
403 while (str[i] != '\0') {
404 if (str[i] == '\n') {
405 ++tot;
406 }
407 ++i;
408 }
409
410 return tot;
411}
412
413/**
414 * Frees a CMSF array.
415 *
416 * @param cmsf CMSF array to free
2298f329 417 * @return 0 if successful, or error USTCTL_ERR_ARG
ab33e65c 418 */
2298f329 419int ustctl_free_cmsf(struct marker_status *cmsf)
772030fe 420{
ab33e65c 421 if (cmsf == NULL) {
2298f329 422 return USTCTL_ERR_ARG;
ab33e65c
PP
423 }
424
425 unsigned int i = 0;
426 while (cmsf[i].channel != NULL) {
427 free(cmsf[i].channel);
428 free(cmsf[i].marker);
429 free(cmsf[i].fs);
430 ++i;
431 }
432 free(cmsf);
433
434 return 0;
435}
436
437/**
438 * Gets channel/marker/state/format string for a given PID.
439 *
440 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
2298f329 441 * frees with `ustctl_free_cmsf')
ab33e65c 442 * @param pid Targeted PID
2a79ceeb 443 * @return 0 if successful, or -1 on error
ab33e65c 444 */
8b26d56b 445int ustctl_get_cmsf(int sock, struct marker_status **cmsf)
772030fe 446{
72098143 447 struct ustcomm_header req_header, res_header;
08230db7 448 char *big_str = NULL;
8b26d56b 449 int result;
08230db7 450 struct marker_status *tmp_cmsf = NULL;
ef290fca
PMF
451 unsigned int i = 0, cmsf_ind = 0;
452
ab33e65c 453 if (cmsf == NULL) {
2a79ceeb 454 return -1;
ab33e65c 455 }
72098143 456
72098143
NC
457 req_header.command = LIST_MARKERS;
458 req_header.size = 0;
459
8b26d56b 460 result = ustcomm_send(sock, &req_header, NULL);
72098143 461 if (result <= 0) {
8b26d56b 462 PERROR("error while requesting markers list");
2a79ceeb 463 return -1;
ab33e65c
PP
464 }
465
8b26d56b 466 result = ustcomm_recv_alloc(sock, &res_header, &big_str);
72098143
NC
467 if (result <= 0) {
468 ERR("error while receiving markers list");
469 return -1;
470 }
471
72098143 472 tmp_cmsf = (struct marker_status *) zmalloc(sizeof(struct marker_status) *
2298f329 473 (ustctl_count_nl(big_str) + 1));
ab33e65c 474 if (tmp_cmsf == NULL) {
dc46f6e6 475 ERR("Failed to allocate CMSF array");
2a79ceeb 476 return -1;
ab33e65c
PP
477 }
478
77957c95 479 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
ab33e65c 480 while (big_str[i] != '\0') {
ab33e65c 481 char state;
ef290fca 482
264f6231 483 sscanf(big_str + i, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
72098143
NC
484 &tmp_cmsf[cmsf_ind].channel,
485 &tmp_cmsf[cmsf_ind].marker,
486 &state,
487 &tmp_cmsf[cmsf_ind].fs);
2298f329
NC
488 tmp_cmsf[cmsf_ind].state = (state == USTCTL_MS_CHR_ON ?
489 USTCTL_MS_ON : USTCTL_MS_OFF); /* Marker state */
ab33e65c
PP
490
491 while (big_str[i] != '\n') {
77957c95 492 ++i; /* Go to next '\n' */
ab33e65c 493 }
77957c95 494 ++i; /* Skip current pointed '\n' */
ab33e65c
PP
495 ++cmsf_ind;
496 }
497 tmp_cmsf[cmsf_ind].channel = NULL;
498 tmp_cmsf[cmsf_ind].marker = NULL;
499 tmp_cmsf[cmsf_ind].fs = NULL;
500
501 *cmsf = tmp_cmsf;
502
503 free(big_str);
504 return 0;
505}
506
a3adfb05
NC
507/**
508 * Frees a TES array.
509 *
510 * @param tes TES array to free
2298f329 511 * @return 0 if successful, or error USTCTL_ERR_ARG
a3adfb05 512 */
2298f329 513int ustctl_free_tes(struct trace_event_status *tes)
a3adfb05
NC
514{
515 if (tes == NULL) {
2298f329 516 return USTCTL_ERR_ARG;
a3adfb05
NC
517 }
518
519 unsigned int i = 0;
520 while (tes[i].name != NULL) {
521 free(tes[i].name);
522 ++i;
523 }
524 free(tes);
525
526 return 0;
527}
528
529/**
530 * Gets trace_events string for a given PID.
531 *
532 * @param tes Pointer to TES array to be filled (callee allocates, caller
2298f329 533 * frees with `ustctl_free_tes')
a3adfb05
NC
534 * @param pid Targeted PID
535 * @return 0 if successful, or -1 on error
536 */
8b26d56b 537int ustctl_get_tes(int sock, struct trace_event_status **tes)
a3adfb05 538{
72098143 539 struct ustcomm_header req_header, res_header;
a3adfb05 540 char *big_str = NULL;
8b26d56b 541 int result;
a3adfb05
NC
542 struct trace_event_status *tmp_tes = NULL;
543 unsigned int i = 0, tes_ind = 0;
544
545 if (tes == NULL) {
546 return -1;
547 }
548
72098143
NC
549 req_header.command = LIST_TRACE_EVENTS;
550 req_header.size = 0;
551
8b26d56b 552 result = ustcomm_send(sock, &req_header, NULL);
72098143
NC
553 if (result != 1) {
554 ERR("error while requesting trace_event list");
555 return -1;
556 }
557
8b26d56b 558 result = ustcomm_recv_alloc(sock, &res_header, &big_str);
a3adfb05 559 if (result != 1) {
72098143 560 ERR("error while receiving markers list");
a3adfb05
NC
561 return -1;
562 }
563
564 tmp_tes = (struct trace_event_status *)
565 zmalloc(sizeof(struct trace_event_status) *
2298f329 566 (ustctl_count_nl(big_str) + 1));
a3adfb05
NC
567 if (tmp_tes == NULL) {
568 ERR("Failed to allocate TES array");
569 return -1;
570 }
571
572 /* Parse received reply string (format: "[name]"): */
573 while (big_str[i] != '\0') {
a3adfb05 574 sscanf(big_str + i, "trace_event: %a[^\n]",
72098143 575 &tmp_tes[tes_ind].name);
a3adfb05
NC
576 while (big_str[i] != '\n') {
577 ++i; /* Go to next '\n' */
578 }
579 ++i; /* Skip current pointed '\n' */
580 ++tes_ind;
581 }
582 tmp_tes[tes_ind].name = NULL;
583
584 *tes = tmp_tes;
585
586 free(big_str);
587 return 0;
588}
589
b2fb2f91 590/**
8b26d56b 591 * Set sock path
b2fb2f91 592 *
8b26d56b 593 * @param sock_path Sock path
b2fb2f91
AH
594 * @param pid Traced process ID
595 * @return 0 if successful, or error
596 */
8b26d56b 597int ustctl_set_sock_path(int sock, const char *sock_path)
b2fb2f91 598{
28c1bb40 599 int result;
72098143 600 struct ustcomm_header req_header, res_header;
28c1bb40 601 struct ustcomm_single_field sock_path_msg;
72098143 602
28c1bb40
NC
603 result = ustcomm_pack_single_field(&req_header,
604 &sock_path_msg,
605 sock_path);
606 if (result < 0) {
607 errno = -result;
08b8805e
DG
608 return -1;
609 }
b2fb2f91 610
72098143 611 req_header.command = SET_SOCK_PATH;
b2fb2f91 612
8b26d56b 613 return do_cmd(sock, &req_header, (char *)&sock_path_msg,
72098143 614 &res_header, NULL);
b2fb2f91
AH
615}
616
617/**
8b26d56b 618 * Get sock path
b2fb2f91 619 *
8b26d56b 620 * @param sock_path Pointer to where the sock path will be returned
b2fb2f91
AH
621 * @param pid Traced process ID
622 * @return 0 if successful, or error
623 */
8b26d56b 624int ustctl_get_sock_path(int sock, char **sock_path)
b2fb2f91 625{
b2fb2f91 626 int result;
72098143 627 struct ustcomm_header req_header, res_header;
28c1bb40 628 struct ustcomm_single_field *sock_path_msg;
72098143
NC
629
630 req_header.command = GET_SOCK_PATH;
631 req_header.size = 0;
b2fb2f91 632
8b26d56b 633 result = do_cmd(sock, &req_header, NULL, &res_header,
72098143
NC
634 (char **)&sock_path_msg);
635 if (result < 0) {
636 return -1;
08b8805e 637 }
b2fb2f91 638
28c1bb40 639 result = ustcomm_unpack_single_field(sock_path_msg);
72098143
NC
640 if (result < 0) {
641 return result;
b2fb2f91
AH
642 }
643
28c1bb40 644 *sock_path = strdup(sock_path_msg->field);
b2fb2f91 645
72098143 646 free(sock_path_msg);
b9318b35
AH
647
648 return 0;
649}
650
8b26d56b 651int ustctl_force_switch(int sock, const char *trace)
772030fe 652{
72098143 653 struct ustcomm_header req_header, res_header;
ab33e65c 654
72098143
NC
655 req_header.command = FORCE_SUBBUF_SWITCH;
656 req_header.size = 0;
ab33e65c 657
8b26d56b 658 return do_cmd(sock, &req_header, NULL, &res_header, NULL);
ab33e65c 659}
This page took 0.064826 seconds and 4 git commands to generate.