1 /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette
2 * Copyright (C) 2011 Ericsson AB
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.
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.
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
29 #include "ust/ustctl.h"
32 static int do_cmd(int sock
,
33 const struct ustcomm_header
*req_header
,
35 struct ustcomm_header
*res_header
,
38 int result
, saved_errno
= 0;
41 recv_buf
= zmalloc(USTCOMM_BUFFER_SIZE
);
47 result
= ustcomm_req(sock
, req_header
, req_data
, res_header
, recv_buf
);
49 saved_errno
= -res_header
->result
;
50 if (res_header
->size
== 0 || saved_errno
> 0) {
60 ERR("ustcomm req failed");
62 saved_errno
= ENOTCONN
;
64 saved_errno
= -result
;
78 int ustctl_connect_pid(pid_t pid
)
82 if (ustcomm_connect_app(pid
, &sock
)) {
83 ERR("could not connect to PID %u", (unsigned int) pid
);
91 static int realloc_pid_list(pid_t
**pid_list
, unsigned int *pid_list_size
)
94 unsigned int new_pid_list_size
= 2 * *pid_list_size
;
96 new_pid_list
= realloc(*pid_list
,
97 new_pid_list_size
* sizeof(pid_t
));
102 *pid_list
= new_pid_list
;
103 *pid_list_size
= new_pid_list_size
;
108 static int get_pids_in_dir(DIR *dir
, pid_t
**pid_list
,
109 unsigned int *pid_list_index
,
110 unsigned int *pid_list_size
)
112 struct dirent
*dirent
;
115 while ((dirent
= readdir(dir
))) {
116 if (!strcmp(dirent
->d_name
, ".") ||
117 !strcmp(dirent
->d_name
, "..") ||
118 !strcmp(dirent
->d_name
, "ust-consumer") ||
119 dirent
->d_type
== DT_DIR
) {
125 read_pid
= strtol(dirent
->d_name
, NULL
, 10);
131 * FIXME: Here we previously called pid_is_online, which
132 * always returned 1, now I replaced it with just 1.
133 * We need to figure out an intelligent way of solving
134 * this, maybe connect-disconnect.
138 (*pid_list
)[(*pid_list_index
)++] = read_pid
;
140 if (*pid_list_index
== *pid_list_size
) {
141 if (realloc_pid_list(pid_list
, pid_list_size
)) {
148 (*pid_list
)[*pid_list_index
] = 0; /* Array end */
153 static pid_t
*get_pids_non_root(void)
157 unsigned int pid_list_index
= 0, pid_list_size
= 1;
158 pid_t
*pid_list
= NULL
;
160 dir_name
= ustcomm_user_sock_dir();
165 dir
= opendir(dir_name
);
170 pid_list
= malloc(pid_list_size
* sizeof(pid_t
));
175 if (get_pids_in_dir(dir
, &pid_list
, &pid_list_index
, &pid_list_size
)) {
176 /* if any errors are encountered, force freeing of the list */
189 static pid_t
*get_pids_root(void)
193 unsigned int pid_list_index
= 0, pid_list_size
= 1;
194 pid_t
*pid_list
= NULL
;
195 struct dirent
*dirent
;
198 tmp_dir
= opendir(USER_TMP_DIR
);
203 pid_list
= malloc(pid_list_size
* sizeof(pid_t
));
208 while ((dirent
= readdir(tmp_dir
))) {
209 /* Compare the dir to check for the USER_SOCK_DIR_BASE prefix */
210 if (!strncmp(dirent
->d_name
, USER_SOCK_DIR_BASE
,
211 strlen(USER_SOCK_DIR_BASE
))) {
213 if (asprintf(&dir_name
, USER_TMP_DIR
"/%s",
214 dirent
->d_name
) < 0) {
218 dir
= opendir(dir_name
);
226 result
= get_pids_in_dir(dir
, &pid_list
, &pid_list_index
,
233 * if any errors are encountered,
234 * force freeing of the list
248 pid_t
*ustctl_get_online_pids(void)
253 pid_list
= get_pids_non_root();
255 pid_list
= get_pids_root();
258 if (pid_list
&& pid_list
[0] == 0) {
268 * Sets ust_marker state (USTCTL_MS_ON or USTCTL_MS_OFF).
270 * @param mn Marker name
271 * @param state Marker's new state
272 * @param pid Traced process ID
273 * @return 0 if successful, or errors {USTCTL_ERR_GEN, USTCTL_ERR_ARG}
275 int ustctl_set_ust_marker_state(int sock
, const char *trace
, const char *channel
,
276 const char *ust_marker
, int state
)
278 struct ustcomm_header req_header
, res_header
;
279 struct ustcomm_ust_marker_info ust_marker_inf
;
282 result
= ustcomm_pack_ust_marker_info(&req_header
,
292 req_header
.command
= state
? ENABLE_MARKER
: DISABLE_MARKER
;
294 return do_cmd(sock
, &req_header
, (char *)&ust_marker_inf
,
299 * Set subbuffer size.
301 * @param channel_size Channel name and size
302 * @param pid Traced process ID
303 * @return 0 if successful, or error
305 int ustctl_set_subbuf_size(int sock
, const char *trace
, const char *channel
,
306 unsigned int subbuf_size
)
308 struct ustcomm_header req_header
, res_header
;
309 struct ustcomm_channel_info ch_inf
;
312 result
= ustcomm_pack_channel_info(&req_header
,
321 req_header
.command
= SET_SUBBUF_SIZE
;
322 ch_inf
.subbuf_size
= subbuf_size
;
324 return do_cmd(sock
, &req_header
, (char *)&ch_inf
,
331 * @param channel_num Channel name and num
332 * @param pid Traced process ID
333 * @return 0 if successful, or error
335 int ustctl_set_subbuf_num(int sock
, const char *trace
, const char *channel
,
338 struct ustcomm_header req_header
, res_header
;
339 struct ustcomm_channel_info ch_inf
;
342 result
= ustcomm_pack_channel_info(&req_header
,
351 req_header
.command
= SET_SUBBUF_NUM
;
352 ch_inf
.subbuf_num
= num
;
354 return do_cmd(sock
, &req_header
, (char *)&ch_inf
,
360 static int ustctl_get_subbuf_num_size(int sock
, const char *trace
, const char *channel
,
363 struct ustcomm_header req_header
, res_header
;
364 struct ustcomm_channel_info ch_inf
, *ch_inf_res
;
368 result
= ustcomm_pack_channel_info(&req_header
,
377 req_header
.command
= GET_SUBBUF_NUM_SIZE
;
379 result
= do_cmd(sock
, &req_header
, (char *)&ch_inf
,
380 &res_header
, (char **)&ch_inf_res
);
385 *num
= ch_inf_res
->subbuf_num
;
386 *size
= ch_inf_res
->subbuf_size
;
396 * @param channel Channel name
397 * @param pid Traced process ID
398 * @return subbuf cnf if successful, or error
400 int ustctl_get_subbuf_num(int sock
, const char *trace
, const char *channel
)
402 int num
, size
, result
;
404 result
= ustctl_get_subbuf_num_size(sock
, trace
, channel
,
415 * Get subbuffer size.
417 * @param channel Channel name
418 * @param pid Traced process ID
419 * @return subbuf size if successful, or error
421 int ustctl_get_subbuf_size(int sock
, const char *trace
, const char *channel
)
423 int num
, size
, result
;
425 result
= ustctl_get_subbuf_num_size(sock
, trace
, channel
,
435 static int do_trace_cmd(int sock
, const char *trace
, int command
)
437 struct ustcomm_header req_header
, res_header
;
438 struct ustcomm_single_field trace_inf
;
441 result
= ustcomm_pack_single_field(&req_header
,
449 req_header
.command
= command
;
451 return do_cmd(sock
, &req_header
, (char *)&trace_inf
, &res_header
, NULL
);
455 * Destroys an UST trace according to a PID.
457 * @param pid Traced process ID
458 * @return 0 if successful, or error USTCTL_ERR_GEN
460 int ustctl_destroy_trace(int sock
, const char *trace
)
462 return do_trace_cmd(sock
, trace
, DESTROY_TRACE
);
466 * Starts an UST trace (and setups it) according to a PID.
468 * @param pid Traced process ID
469 * @return 0 if successful, or error USTCTL_ERR_GEN
471 int ustctl_setup_and_start(int sock
, const char *trace
)
473 return do_trace_cmd(sock
, trace
, START
);
477 * Creates an UST trace according to a PID.
479 * @param pid Traced process ID
480 * @return 0 if successful, or error USTCTL_ERR_GEN
482 int ustctl_create_trace(int sock
, const char *trace
)
484 return do_trace_cmd(sock
, trace
, CREATE_TRACE
);
488 * Starts an UST trace according to a PID.
490 * @param pid Traced process ID
491 * @return 0 if successful, or error USTCTL_ERR_GEN
493 int ustctl_start_trace(int sock
, const char *trace
)
495 return do_trace_cmd(sock
, trace
, START_TRACE
);
499 * Alloc an UST trace according to a PID.
501 * @param pid Traced process ID
502 * @return 0 if successful, or error USTCTL_ERR_GEN
504 int ustctl_alloc_trace(int sock
, const char *trace
)
506 return do_trace_cmd(sock
, trace
, ALLOC_TRACE
);
510 int ustctl_force_switch(int sock
, const char *trace
)
512 return do_trace_cmd(sock
, trace
, FORCE_SUBBUF_SWITCH
);
516 * Stops an UST trace according to a PID.
518 * @param pid Traced process ID
519 * @return 0 if successful, or error USTCTL_ERR_GEN
521 int ustctl_stop_trace(int sock
, const char *trace
)
523 return do_trace_cmd(sock
, trace
, STOP_TRACE
);
527 * Counts newlines ('\n') in a string.
529 * @param str String to search in
530 * @return Total newlines count
532 unsigned int ustctl_count_nl(const char *str
)
534 unsigned int i
= 0, tot
= 0;
536 while (str
[i
] != '\0') {
537 if (str
[i
] == '\n') {
547 * Frees a CMSF array.
549 * @param cmsf CMSF array to free
550 * @return 0 if successful, or error USTCTL_ERR_ARG
552 int ustctl_free_cmsf(struct ust_marker_status
*cmsf
)
555 return USTCTL_ERR_ARG
;
559 while (cmsf
[i
].channel
!= NULL
) {
560 free(cmsf
[i
].channel
);
561 free(cmsf
[i
].ust_marker
);
571 * Gets channel/ust_marker/state/format string for a given PID.
573 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
574 * frees with `ustctl_free_cmsf')
575 * @param pid Targeted PID
576 * @return 0 if successful, or -1 on error
578 int ustctl_get_cmsf(int sock
, struct ust_marker_status
**cmsf
)
580 struct ustcomm_header req_header
, res_header
;
581 char *big_str
= NULL
;
583 struct ust_marker_status
*tmp_cmsf
= NULL
;
584 unsigned int i
= 0, cmsf_ind
= 0;
590 req_header
.command
= LIST_MARKERS
;
593 result
= ustcomm_send(sock
, &req_header
, NULL
);
595 PERROR("error while requesting ust_marker list");
599 result
= ustcomm_recv_alloc(sock
, &res_header
, &big_str
);
601 ERR("error while receiving ust_marker list");
605 tmp_cmsf
= (struct ust_marker_status
*) zmalloc(sizeof(struct ust_marker_status
) *
606 (ustctl_count_nl(big_str
) + 1));
607 if (tmp_cmsf
== NULL
) {
608 ERR("Failed to allocate CMSF array");
612 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
613 while (big_str
[i
] != '\0') {
616 sscanf(big_str
+ i
, "ust_marker: %a[^/]/%a[^ ] %c %a[^\n]",
617 &tmp_cmsf
[cmsf_ind
].channel
,
618 &tmp_cmsf
[cmsf_ind
].ust_marker
,
620 &tmp_cmsf
[cmsf_ind
].fs
);
621 tmp_cmsf
[cmsf_ind
].state
= (state
== USTCTL_MS_CHR_ON
?
622 USTCTL_MS_ON
: USTCTL_MS_OFF
); /* Marker state */
624 while (big_str
[i
] != '\n') {
625 ++i
; /* Go to next '\n' */
627 ++i
; /* Skip current pointed '\n' */
630 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
631 tmp_cmsf
[cmsf_ind
].ust_marker
= NULL
;
632 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
643 * @param tes TES array to free
644 * @return 0 if successful, or error USTCTL_ERR_ARG
646 int ustctl_free_tes(struct trace_event_status
*tes
)
649 return USTCTL_ERR_ARG
;
653 while (tes
[i
].name
!= NULL
) {
663 * Gets trace_events string for a given PID.
665 * @param tes Pointer to TES array to be filled (callee allocates, caller
666 * frees with `ustctl_free_tes')
667 * @param pid Targeted PID
668 * @return 0 if successful, or -1 on error
670 int ustctl_get_tes(int sock
, struct trace_event_status
**tes
)
672 struct ustcomm_header req_header
, res_header
;
673 char *big_str
= NULL
;
675 struct trace_event_status
*tmp_tes
= NULL
;
676 unsigned int i
= 0, tes_ind
= 0;
682 req_header
.command
= LIST_TRACE_EVENTS
;
685 result
= ustcomm_send(sock
, &req_header
, NULL
);
687 ERR("error while requesting trace_event list");
691 result
= ustcomm_recv_alloc(sock
, &res_header
, &big_str
);
693 ERR("error while receiving ust_marker list");
697 tmp_tes
= (struct trace_event_status
*)
698 zmalloc(sizeof(struct trace_event_status
) *
699 (ustctl_count_nl(big_str
) + 1));
700 if (tmp_tes
== NULL
) {
701 ERR("Failed to allocate TES array");
705 /* Parse received reply string (format: "[name]"): */
706 while (big_str
[i
] != '\0') {
707 sscanf(big_str
+ i
, "trace_event: %a[^\n]",
708 &tmp_tes
[tes_ind
].name
);
709 while (big_str
[i
] != '\n') {
710 ++i
; /* Go to next '\n' */
712 ++i
; /* Skip current pointed '\n' */
715 tmp_tes
[tes_ind
].name
= NULL
;
726 * @param sock_path Sock path
727 * @param pid Traced process ID
728 * @return 0 if successful, or error
730 int ustctl_set_sock_path(int sock
, const char *sock_path
)
733 struct ustcomm_header req_header
, res_header
;
734 struct ustcomm_single_field sock_path_msg
;
736 result
= ustcomm_pack_single_field(&req_header
,
744 req_header
.command
= SET_SOCK_PATH
;
746 return do_cmd(sock
, &req_header
, (char *)&sock_path_msg
,
753 * @param sock_path Pointer to where the sock path will be returned
754 * @param pid Traced process ID
755 * @return 0 if successful, or error
757 int ustctl_get_sock_path(int sock
, char **sock_path
)
760 struct ustcomm_header req_header
, res_header
;
761 struct ustcomm_single_field
*sock_path_msg
;
763 req_header
.command
= GET_SOCK_PATH
;
766 result
= do_cmd(sock
, &req_header
, NULL
, &res_header
,
767 (char **)&sock_path_msg
);
772 result
= ustcomm_unpack_single_field(sock_path_msg
);
777 *sock_path
= strdup(sock_path_msg
->field
);