1 /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "ust/ustcmd.h"
31 static int do_cmd(const pid_t pid
,
32 const struct ustcomm_header
*req_header
,
34 struct ustcomm_header
*res_header
,
37 int app_fd
, result
, saved_errno
= 0;
40 if (ustcomm_connect_app(pid
, &app_fd
)) {
41 ERR("could not connect to PID %u", (unsigned int) pid
);
46 recv_buf
= zmalloc(USTCOMM_BUFFER_SIZE
);
52 result
= ustcomm_req(app_fd
, req_header
, req_data
, res_header
, recv_buf
);
54 saved_errno
= -res_header
->result
;
55 if (res_header
->size
== 0 || saved_errno
> 0) {
65 ERR("ustcomm req failed");
67 saved_errno
= ENOTCONN
;
69 saved_errno
= -result
;
86 pid_t
*ustcmd_get_online_pids(void)
88 struct dirent
*dirent
;
90 unsigned int ret_size
= 1 * sizeof(pid_t
), i
= 0;
92 dir
= opendir(SOCK_DIR
);
97 pid_t
*ret
= (pid_t
*) malloc(ret_size
);
99 while ((dirent
= readdir(dir
))) {
100 if (!strcmp(dirent
->d_name
, ".") ||
101 !strcmp(dirent
->d_name
, "..")) {
106 if (dirent
->d_type
!= DT_DIR
&&
107 !!strcmp(dirent
->d_name
, "ustd")) {
109 sscanf(dirent
->d_name
, "%u", (unsigned int *) &ret
[i
]);
110 /* FIXME: Here we previously called pid_is_online, which
111 * always returned 1, now I replaced it with just 1.
112 * We need to figure out an intelligent way of solving
113 * this, maybe connect-disconnect.
116 ret_size
+= sizeof(pid_t
);
117 ret
= (pid_t
*) realloc(ret
, ret_size
);
123 ret
[i
] = 0; /* Array end */
136 * Sets marker state (USTCMD_MS_ON or USTCMD_MS_OFF).
138 * @param mn Marker name
139 * @param state Marker's new state
140 * @param pid Traced process ID
141 * @return 0 if successful, or errors {USTCMD_ERR_GEN, USTCMD_ERR_ARG}
143 int ustcmd_set_marker_state(const char *channel
, const char *marker
,
144 int state
, pid_t pid
)
146 struct ustcomm_header req_header
, res_header
;
147 struct ustcomm_marker_info marker_inf
;
150 result
= ustcomm_pack_marker_info(&req_header
,
159 req_header
.command
= state
? ENABLE_MARKER
: DISABLE_MARKER
;
161 return do_cmd(pid
, &req_header
, (char *)&marker_inf
,
166 * Set subbuffer size.
168 * @param channel_size Channel name and size
169 * @param pid Traced process ID
170 * @return 0 if successful, or error
172 int ustcmd_set_subbuf_size(const char *channel
, unsigned int subbuf_size
,
175 struct ustcomm_header req_header
, res_header
;
176 struct ustcomm_channel_info ch_inf
;
179 result
= ustcomm_pack_channel_info(&req_header
,
187 req_header
.command
= SET_SUBBUF_SIZE
;
188 ch_inf
.subbuf_size
= subbuf_size
;
190 return do_cmd(pid
, &req_header
, (char *)&ch_inf
,
197 * @param channel_num Channel name and num
198 * @param pid Traced process ID
199 * @return 0 if successful, or error
201 int ustcmd_set_subbuf_num(const char *channel
, unsigned int num
,
204 struct ustcomm_header req_header
, res_header
;
205 struct ustcomm_channel_info ch_inf
;
208 result
= ustcomm_pack_channel_info(&req_header
,
216 req_header
.command
= SET_SUBBUF_NUM
;
217 ch_inf
.subbuf_num
= num
;
219 return do_cmd(pid
, &req_header
, (char *)&ch_inf
,
224 static int ustcmd_get_subbuf_num_size(const char *channel
, pid_t pid
,
227 struct ustcomm_header req_header
, res_header
;
228 struct ustcomm_channel_info ch_inf
, *ch_inf_res
;
232 result
= ustcomm_pack_channel_info(&req_header
,
240 req_header
.command
= GET_SUBBUF_NUM_SIZE
;
242 result
= do_cmd(pid
, &req_header
, (char *)&ch_inf
,
243 &res_header
, (char **)&ch_inf_res
);
248 *num
= ch_inf_res
->subbuf_num
;
249 *size
= ch_inf_res
->subbuf_size
;
259 * @param channel Channel name
260 * @param pid Traced process ID
261 * @return subbuf cnf if successful, or error
263 int ustcmd_get_subbuf_num(const char *channel
, pid_t pid
)
265 int num
, size
, result
;
267 result
= ustcmd_get_subbuf_num_size(channel
, pid
,
278 * Get subbuffer size.
280 * @param channel Channel name
281 * @param pid Traced process ID
282 * @return subbuf size if successful, or error
284 int ustcmd_get_subbuf_size(const char *channel
, pid_t pid
)
286 int num
, size
, result
;
288 result
= ustcmd_get_subbuf_num_size(channel
, pid
,
299 * Destroys an UST trace according to a PID.
301 * @param pid Traced process ID
302 * @return 0 if successful, or error USTCMD_ERR_GEN
304 int ustcmd_destroy_trace(pid_t pid
)
306 struct ustcomm_header req_header
, res_header
;
308 req_header
.command
= DESTROY_TRACE
;
311 return do_cmd(pid
, &req_header
, NULL
, &res_header
, NULL
);
315 * Starts an UST trace (and setups it) according to a PID.
317 * @param pid Traced process ID
318 * @return 0 if successful, or error USTCMD_ERR_GEN
320 int ustcmd_setup_and_start(pid_t pid
)
322 struct ustcomm_header req_header
, res_header
;
324 req_header
.command
= START
;
327 return do_cmd(pid
, &req_header
, NULL
, &res_header
, NULL
);
331 * Creates an UST trace according to a PID.
333 * @param pid Traced process ID
334 * @return 0 if successful, or error USTCMD_ERR_GEN
336 int ustcmd_create_trace(pid_t pid
)
338 struct ustcomm_header req_header
, res_header
;
340 req_header
.command
= CREATE_TRACE
;
343 return do_cmd(pid
, &req_header
, NULL
, &res_header
, NULL
);
347 * Starts an UST trace according to a PID.
349 * @param pid Traced process ID
350 * @return 0 if successful, or error USTCMD_ERR_GEN
352 int ustcmd_start_trace(pid_t pid
)
354 struct ustcomm_header req_header
, res_header
;
356 req_header
.command
= START_TRACE
;
359 return do_cmd(pid
, &req_header
, NULL
, &res_header
, NULL
);
363 * Alloc an UST trace according to a PID.
365 * @param pid Traced process ID
366 * @return 0 if successful, or error USTCMD_ERR_GEN
368 int ustcmd_alloc_trace(pid_t pid
)
370 struct ustcomm_header req_header
, res_header
;
372 req_header
.command
= ALLOC_TRACE
;
375 return do_cmd(pid
, &req_header
, NULL
, &res_header
, NULL
);
379 * Stops an UST trace according to a PID.
381 * @param pid Traced process ID
382 * @return 0 if successful, or error USTCMD_ERR_GEN
384 int ustcmd_stop_trace(pid_t pid
)
386 struct ustcomm_header req_header
, res_header
;
388 req_header
.command
= STOP_TRACE
;
391 return do_cmd(pid
, &req_header
, NULL
, &res_header
, NULL
);
395 * Counts newlines ('\n') in a string.
397 * @param str String to search in
398 * @return Total newlines count
400 unsigned int ustcmd_count_nl(const char *str
)
402 unsigned int i
= 0, tot
= 0;
404 while (str
[i
] != '\0') {
405 if (str
[i
] == '\n') {
415 * Frees a CMSF array.
417 * @param cmsf CMSF array to free
418 * @return 0 if successful, or error USTCMD_ERR_ARG
420 int ustcmd_free_cmsf(struct marker_status
*cmsf
)
423 return USTCMD_ERR_ARG
;
427 while (cmsf
[i
].channel
!= NULL
) {
428 free(cmsf
[i
].channel
);
429 free(cmsf
[i
].marker
);
439 * Gets channel/marker/state/format string for a given PID.
441 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
442 * frees with `ustcmd_free_cmsf')
443 * @param pid Targeted PID
444 * @return 0 if successful, or -1 on error
446 int ustcmd_get_cmsf(struct marker_status
**cmsf
, const pid_t pid
)
448 struct ustcomm_header req_header
, res_header
;
449 char *big_str
= NULL
;
451 struct marker_status
*tmp_cmsf
= NULL
;
452 unsigned int i
= 0, cmsf_ind
= 0;
458 if (ustcomm_connect_app(pid
, &app_fd
)) {
459 ERR("could not connect to PID %u", (unsigned int) pid
);
463 req_header
.command
= LIST_MARKERS
;
466 result
= ustcomm_send(app_fd
, &req_header
, NULL
);
468 PERROR("error while requesting markers list for process %d", pid
);
472 result
= ustcomm_recv_alloc(app_fd
, &res_header
, &big_str
);
474 ERR("error while receiving markers list");
480 tmp_cmsf
= (struct marker_status
*) zmalloc(sizeof(struct marker_status
) *
481 (ustcmd_count_nl(big_str
) + 1));
482 if (tmp_cmsf
== NULL
) {
483 ERR("Failed to allocate CMSF array");
487 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
488 while (big_str
[i
] != '\0') {
491 sscanf(big_str
+ i
, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
492 &tmp_cmsf
[cmsf_ind
].channel
,
493 &tmp_cmsf
[cmsf_ind
].marker
,
495 &tmp_cmsf
[cmsf_ind
].fs
);
496 tmp_cmsf
[cmsf_ind
].state
= (state
== USTCMD_MS_CHR_ON
?
497 USTCMD_MS_ON
: USTCMD_MS_OFF
); /* Marker state */
499 while (big_str
[i
] != '\n') {
500 ++i
; /* Go to next '\n' */
502 ++i
; /* Skip current pointed '\n' */
505 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
506 tmp_cmsf
[cmsf_ind
].marker
= NULL
;
507 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
519 * @param tes TES array to free
520 * @return 0 if successful, or error USTCMD_ERR_ARG
522 int ustcmd_free_tes(struct trace_event_status
*tes
)
525 return USTCMD_ERR_ARG
;
529 while (tes
[i
].name
!= NULL
) {
539 * Gets trace_events string for a given PID.
541 * @param tes Pointer to TES array to be filled (callee allocates, caller
542 * frees with `ustcmd_free_tes')
543 * @param pid Targeted PID
544 * @return 0 if successful, or -1 on error
546 int ustcmd_get_tes(struct trace_event_status
**tes
,
549 struct ustcomm_header req_header
, res_header
;
550 char *big_str
= NULL
;
552 struct trace_event_status
*tmp_tes
= NULL
;
553 unsigned int i
= 0, tes_ind
= 0;
559 if (ustcomm_connect_app(pid
, &app_fd
)) {
560 ERR("could not connect to PID %u", (unsigned int) pid
);
564 req_header
.command
= LIST_TRACE_EVENTS
;
567 result
= ustcomm_send(app_fd
, &req_header
, NULL
);
569 ERR("error while requesting trace_event list");
573 result
= ustcomm_recv_alloc(app_fd
, &res_header
, &big_str
);
575 ERR("error while receiving markers list");
581 tmp_tes
= (struct trace_event_status
*)
582 zmalloc(sizeof(struct trace_event_status
) *
583 (ustcmd_count_nl(big_str
) + 1));
584 if (tmp_tes
== NULL
) {
585 ERR("Failed to allocate TES array");
589 /* Parse received reply string (format: "[name]"): */
590 while (big_str
[i
] != '\0') {
591 sscanf(big_str
+ i
, "trace_event: %a[^\n]",
592 &tmp_tes
[tes_ind
].name
);
593 while (big_str
[i
] != '\n') {
594 ++i
; /* Go to next '\n' */
596 ++i
; /* Skip current pointed '\n' */
599 tmp_tes
[tes_ind
].name
= NULL
;
610 * @param sock_path Socket path
611 * @param pid Traced process ID
612 * @return 0 if successful, or error
614 int ustcmd_set_sock_path(const char *sock_path
, pid_t pid
)
617 struct ustcomm_header req_header
, res_header
;
618 struct ustcomm_sock_path sock_path_msg
;
620 sock_path_msg
.sock_path
= ustcomm_print_data(sock_path_msg
.data
,
621 sizeof(sock_path_msg
.data
),
624 if (sock_path_msg
.sock_path
== USTCOMM_POISON_PTR
) {
628 req_header
.command
= SET_SOCK_PATH
;
629 req_header
.size
= COMPUTE_MSG_SIZE(&sock_path_msg
, offset
);
631 return do_cmd(pid
, &req_header
, (char *)&sock_path_msg
,
638 * @param sock_path Pointer to where the socket path will be returned
639 * @param pid Traced process ID
640 * @return 0 if successful, or error
642 int ustcmd_get_sock_path(char **sock_path
, pid_t pid
)
645 struct ustcomm_header req_header
, res_header
;
646 struct ustcomm_sock_path
*sock_path_msg
;
648 req_header
.command
= GET_SOCK_PATH
;
651 result
= do_cmd(pid
, &req_header
, NULL
, &res_header
,
652 (char **)&sock_path_msg
);
657 result
= ustcomm_unpack_sock_path(sock_path_msg
);
662 *sock_path
= strdup(sock_path_msg
->sock_path
);
669 int ustcmd_force_switch(pid_t pid
)
671 struct ustcomm_header req_header
, res_header
;
673 req_header
.command
= FORCE_SUBBUF_SWITCH
;
676 return do_cmd(pid
, &req_header
, NULL
, &res_header
, NULL
);