libustctl: use direct socket communication
[ust.git] / libustctl / libustctl.c
1 /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette
2 * Copyright (C) 2011 Ericsson AB
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
19 #define _GNU_SOURCE
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>
27
28 #include "ustcomm.h"
29 #include "ust/ustctl.h"
30 #include "usterr.h"
31
32 static int do_cmd(int sock,
33 const struct ustcomm_header *req_header,
34 const char *req_data,
35 struct ustcomm_header *res_header,
36 char **res_data)
37 {
38 int result, saved_errno = 0;
39 char *recv_buf;
40
41 recv_buf = zmalloc(USTCOMM_BUFFER_SIZE);
42 if (!recv_buf) {
43 saved_errno = ENOMEM;
44 goto out;
45 }
46
47 result = ustcomm_req(sock, req_header, req_data, res_header, recv_buf);
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
69 out:
70 errno = saved_errno;
71 if (errno) {
72 return -1;
73 }
74
75 return 0;
76 }
77
78 int 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
91 pid_t *ustctl_get_online_pids(void)
92 {
93 struct dirent *dirent;
94 DIR *dir;
95 unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
96
97 dir = opendir(SOCK_DIR);
98 if (!dir) {
99 return NULL;
100 }
101
102 pid_t *ret = (pid_t *) malloc(ret_size);
103
104 while ((dirent = readdir(dir))) {
105 if (!strcmp(dirent->d_name, ".") ||
106 !strcmp(dirent->d_name, "..")) {
107
108 continue;
109 }
110
111 if (dirent->d_type != DT_DIR &&
112 !!strcmp(dirent->d_name, "ust-consumer")) {
113
114 sscanf(dirent->d_name, "%u", (unsigned int *) &ret[i]);
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) {
121 ret_size += sizeof(pid_t);
122 ret = (pid_t *) realloc(ret, ret_size);
123 ++i;
124 }
125 }
126 }
127
128 ret[i] = 0; /* Array end */
129
130 if (ret[0] == 0) {
131 /* No PID at all */
132 free(ret);
133 return NULL;
134 }
135
136 closedir(dir);
137 return ret;
138 }
139
140 /**
141 * Sets marker state (USTCTL_MS_ON or USTCTL_MS_OFF).
142 *
143 * @param mn Marker name
144 * @param state Marker's new state
145 * @param pid Traced process ID
146 * @return 0 if successful, or errors {USTCTL_ERR_GEN, USTCTL_ERR_ARG}
147 */
148 int ustctl_set_marker_state(int sock, const char *trace, const char *channel,
149 const char *marker, int state)
150 {
151 struct ustcomm_header req_header, res_header;
152 struct ustcomm_marker_info marker_inf;
153 int result;
154
155 result = ustcomm_pack_marker_info(&req_header,
156 &marker_inf,
157 trace,
158 channel,
159 marker);
160 if (result < 0) {
161 errno = -result;
162 return -1;
163 }
164
165 req_header.command = state ? ENABLE_MARKER : DISABLE_MARKER;
166
167 return do_cmd(sock, &req_header, (char *)&marker_inf,
168 &res_header, NULL);
169 }
170
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 */
178 int ustctl_set_subbuf_size(int sock, const char *trace, const char *channel,
179 unsigned int subbuf_size)
180 {
181 struct ustcomm_header req_header, res_header;
182 struct ustcomm_channel_info ch_inf;
183 int result;
184
185 result = ustcomm_pack_channel_info(&req_header,
186 &ch_inf,
187 trace,
188 channel);
189 if (result < 0) {
190 errno = -result;
191 return -1;
192 }
193
194 req_header.command = SET_SUBBUF_SIZE;
195 ch_inf.subbuf_size = subbuf_size;
196
197 return do_cmd(sock, &req_header, (char *)&ch_inf,
198 &res_header, NULL);
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 */
208 int ustctl_set_subbuf_num(int sock, const char *trace, const char *channel,
209 unsigned int num)
210 {
211 struct ustcomm_header req_header, res_header;
212 struct ustcomm_channel_info ch_inf;
213 int result;
214
215 result = ustcomm_pack_channel_info(&req_header,
216 &ch_inf,
217 trace,
218 channel);
219 if (result < 0) {
220 errno = -result;
221 return -1;
222 }
223
224 req_header.command = SET_SUBBUF_NUM;
225 ch_inf.subbuf_num = num;
226
227 return do_cmd(sock, &req_header, (char *)&ch_inf,
228 &res_header, NULL);
229
230 }
231
232
233 static int ustctl_get_subbuf_num_size(int sock, const char *trace, const char *channel,
234 int *num, int *size)
235 {
236 struct ustcomm_header req_header, res_header;
237 struct ustcomm_channel_info ch_inf, *ch_inf_res;
238 int result;
239
240
241 result = ustcomm_pack_channel_info(&req_header,
242 &ch_inf,
243 trace,
244 channel);
245 if (result < 0) {
246 errno = -result;
247 return -1;
248 }
249
250 req_header.command = GET_SUBBUF_NUM_SIZE;
251
252 result = do_cmd(sock, &req_header, (char *)&ch_inf,
253 &res_header, (char **)&ch_inf_res);
254 if (result < 0) {
255 return -1;
256 }
257
258 *num = ch_inf_res->subbuf_num;
259 *size = ch_inf_res->subbuf_size;
260
261 free(ch_inf_res);
262
263 return 0;
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 */
273 int ustctl_get_subbuf_num(int sock, const char *trace, const char *channel)
274 {
275 int num, size, result;
276
277 result = ustctl_get_subbuf_num_size(sock, trace, channel,
278 &num, &size);
279 if (result < 0) {
280 errno = -result;
281 return -1;
282 }
283
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 */
294 int ustctl_get_subbuf_size(int sock, const char *trace, const char *channel)
295 {
296 int num, size, result;
297
298 result = ustctl_get_subbuf_num_size(sock, trace, channel,
299 &num, &size);
300 if (result < 0) {
301 errno = -result;
302 return -1;
303 }
304
305 return size;
306 }
307
308 static int do_trace_cmd(int sock, const char *trace, int command)
309 {
310 struct ustcomm_header req_header, res_header;
311 struct ustcomm_single_field trace_inf;
312 int result;
313
314 result = ustcomm_pack_single_field(&req_header,
315 &trace_inf,
316 trace);
317 if (result < 0) {
318 errno = -result;
319 return -1;
320 }
321
322 req_header.command = command;
323
324 return do_cmd(sock, &req_header, (char *)&trace_inf, &res_header, NULL);
325 }
326
327 /**
328 * Destroys an UST trace according to a PID.
329 *
330 * @param pid Traced process ID
331 * @return 0 if successful, or error USTCTL_ERR_GEN
332 */
333 int ustctl_destroy_trace(int sock, const char *trace)
334 {
335 return do_trace_cmd(sock, trace, DESTROY_TRACE);
336 }
337
338 /**
339 * Starts an UST trace (and setups it) according to a PID.
340 *
341 * @param pid Traced process ID
342 * @return 0 if successful, or error USTCTL_ERR_GEN
343 */
344 int ustctl_setup_and_start(int sock, const char *trace)
345 {
346 return do_trace_cmd(sock, trace, START);
347 }
348
349 /**
350 * Creates an UST trace according to a PID.
351 *
352 * @param pid Traced process ID
353 * @return 0 if successful, or error USTCTL_ERR_GEN
354 */
355 int ustctl_create_trace(int sock, const char *trace)
356 {
357 return do_trace_cmd(sock, trace, CREATE_TRACE);
358 }
359
360 /**
361 * Starts an UST trace according to a PID.
362 *
363 * @param pid Traced process ID
364 * @return 0 if successful, or error USTCTL_ERR_GEN
365 */
366 int ustctl_start_trace(int sock, const char *trace)
367 {
368 return do_trace_cmd(sock, trace, START_TRACE);
369 }
370
371 /**
372 * Alloc an UST trace according to a PID.
373 *
374 * @param pid Traced process ID
375 * @return 0 if successful, or error USTCTL_ERR_GEN
376 */
377 int ustctl_alloc_trace(int sock, const char *trace)
378 {
379 return do_trace_cmd(sock, trace, ALLOC_TRACE);
380 }
381
382 /**
383 * Stops an UST trace according to a PID.
384 *
385 * @param pid Traced process ID
386 * @return 0 if successful, or error USTCTL_ERR_GEN
387 */
388 int ustctl_stop_trace(int sock, const char *trace)
389 {
390 return do_trace_cmd(sock, trace, STOP_TRACE);
391 }
392
393 /**
394 * Counts newlines ('\n') in a string.
395 *
396 * @param str String to search in
397 * @return Total newlines count
398 */
399 unsigned int ustctl_count_nl(const char *str)
400 {
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
417 * @return 0 if successful, or error USTCTL_ERR_ARG
418 */
419 int ustctl_free_cmsf(struct marker_status *cmsf)
420 {
421 if (cmsf == NULL) {
422 return USTCTL_ERR_ARG;
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
441 * frees with `ustctl_free_cmsf')
442 * @param pid Targeted PID
443 * @return 0 if successful, or -1 on error
444 */
445 int ustctl_get_cmsf(int sock, struct marker_status **cmsf)
446 {
447 struct ustcomm_header req_header, res_header;
448 char *big_str = NULL;
449 int result;
450 struct marker_status *tmp_cmsf = NULL;
451 unsigned int i = 0, cmsf_ind = 0;
452
453 if (cmsf == NULL) {
454 return -1;
455 }
456
457 req_header.command = LIST_MARKERS;
458 req_header.size = 0;
459
460 result = ustcomm_send(sock, &req_header, NULL);
461 if (result <= 0) {
462 PERROR("error while requesting markers list");
463 return -1;
464 }
465
466 result = ustcomm_recv_alloc(sock, &res_header, &big_str);
467 if (result <= 0) {
468 ERR("error while receiving markers list");
469 return -1;
470 }
471
472 tmp_cmsf = (struct marker_status *) zmalloc(sizeof(struct marker_status) *
473 (ustctl_count_nl(big_str) + 1));
474 if (tmp_cmsf == NULL) {
475 ERR("Failed to allocate CMSF array");
476 return -1;
477 }
478
479 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
480 while (big_str[i] != '\0') {
481 char state;
482
483 sscanf(big_str + i, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
484 &tmp_cmsf[cmsf_ind].channel,
485 &tmp_cmsf[cmsf_ind].marker,
486 &state,
487 &tmp_cmsf[cmsf_ind].fs);
488 tmp_cmsf[cmsf_ind].state = (state == USTCTL_MS_CHR_ON ?
489 USTCTL_MS_ON : USTCTL_MS_OFF); /* Marker state */
490
491 while (big_str[i] != '\n') {
492 ++i; /* Go to next '\n' */
493 }
494 ++i; /* Skip current pointed '\n' */
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
507 /**
508 * Frees a TES array.
509 *
510 * @param tes TES array to free
511 * @return 0 if successful, or error USTCTL_ERR_ARG
512 */
513 int ustctl_free_tes(struct trace_event_status *tes)
514 {
515 if (tes == NULL) {
516 return USTCTL_ERR_ARG;
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
533 * frees with `ustctl_free_tes')
534 * @param pid Targeted PID
535 * @return 0 if successful, or -1 on error
536 */
537 int ustctl_get_tes(int sock, struct trace_event_status **tes)
538 {
539 struct ustcomm_header req_header, res_header;
540 char *big_str = NULL;
541 int result;
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
549 req_header.command = LIST_TRACE_EVENTS;
550 req_header.size = 0;
551
552 result = ustcomm_send(sock, &req_header, NULL);
553 if (result != 1) {
554 ERR("error while requesting trace_event list");
555 return -1;
556 }
557
558 result = ustcomm_recv_alloc(sock, &res_header, &big_str);
559 if (result != 1) {
560 ERR("error while receiving markers list");
561 return -1;
562 }
563
564 tmp_tes = (struct trace_event_status *)
565 zmalloc(sizeof(struct trace_event_status) *
566 (ustctl_count_nl(big_str) + 1));
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') {
574 sscanf(big_str + i, "trace_event: %a[^\n]",
575 &tmp_tes[tes_ind].name);
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
590 /**
591 * Set sock path
592 *
593 * @param sock_path Sock path
594 * @param pid Traced process ID
595 * @return 0 if successful, or error
596 */
597 int ustctl_set_sock_path(int sock, const char *sock_path)
598 {
599 int result;
600 struct ustcomm_header req_header, res_header;
601 struct ustcomm_single_field sock_path_msg;
602
603 result = ustcomm_pack_single_field(&req_header,
604 &sock_path_msg,
605 sock_path);
606 if (result < 0) {
607 errno = -result;
608 return -1;
609 }
610
611 req_header.command = SET_SOCK_PATH;
612
613 return do_cmd(sock, &req_header, (char *)&sock_path_msg,
614 &res_header, NULL);
615 }
616
617 /**
618 * Get sock path
619 *
620 * @param sock_path Pointer to where the sock path will be returned
621 * @param pid Traced process ID
622 * @return 0 if successful, or error
623 */
624 int ustctl_get_sock_path(int sock, char **sock_path)
625 {
626 int result;
627 struct ustcomm_header req_header, res_header;
628 struct ustcomm_single_field *sock_path_msg;
629
630 req_header.command = GET_SOCK_PATH;
631 req_header.size = 0;
632
633 result = do_cmd(sock, &req_header, NULL, &res_header,
634 (char **)&sock_path_msg);
635 if (result < 0) {
636 return -1;
637 }
638
639 result = ustcomm_unpack_single_field(sock_path_msg);
640 if (result < 0) {
641 return result;
642 }
643
644 *sock_path = strdup(sock_path_msg->field);
645
646 free(sock_path_msg);
647
648 return 0;
649 }
650
651 int ustctl_force_switch(int sock, const char *trace)
652 {
653 struct ustcomm_header req_header, res_header;
654
655 req_header.command = FORCE_SUBBUF_SWITCH;
656 req_header.size = 0;
657
658 return do_cmd(sock, &req_header, NULL, &res_header, NULL);
659 }
This page took 0.043262 seconds and 4 git commands to generate.