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 pid_t
*ustctl_get_online_pids(void)
93 struct dirent
*dirent
;
95 unsigned int ret_size
= 1 * sizeof(pid_t
), i
= 0;
97 dir
= opendir(SOCK_DIR
);
102 pid_t
*ret
= (pid_t
*) malloc(ret_size
);
104 while ((dirent
= readdir(dir
))) {
105 if (!strcmp(dirent
->d_name
, ".") ||
106 !strcmp(dirent
->d_name
, "..")) {
111 if (dirent
->d_type
!= DT_DIR
&&
112 !!strcmp(dirent
->d_name
, "ust-consumer")) {
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.
121 ret_size
+= sizeof(pid_t
);
122 ret
= (pid_t
*) realloc(ret
, ret_size
);
128 ret
[i
] = 0; /* Array end */
141 * Sets marker state (USTCTL_MS_ON or USTCTL_MS_OFF).
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}
148 int ustctl_set_marker_state(int sock
, const char *trace
, const char *channel
,
149 const char *marker
, int state
)
151 struct ustcomm_header req_header
, res_header
;
152 struct ustcomm_marker_info marker_inf
;
155 result
= ustcomm_pack_marker_info(&req_header
,
165 req_header
.command
= state
? ENABLE_MARKER
: DISABLE_MARKER
;
167 return do_cmd(sock
, &req_header
, (char *)&marker_inf
,
172 * Set subbuffer size.
174 * @param channel_size Channel name and size
175 * @param pid Traced process ID
176 * @return 0 if successful, or error
178 int ustctl_set_subbuf_size(int sock
, const char *trace
, const char *channel
,
179 unsigned int subbuf_size
)
181 struct ustcomm_header req_header
, res_header
;
182 struct ustcomm_channel_info ch_inf
;
185 result
= ustcomm_pack_channel_info(&req_header
,
194 req_header
.command
= SET_SUBBUF_SIZE
;
195 ch_inf
.subbuf_size
= subbuf_size
;
197 return do_cmd(sock
, &req_header
, (char *)&ch_inf
,
204 * @param channel_num Channel name and num
205 * @param pid Traced process ID
206 * @return 0 if successful, or error
208 int ustctl_set_subbuf_num(int sock
, const char *trace
, const char *channel
,
211 struct ustcomm_header req_header
, res_header
;
212 struct ustcomm_channel_info ch_inf
;
215 result
= ustcomm_pack_channel_info(&req_header
,
224 req_header
.command
= SET_SUBBUF_NUM
;
225 ch_inf
.subbuf_num
= num
;
227 return do_cmd(sock
, &req_header
, (char *)&ch_inf
,
233 static int ustctl_get_subbuf_num_size(int sock
, const char *trace
, const char *channel
,
236 struct ustcomm_header req_header
, res_header
;
237 struct ustcomm_channel_info ch_inf
, *ch_inf_res
;
241 result
= ustcomm_pack_channel_info(&req_header
,
250 req_header
.command
= GET_SUBBUF_NUM_SIZE
;
252 result
= do_cmd(sock
, &req_header
, (char *)&ch_inf
,
253 &res_header
, (char **)&ch_inf_res
);
258 *num
= ch_inf_res
->subbuf_num
;
259 *size
= ch_inf_res
->subbuf_size
;
269 * @param channel Channel name
270 * @param pid Traced process ID
271 * @return subbuf cnf if successful, or error
273 int ustctl_get_subbuf_num(int sock
, const char *trace
, const char *channel
)
275 int num
, size
, result
;
277 result
= ustctl_get_subbuf_num_size(sock
, trace
, channel
,
288 * Get subbuffer size.
290 * @param channel Channel name
291 * @param pid Traced process ID
292 * @return subbuf size if successful, or error
294 int ustctl_get_subbuf_size(int sock
, const char *trace
, const char *channel
)
296 int num
, size
, result
;
298 result
= ustctl_get_subbuf_num_size(sock
, trace
, channel
,
308 static int do_trace_cmd(int sock
, const char *trace
, int command
)
310 struct ustcomm_header req_header
, res_header
;
311 struct ustcomm_single_field trace_inf
;
314 result
= ustcomm_pack_single_field(&req_header
,
322 req_header
.command
= command
;
324 return do_cmd(sock
, &req_header
, (char *)&trace_inf
, &res_header
, NULL
);
328 * Destroys an UST trace according to a PID.
330 * @param pid Traced process ID
331 * @return 0 if successful, or error USTCTL_ERR_GEN
333 int ustctl_destroy_trace(int sock
, const char *trace
)
335 return do_trace_cmd(sock
, trace
, DESTROY_TRACE
);
339 * Starts an UST trace (and setups it) according to a PID.
341 * @param pid Traced process ID
342 * @return 0 if successful, or error USTCTL_ERR_GEN
344 int ustctl_setup_and_start(int sock
, const char *trace
)
346 return do_trace_cmd(sock
, trace
, START
);
350 * Creates an UST trace according to a PID.
352 * @param pid Traced process ID
353 * @return 0 if successful, or error USTCTL_ERR_GEN
355 int ustctl_create_trace(int sock
, const char *trace
)
357 return do_trace_cmd(sock
, trace
, CREATE_TRACE
);
361 * Starts an UST trace according to a PID.
363 * @param pid Traced process ID
364 * @return 0 if successful, or error USTCTL_ERR_GEN
366 int ustctl_start_trace(int sock
, const char *trace
)
368 return do_trace_cmd(sock
, trace
, START_TRACE
);
372 * Alloc an UST trace according to a PID.
374 * @param pid Traced process ID
375 * @return 0 if successful, or error USTCTL_ERR_GEN
377 int ustctl_alloc_trace(int sock
, const char *trace
)
379 return do_trace_cmd(sock
, trace
, ALLOC_TRACE
);
383 * Stops an UST trace according to a PID.
385 * @param pid Traced process ID
386 * @return 0 if successful, or error USTCTL_ERR_GEN
388 int ustctl_stop_trace(int sock
, const char *trace
)
390 return do_trace_cmd(sock
, trace
, STOP_TRACE
);
394 * Counts newlines ('\n') in a string.
396 * @param str String to search in
397 * @return Total newlines count
399 unsigned int ustctl_count_nl(const char *str
)
401 unsigned int i
= 0, tot
= 0;
403 while (str
[i
] != '\0') {
404 if (str
[i
] == '\n') {
414 * Frees a CMSF array.
416 * @param cmsf CMSF array to free
417 * @return 0 if successful, or error USTCTL_ERR_ARG
419 int ustctl_free_cmsf(struct marker_status
*cmsf
)
422 return USTCTL_ERR_ARG
;
426 while (cmsf
[i
].channel
!= NULL
) {
427 free(cmsf
[i
].channel
);
428 free(cmsf
[i
].marker
);
438 * Gets channel/marker/state/format string for a given PID.
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
445 int ustctl_get_cmsf(int sock
, struct marker_status
**cmsf
)
447 struct ustcomm_header req_header
, res_header
;
448 char *big_str
= NULL
;
450 struct marker_status
*tmp_cmsf
= NULL
;
451 unsigned int i
= 0, cmsf_ind
= 0;
457 req_header
.command
= LIST_MARKERS
;
460 result
= ustcomm_send(sock
, &req_header
, NULL
);
462 PERROR("error while requesting markers list");
466 result
= ustcomm_recv_alloc(sock
, &res_header
, &big_str
);
468 ERR("error while receiving markers list");
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");
479 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
480 while (big_str
[i
] != '\0') {
483 sscanf(big_str
+ i
, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
484 &tmp_cmsf
[cmsf_ind
].channel
,
485 &tmp_cmsf
[cmsf_ind
].marker
,
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 */
491 while (big_str
[i
] != '\n') {
492 ++i
; /* Go to next '\n' */
494 ++i
; /* Skip current pointed '\n' */
497 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
498 tmp_cmsf
[cmsf_ind
].marker
= NULL
;
499 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
510 * @param tes TES array to free
511 * @return 0 if successful, or error USTCTL_ERR_ARG
513 int ustctl_free_tes(struct trace_event_status
*tes
)
516 return USTCTL_ERR_ARG
;
520 while (tes
[i
].name
!= NULL
) {
530 * Gets trace_events string for a given PID.
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
537 int ustctl_get_tes(int sock
, struct trace_event_status
**tes
)
539 struct ustcomm_header req_header
, res_header
;
540 char *big_str
= NULL
;
542 struct trace_event_status
*tmp_tes
= NULL
;
543 unsigned int i
= 0, tes_ind
= 0;
549 req_header
.command
= LIST_TRACE_EVENTS
;
552 result
= ustcomm_send(sock
, &req_header
, NULL
);
554 ERR("error while requesting trace_event list");
558 result
= ustcomm_recv_alloc(sock
, &res_header
, &big_str
);
560 ERR("error while receiving markers list");
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");
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' */
579 ++i
; /* Skip current pointed '\n' */
582 tmp_tes
[tes_ind
].name
= NULL
;
593 * @param sock_path Sock path
594 * @param pid Traced process ID
595 * @return 0 if successful, or error
597 int ustctl_set_sock_path(int sock
, const char *sock_path
)
600 struct ustcomm_header req_header
, res_header
;
601 struct ustcomm_single_field sock_path_msg
;
603 result
= ustcomm_pack_single_field(&req_header
,
611 req_header
.command
= SET_SOCK_PATH
;
613 return do_cmd(sock
, &req_header
, (char *)&sock_path_msg
,
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
624 int ustctl_get_sock_path(int sock
, char **sock_path
)
627 struct ustcomm_header req_header
, res_header
;
628 struct ustcomm_single_field
*sock_path_msg
;
630 req_header
.command
= GET_SOCK_PATH
;
633 result
= do_cmd(sock
, &req_header
, NULL
, &res_header
,
634 (char **)&sock_path_msg
);
639 result
= ustcomm_unpack_single_field(sock_path_msg
);
644 *sock_path
= strdup(sock_path_msg
->field
);
651 int ustctl_force_switch(int sock
, const char *trace
)
653 struct ustcomm_header req_header
, res_header
;
655 req_header
.command
= FORCE_SUBBUF_SWITCH
;
658 return do_cmd(sock
, &req_header
, NULL
, &res_header
, NULL
);