Change force_subbuffer switch to be per trace
[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
e005efaa
NC
382
383int ustctl_force_switch(int sock, const char *trace)
384{
385 return do_trace_cmd(sock, trace, FORCE_SUBBUF_SWITCH);
386}
387
ab33e65c
PP
388/**
389 * Stops an UST trace according to a PID.
390 *
391 * @param pid Traced process ID
2298f329 392 * @return 0 if successful, or error USTCTL_ERR_GEN
ab33e65c 393 */
8b26d56b 394int ustctl_stop_trace(int sock, const char *trace)
772030fe 395{
8b26d56b 396 return do_trace_cmd(sock, trace, STOP_TRACE);
ab33e65c
PP
397}
398
399/**
400 * Counts newlines ('\n') in a string.
401 *
402 * @param str String to search in
403 * @return Total newlines count
404 */
2298f329 405unsigned int ustctl_count_nl(const char *str)
772030fe 406{
ab33e65c
PP
407 unsigned int i = 0, tot = 0;
408
409 while (str[i] != '\0') {
410 if (str[i] == '\n') {
411 ++tot;
412 }
413 ++i;
414 }
415
416 return tot;
417}
418
419/**
420 * Frees a CMSF array.
421 *
422 * @param cmsf CMSF array to free
2298f329 423 * @return 0 if successful, or error USTCTL_ERR_ARG
ab33e65c 424 */
2298f329 425int ustctl_free_cmsf(struct marker_status *cmsf)
772030fe 426{
ab33e65c 427 if (cmsf == NULL) {
2298f329 428 return USTCTL_ERR_ARG;
ab33e65c
PP
429 }
430
431 unsigned int i = 0;
432 while (cmsf[i].channel != NULL) {
433 free(cmsf[i].channel);
434 free(cmsf[i].marker);
435 free(cmsf[i].fs);
436 ++i;
437 }
438 free(cmsf);
439
440 return 0;
441}
442
443/**
444 * Gets channel/marker/state/format string for a given PID.
445 *
446 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
2298f329 447 * frees with `ustctl_free_cmsf')
ab33e65c 448 * @param pid Targeted PID
2a79ceeb 449 * @return 0 if successful, or -1 on error
ab33e65c 450 */
8b26d56b 451int ustctl_get_cmsf(int sock, struct marker_status **cmsf)
772030fe 452{
72098143 453 struct ustcomm_header req_header, res_header;
08230db7 454 char *big_str = NULL;
8b26d56b 455 int result;
08230db7 456 struct marker_status *tmp_cmsf = NULL;
ef290fca
PMF
457 unsigned int i = 0, cmsf_ind = 0;
458
ab33e65c 459 if (cmsf == NULL) {
2a79ceeb 460 return -1;
ab33e65c 461 }
72098143 462
72098143
NC
463 req_header.command = LIST_MARKERS;
464 req_header.size = 0;
465
8b26d56b 466 result = ustcomm_send(sock, &req_header, NULL);
72098143 467 if (result <= 0) {
8b26d56b 468 PERROR("error while requesting markers list");
2a79ceeb 469 return -1;
ab33e65c
PP
470 }
471
8b26d56b 472 result = ustcomm_recv_alloc(sock, &res_header, &big_str);
72098143
NC
473 if (result <= 0) {
474 ERR("error while receiving markers list");
475 return -1;
476 }
477
72098143 478 tmp_cmsf = (struct marker_status *) zmalloc(sizeof(struct marker_status) *
2298f329 479 (ustctl_count_nl(big_str) + 1));
ab33e65c 480 if (tmp_cmsf == NULL) {
dc46f6e6 481 ERR("Failed to allocate CMSF array");
2a79ceeb 482 return -1;
ab33e65c
PP
483 }
484
77957c95 485 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
ab33e65c 486 while (big_str[i] != '\0') {
ab33e65c 487 char state;
ef290fca 488
264f6231 489 sscanf(big_str + i, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
72098143
NC
490 &tmp_cmsf[cmsf_ind].channel,
491 &tmp_cmsf[cmsf_ind].marker,
492 &state,
493 &tmp_cmsf[cmsf_ind].fs);
2298f329
NC
494 tmp_cmsf[cmsf_ind].state = (state == USTCTL_MS_CHR_ON ?
495 USTCTL_MS_ON : USTCTL_MS_OFF); /* Marker state */
ab33e65c
PP
496
497 while (big_str[i] != '\n') {
77957c95 498 ++i; /* Go to next '\n' */
ab33e65c 499 }
77957c95 500 ++i; /* Skip current pointed '\n' */
ab33e65c
PP
501 ++cmsf_ind;
502 }
503 tmp_cmsf[cmsf_ind].channel = NULL;
504 tmp_cmsf[cmsf_ind].marker = NULL;
505 tmp_cmsf[cmsf_ind].fs = NULL;
506
507 *cmsf = tmp_cmsf;
508
509 free(big_str);
510 return 0;
511}
512
a3adfb05
NC
513/**
514 * Frees a TES array.
515 *
516 * @param tes TES array to free
2298f329 517 * @return 0 if successful, or error USTCTL_ERR_ARG
a3adfb05 518 */
2298f329 519int ustctl_free_tes(struct trace_event_status *tes)
a3adfb05
NC
520{
521 if (tes == NULL) {
2298f329 522 return USTCTL_ERR_ARG;
a3adfb05
NC
523 }
524
525 unsigned int i = 0;
526 while (tes[i].name != NULL) {
527 free(tes[i].name);
528 ++i;
529 }
530 free(tes);
531
532 return 0;
533}
534
535/**
536 * Gets trace_events string for a given PID.
537 *
538 * @param tes Pointer to TES array to be filled (callee allocates, caller
2298f329 539 * frees with `ustctl_free_tes')
a3adfb05
NC
540 * @param pid Targeted PID
541 * @return 0 if successful, or -1 on error
542 */
8b26d56b 543int ustctl_get_tes(int sock, struct trace_event_status **tes)
a3adfb05 544{
72098143 545 struct ustcomm_header req_header, res_header;
a3adfb05 546 char *big_str = NULL;
8b26d56b 547 int result;
a3adfb05
NC
548 struct trace_event_status *tmp_tes = NULL;
549 unsigned int i = 0, tes_ind = 0;
550
551 if (tes == NULL) {
552 return -1;
553 }
554
72098143
NC
555 req_header.command = LIST_TRACE_EVENTS;
556 req_header.size = 0;
557
8b26d56b 558 result = ustcomm_send(sock, &req_header, NULL);
72098143
NC
559 if (result != 1) {
560 ERR("error while requesting trace_event list");
561 return -1;
562 }
563
8b26d56b 564 result = ustcomm_recv_alloc(sock, &res_header, &big_str);
a3adfb05 565 if (result != 1) {
72098143 566 ERR("error while receiving markers list");
a3adfb05
NC
567 return -1;
568 }
569
570 tmp_tes = (struct trace_event_status *)
571 zmalloc(sizeof(struct trace_event_status) *
2298f329 572 (ustctl_count_nl(big_str) + 1));
a3adfb05
NC
573 if (tmp_tes == NULL) {
574 ERR("Failed to allocate TES array");
575 return -1;
576 }
577
578 /* Parse received reply string (format: "[name]"): */
579 while (big_str[i] != '\0') {
a3adfb05 580 sscanf(big_str + i, "trace_event: %a[^\n]",
72098143 581 &tmp_tes[tes_ind].name);
a3adfb05
NC
582 while (big_str[i] != '\n') {
583 ++i; /* Go to next '\n' */
584 }
585 ++i; /* Skip current pointed '\n' */
586 ++tes_ind;
587 }
588 tmp_tes[tes_ind].name = NULL;
589
590 *tes = tmp_tes;
591
592 free(big_str);
593 return 0;
594}
595
b2fb2f91 596/**
8b26d56b 597 * Set sock path
b2fb2f91 598 *
8b26d56b 599 * @param sock_path Sock path
b2fb2f91
AH
600 * @param pid Traced process ID
601 * @return 0 if successful, or error
602 */
8b26d56b 603int ustctl_set_sock_path(int sock, const char *sock_path)
b2fb2f91 604{
28c1bb40 605 int result;
72098143 606 struct ustcomm_header req_header, res_header;
28c1bb40 607 struct ustcomm_single_field sock_path_msg;
72098143 608
28c1bb40
NC
609 result = ustcomm_pack_single_field(&req_header,
610 &sock_path_msg,
611 sock_path);
612 if (result < 0) {
613 errno = -result;
08b8805e
DG
614 return -1;
615 }
b2fb2f91 616
72098143 617 req_header.command = SET_SOCK_PATH;
b2fb2f91 618
8b26d56b 619 return do_cmd(sock, &req_header, (char *)&sock_path_msg,
72098143 620 &res_header, NULL);
b2fb2f91
AH
621}
622
623/**
8b26d56b 624 * Get sock path
b2fb2f91 625 *
8b26d56b 626 * @param sock_path Pointer to where the sock path will be returned
b2fb2f91
AH
627 * @param pid Traced process ID
628 * @return 0 if successful, or error
629 */
8b26d56b 630int ustctl_get_sock_path(int sock, char **sock_path)
b2fb2f91 631{
b2fb2f91 632 int result;
72098143 633 struct ustcomm_header req_header, res_header;
28c1bb40 634 struct ustcomm_single_field *sock_path_msg;
72098143
NC
635
636 req_header.command = GET_SOCK_PATH;
637 req_header.size = 0;
b2fb2f91 638
8b26d56b 639 result = do_cmd(sock, &req_header, NULL, &res_header,
72098143
NC
640 (char **)&sock_path_msg);
641 if (result < 0) {
642 return -1;
08b8805e 643 }
b2fb2f91 644
28c1bb40 645 result = ustcomm_unpack_single_field(sock_path_msg);
72098143
NC
646 if (result < 0) {
647 return result;
b2fb2f91
AH
648 }
649
28c1bb40 650 *sock_path = strdup(sock_path_msg->field);
b2fb2f91 651
72098143 652 free(sock_path_msg);
b9318b35
AH
653
654 return 0;
655}
This page took 0.06527 seconds and 4 git commands to generate.