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 pid_t
*ustcmd_get_online_pids(void)
33 struct dirent
*dirent
;
35 unsigned int ret_size
= 1 * sizeof(pid_t
), i
= 0;
37 dir
= opendir(SOCK_DIR
);
42 pid_t
*ret
= (pid_t
*) malloc(ret_size
);
44 while ((dirent
= readdir(dir
))) {
45 if (!strcmp(dirent
->d_name
, ".") ||
46 !strcmp(dirent
->d_name
, "..")) {
51 if (dirent
->d_type
!= DT_DIR
&&
52 !!strcmp(dirent
->d_name
, "ustd")) {
54 sscanf(dirent
->d_name
, "%u", (unsigned int *) &ret
[i
]);
55 if (pid_is_online(ret
[i
])) {
56 ret_size
+= sizeof(pid_t
);
57 ret
= (pid_t
*) realloc(ret
, ret_size
);
63 ret
[i
] = 0; /* Array end */
76 * Sets marker state (USTCMD_MS_ON or USTCMD_MS_OFF).
78 * @param mn Marker name
79 * @param state Marker's new state
80 * @param pid Traced process ID
81 * @return 0 if successful, or errors {USTCMD_ERR_GEN, USTCMD_ERR_ARG}
83 int ustcmd_set_marker_state(const char *mn
, int state
, pid_t pid
)
85 char *cmd_str
[] = {"disable_marker", "enable_marker"};
90 return USTCMD_ERR_ARG
;
93 asprintf(&cmd
, "%s %s", cmd_str
[state
], mn
);
95 result
= ustcmd_send_cmd(cmd
, pid
, NULL
);
98 return USTCMD_ERR_GEN
;
106 * Set subbuffer size.
108 * @param channel_size Channel name and size
109 * @param pid Traced process ID
110 * @return 0 if successful, or error
112 int ustcmd_set_subbuf_size(const char *channel_size
, pid_t pid
)
117 asprintf(&cmd
, "%s %s", "set_subbuf_size", channel_size
);
119 result
= ustcmd_send_cmd(cmd
, pid
, NULL
);
132 * @param channel_num Channel name and num
133 * @param pid Traced process ID
134 * @return 0 if successful, or error
136 int ustcmd_set_subbuf_num(const char *channel_size
, pid_t pid
)
141 asprintf(&cmd
, "%s %s", "set_subbuf_num", channel_size
);
143 result
= ustcmd_send_cmd(cmd
, pid
, NULL
);
154 * Get subbuffer size.
156 * @param channel Channel name
157 * @param pid Traced process ID
158 * @return subbuf size if successful, or error
160 int ustcmd_get_subbuf_size(const char *channel
, pid_t pid
)
165 /* format: channel_cpu */
166 asprintf(&cmd
, "%s %s_0", "get_subbuf_size", channel
);
168 result
= ustcmd_send_cmd(cmd
, pid
, &reply
);
175 result
= atoi(reply
);
184 * @param channel Channel name
185 * @param pid Traced process ID
186 * @return subbuf cnf if successful, or error
188 int ustcmd_get_subbuf_num(const char *channel
, pid_t pid
)
193 /* format: channel_cpu */
194 asprintf(&cmd
, "%s %s_0", "get_n_subbufs", channel
);
196 result
= ustcmd_send_cmd(cmd
, pid
, &reply
);
203 result
= atoi(reply
);
210 * Destroys an UST trace according to a PID.
212 * @param pid Traced process ID
213 * @return 0 if successful, or error USTCMD_ERR_GEN
215 int ustcmd_destroy_trace(pid_t pid
)
219 result
= ustcmd_send_cmd("trace_destroy", pid
, NULL
);
221 return USTCMD_ERR_GEN
;
228 * Starts an UST trace (and setups it) according to a PID.
230 * @param pid Traced process ID
231 * @return 0 if successful, or error USTCMD_ERR_GEN
233 int ustcmd_setup_and_start(pid_t pid
)
237 result
= ustcmd_send_cmd("start", pid
, NULL
);
239 return USTCMD_ERR_GEN
;
246 * Creates an UST trace according to a PID.
248 * @param pid Traced process ID
249 * @return 0 if successful, or error USTCMD_ERR_GEN
251 int ustcmd_create_trace(pid_t pid
)
255 result
= ustcmd_send_cmd("trace_create", pid
, NULL
);
257 return USTCMD_ERR_GEN
;
264 * Starts an UST trace according to a PID.
266 * @param pid Traced process ID
267 * @return 0 if successful, or error USTCMD_ERR_GEN
269 int ustcmd_start_trace(pid_t pid
)
273 result
= ustcmd_send_cmd("trace_start", pid
, NULL
);
275 return USTCMD_ERR_GEN
;
282 * Alloc an UST trace according to a PID.
284 * @param pid Traced process ID
285 * @return 0 if successful, or error USTCMD_ERR_GEN
287 int ustcmd_alloc_trace(pid_t pid
)
291 result
= ustcmd_send_cmd("trace_alloc", pid
, NULL
);
293 return USTCMD_ERR_GEN
;
300 * Stops an UST trace according to a PID.
302 * @param pid Traced process ID
303 * @return 0 if successful, or error USTCMD_ERR_GEN
305 int ustcmd_stop_trace(pid_t pid
)
309 result
= ustcmd_send_cmd("trace_stop", pid
, NULL
);
311 return USTCMD_ERR_GEN
;
318 * Counts newlines ('\n') in a string.
320 * @param str String to search in
321 * @return Total newlines count
323 unsigned int ustcmd_count_nl(const char *str
)
325 unsigned int i
= 0, tot
= 0;
327 while (str
[i
] != '\0') {
328 if (str
[i
] == '\n') {
338 * Frees a CMSF array.
340 * @param cmsf CMSF array to free
341 * @return 0 if successful, or error USTCMD_ERR_ARG
343 int ustcmd_free_cmsf(struct marker_status
*cmsf
)
346 return USTCMD_ERR_ARG
;
350 while (cmsf
[i
].channel
!= NULL
) {
351 free(cmsf
[i
].channel
);
352 free(cmsf
[i
].marker
);
362 * Gets channel/marker/state/format string for a given PID.
364 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
365 * frees with `ustcmd_free_cmsf')
366 * @param pid Targeted PID
367 * @return 0 if successful, or -1 on error
369 int ustcmd_get_cmsf(struct marker_status
**cmsf
, const pid_t pid
)
371 char *big_str
= NULL
;
373 struct marker_status
*tmp_cmsf
= NULL
;
374 unsigned int i
= 0, cmsf_ind
= 0;
379 result
= ustcmd_send_cmd("list_markers", pid
, &big_str
);
385 ERR("error while getting markers list");
389 tmp_cmsf
= (struct marker_status
*) malloc(sizeof(struct marker_status
) *
390 (ustcmd_count_nl(big_str
) + 1));
391 if (tmp_cmsf
== NULL
) {
395 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
396 while (big_str
[i
] != '\0') {
399 sscanf(big_str
+ i
, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
400 &tmp_cmsf
[cmsf_ind
].channel
,
401 &tmp_cmsf
[cmsf_ind
].marker
,
403 &tmp_cmsf
[cmsf_ind
].fs
);
404 tmp_cmsf
[cmsf_ind
].state
= (state
== USTCMD_MS_CHR_ON
?
405 USTCMD_MS_ON
: USTCMD_MS_OFF
); /* Marker state */
407 while (big_str
[i
] != '\n') {
408 ++i
; /* Go to next '\n' */
410 ++i
; /* Skip current pointed '\n' */
413 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
414 tmp_cmsf
[cmsf_ind
].marker
= NULL
;
415 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
426 * @param sock_path Socket path
427 * @param pid Traced process ID
428 * @return 0 if successful, or error
430 int ustcmd_set_sock_path(const char *sock_path
, pid_t pid
)
435 asprintf(&cmd
, "%s %s", "set_sock_path", sock_path
);
437 result
= ustcmd_send_cmd(cmd
, pid
, NULL
);
440 return USTCMD_ERR_GEN
;
450 * @param sock_path Pointer to where the socket path will be returned
451 * @param pid Traced process ID
452 * @return 0 if successful, or error
454 int ustcmd_get_sock_path(char **sock_path
, pid_t pid
)
459 asprintf(&cmd
, "%s", "get_sock_path");
461 result
= ustcmd_send_cmd(cmd
, pid
, &reply
);
465 return USTCMD_ERR_GEN
;
473 int ustcmd_force_switch(pid_t pid
)
477 result
= ustcmd_send_cmd("force_switch", pid
, NULL
);
479 return USTCMD_ERR_GEN
;
486 * Sends a given command to a traceable process
488 * @param cmd Null-terminated command to send
489 * @param pid Targeted PID
490 * @param reply Pointer to string to be filled with a reply string (must
491 * be NULL if no reply is needed for the given command).
492 * @return -1 if successful, 0 on EOT, 1 on success
495 int ustcmd_send_cmd(const char *cmd
, const pid_t pid
, char **reply
)
497 struct ustcomm_connection conn
;
500 if (ustcomm_connect_app(pid
, &conn
)) {
501 ERR("could not connect to PID %u", (unsigned int) pid
);
505 retval
= ustcomm_send_request(&conn
, cmd
, reply
);
507 ustcomm_close_app(&conn
);
This page took 0.042271 seconds and 4 git commands to generate.