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 *trace
, const char *channel
,
144 const char *marker
, 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
,
160 req_header
.command
= state
? ENABLE_MARKER
: DISABLE_MARKER
;
162 return do_cmd(pid
, &req_header
, (char *)&marker_inf
,
167 * Set subbuffer size.
169 * @param channel_size Channel name and size
170 * @param pid Traced process ID
171 * @return 0 if successful, or error
173 int ustcmd_set_subbuf_size(const char *trace
, const char *channel
,
174 unsigned int subbuf_size
, pid_t pid
)
176 struct ustcomm_header req_header
, res_header
;
177 struct ustcomm_channel_info ch_inf
;
180 result
= ustcomm_pack_channel_info(&req_header
,
189 req_header
.command
= SET_SUBBUF_SIZE
;
190 ch_inf
.subbuf_size
= subbuf_size
;
192 return do_cmd(pid
, &req_header
, (char *)&ch_inf
,
199 * @param channel_num Channel name and num
200 * @param pid Traced process ID
201 * @return 0 if successful, or error
203 int ustcmd_set_subbuf_num(const char *trace
, const char *channel
,
204 unsigned int num
, pid_t pid
)
206 struct ustcomm_header req_header
, res_header
;
207 struct ustcomm_channel_info ch_inf
;
210 result
= ustcomm_pack_channel_info(&req_header
,
219 req_header
.command
= SET_SUBBUF_NUM
;
220 ch_inf
.subbuf_num
= num
;
222 return do_cmd(pid
, &req_header
, (char *)&ch_inf
,
227 static int ustcmd_get_subbuf_num_size(const char *trace
, const char *channel
,
228 pid_t pid
, int *num
, int *size
)
230 struct ustcomm_header req_header
, res_header
;
231 struct ustcomm_channel_info ch_inf
, *ch_inf_res
;
235 result
= ustcomm_pack_channel_info(&req_header
,
244 req_header
.command
= GET_SUBBUF_NUM_SIZE
;
246 result
= do_cmd(pid
, &req_header
, (char *)&ch_inf
,
247 &res_header
, (char **)&ch_inf_res
);
252 *num
= ch_inf_res
->subbuf_num
;
253 *size
= ch_inf_res
->subbuf_size
;
263 * @param channel Channel name
264 * @param pid Traced process ID
265 * @return subbuf cnf if successful, or error
267 int ustcmd_get_subbuf_num(const char *trace
, const char *channel
, pid_t pid
)
269 int num
, size
, result
;
271 result
= ustcmd_get_subbuf_num_size(trace
, channel
, pid
,
282 * Get subbuffer size.
284 * @param channel Channel name
285 * @param pid Traced process ID
286 * @return subbuf size if successful, or error
288 int ustcmd_get_subbuf_size(const char *trace
, const char *channel
, pid_t pid
)
290 int num
, size
, result
;
292 result
= ustcmd_get_subbuf_num_size(trace
, channel
, pid
,
303 static int do_trace_cmd(const char *trace
, pid_t pid
, int command
)
305 struct ustcomm_header req_header
, res_header
;
306 struct ustcomm_single_field trace_inf
;
309 result
= ustcomm_pack_single_field(&req_header
,
317 req_header
.command
= command
;
319 return do_cmd(pid
, &req_header
, (char *)&trace_inf
, &res_header
, NULL
);
323 * Destroys an UST trace according to a PID.
325 * @param pid Traced process ID
326 * @return 0 if successful, or error USTCMD_ERR_GEN
328 int ustcmd_destroy_trace(const char *trace
, pid_t pid
)
330 return do_trace_cmd(trace
, pid
, DESTROY_TRACE
);
334 * Starts an UST trace (and setups it) according to a PID.
336 * @param pid Traced process ID
337 * @return 0 if successful, or error USTCMD_ERR_GEN
339 int ustcmd_setup_and_start(const char *trace
, pid_t pid
)
341 return do_trace_cmd(trace
, pid
, START
);
345 * Creates an UST trace according to a PID.
347 * @param pid Traced process ID
348 * @return 0 if successful, or error USTCMD_ERR_GEN
350 int ustcmd_create_trace(const char *trace
, pid_t pid
)
352 return do_trace_cmd(trace
, pid
, CREATE_TRACE
);
356 * Starts an UST trace according to a PID.
358 * @param pid Traced process ID
359 * @return 0 if successful, or error USTCMD_ERR_GEN
361 int ustcmd_start_trace(const char *trace
, pid_t pid
)
363 return do_trace_cmd(trace
, pid
, START_TRACE
);
367 * Alloc an UST trace according to a PID.
369 * @param pid Traced process ID
370 * @return 0 if successful, or error USTCMD_ERR_GEN
372 int ustcmd_alloc_trace(const char *trace
, pid_t pid
)
374 return do_trace_cmd(trace
, pid
, ALLOC_TRACE
);
378 * Stops an UST trace according to a PID.
380 * @param pid Traced process ID
381 * @return 0 if successful, or error USTCMD_ERR_GEN
383 int ustcmd_stop_trace(const char *trace
, pid_t pid
)
385 return do_trace_cmd(trace
, pid
, STOP_TRACE
);
389 * Counts newlines ('\n') in a string.
391 * @param str String to search in
392 * @return Total newlines count
394 unsigned int ustcmd_count_nl(const char *str
)
396 unsigned int i
= 0, tot
= 0;
398 while (str
[i
] != '\0') {
399 if (str
[i
] == '\n') {
409 * Frees a CMSF array.
411 * @param cmsf CMSF array to free
412 * @return 0 if successful, or error USTCMD_ERR_ARG
414 int ustcmd_free_cmsf(struct marker_status
*cmsf
)
417 return USTCMD_ERR_ARG
;
421 while (cmsf
[i
].channel
!= NULL
) {
422 free(cmsf
[i
].channel
);
423 free(cmsf
[i
].marker
);
433 * Gets channel/marker/state/format string for a given PID.
435 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
436 * frees with `ustcmd_free_cmsf')
437 * @param pid Targeted PID
438 * @return 0 if successful, or -1 on error
440 int ustcmd_get_cmsf(struct marker_status
**cmsf
, const pid_t pid
)
442 struct ustcomm_header req_header
, res_header
;
443 char *big_str
= NULL
;
445 struct marker_status
*tmp_cmsf
= NULL
;
446 unsigned int i
= 0, cmsf_ind
= 0;
452 if (ustcomm_connect_app(pid
, &app_fd
)) {
453 ERR("could not connect to PID %u", (unsigned int) pid
);
457 req_header
.command
= LIST_MARKERS
;
460 result
= ustcomm_send(app_fd
, &req_header
, NULL
);
462 PERROR("error while requesting markers list for process %d", pid
);
466 result
= ustcomm_recv_alloc(app_fd
, &res_header
, &big_str
);
468 ERR("error while receiving markers list");
474 tmp_cmsf
= (struct marker_status
*) zmalloc(sizeof(struct marker_status
) *
475 (ustcmd_count_nl(big_str
) + 1));
476 if (tmp_cmsf
== NULL
) {
477 ERR("Failed to allocate CMSF array");
481 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
482 while (big_str
[i
] != '\0') {
485 sscanf(big_str
+ i
, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
486 &tmp_cmsf
[cmsf_ind
].channel
,
487 &tmp_cmsf
[cmsf_ind
].marker
,
489 &tmp_cmsf
[cmsf_ind
].fs
);
490 tmp_cmsf
[cmsf_ind
].state
= (state
== USTCMD_MS_CHR_ON
?
491 USTCMD_MS_ON
: USTCMD_MS_OFF
); /* Marker state */
493 while (big_str
[i
] != '\n') {
494 ++i
; /* Go to next '\n' */
496 ++i
; /* Skip current pointed '\n' */
499 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
500 tmp_cmsf
[cmsf_ind
].marker
= NULL
;
501 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
513 * @param tes TES array to free
514 * @return 0 if successful, or error USTCMD_ERR_ARG
516 int ustcmd_free_tes(struct trace_event_status
*tes
)
519 return USTCMD_ERR_ARG
;
523 while (tes
[i
].name
!= NULL
) {
533 * Gets trace_events string for a given PID.
535 * @param tes Pointer to TES array to be filled (callee allocates, caller
536 * frees with `ustcmd_free_tes')
537 * @param pid Targeted PID
538 * @return 0 if successful, or -1 on error
540 int ustcmd_get_tes(struct trace_event_status
**tes
,
543 struct ustcomm_header req_header
, res_header
;
544 char *big_str
= NULL
;
546 struct trace_event_status
*tmp_tes
= NULL
;
547 unsigned int i
= 0, tes_ind
= 0;
553 if (ustcomm_connect_app(pid
, &app_fd
)) {
554 ERR("could not connect to PID %u", (unsigned int) pid
);
558 req_header
.command
= LIST_TRACE_EVENTS
;
561 result
= ustcomm_send(app_fd
, &req_header
, NULL
);
563 ERR("error while requesting trace_event list");
567 result
= ustcomm_recv_alloc(app_fd
, &res_header
, &big_str
);
569 ERR("error while receiving markers list");
575 tmp_tes
= (struct trace_event_status
*)
576 zmalloc(sizeof(struct trace_event_status
) *
577 (ustcmd_count_nl(big_str
) + 1));
578 if (tmp_tes
== NULL
) {
579 ERR("Failed to allocate TES array");
583 /* Parse received reply string (format: "[name]"): */
584 while (big_str
[i
] != '\0') {
585 sscanf(big_str
+ i
, "trace_event: %a[^\n]",
586 &tmp_tes
[tes_ind
].name
);
587 while (big_str
[i
] != '\n') {
588 ++i
; /* Go to next '\n' */
590 ++i
; /* Skip current pointed '\n' */
593 tmp_tes
[tes_ind
].name
= NULL
;
604 * @param sock_path Socket path
605 * @param pid Traced process ID
606 * @return 0 if successful, or error
608 int ustcmd_set_sock_path(const char *sock_path
, pid_t pid
)
611 struct ustcomm_header req_header
, res_header
;
612 struct ustcomm_single_field sock_path_msg
;
614 result
= ustcomm_pack_single_field(&req_header
,
622 req_header
.command
= SET_SOCK_PATH
;
624 return do_cmd(pid
, &req_header
, (char *)&sock_path_msg
,
631 * @param sock_path Pointer to where the socket path will be returned
632 * @param pid Traced process ID
633 * @return 0 if successful, or error
635 int ustcmd_get_sock_path(char **sock_path
, pid_t pid
)
638 struct ustcomm_header req_header
, res_header
;
639 struct ustcomm_single_field
*sock_path_msg
;
641 req_header
.command
= GET_SOCK_PATH
;
644 result
= do_cmd(pid
, &req_header
, NULL
, &res_header
,
645 (char **)&sock_path_msg
);
650 result
= ustcomm_unpack_single_field(sock_path_msg
);
655 *sock_path
= strdup(sock_path_msg
->field
);
662 int ustcmd_force_switch(pid_t pid
)
664 struct ustcomm_header req_header
, res_header
;
666 req_header
.command
= FORCE_SUBBUF_SWITCH
;
669 return do_cmd(pid
, &req_header
, NULL
, &res_header
, NULL
);