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
32 pid_t
* ustcmd_get_online_pids(void) {
33 struct dirent
* dirent
;
37 dir
= opendir(SOCK_DIR
);
42 unsigned int ret_size
= 1 * sizeof(pid_t
), i
= 0;
43 pid_t
* ret
= (pid_t
*) malloc(ret_size
);
45 while (dirent
= readdir(dir
)) {
46 if (!strcmp(dirent
->d_name
, ".") ||
47 !strcmp(dirent
->d_name
, "..")) {
52 if (dirent
->d_type
!= DT_DIR
&&
53 !!strcmp(dirent
->d_name
, "ustd")) {
55 sscanf(dirent
->d_name
, "%u", (unsigned int*) &ret
[i
]);
56 if (pid_is_online(ret
[i
])) {
57 ret_size
+= sizeof(pid_t
);
58 ret
= (pid_t
*) realloc(ret
, ret_size
);
64 ret
[i
] = 0; /* Array end */
66 if (ret
[0] == 0) { /* No PID at all.. */
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 return USTCMD_ERR_ARG
;
88 char* cmd_str
[] = {"disable_marker", "enable_marker"};
90 asprintf(&cmd
, "%s %s", cmd_str
[state
], mn
);
93 if (tres
= ustcmd_shoot(cmd
, pid
, NULL
)) {
95 return USTCMD_ERR_GEN
;
103 * Destroys an UST trace according to a PID.
105 * @param pid Traced process ID
106 * @return 0 if successful, or error USTCMD_ERR_GEN
108 int ustcmd_destroy_trace(pid_t pid
) {
111 if (tres
= ustcmd_shoot("destroy", pid
, NULL
)) {
112 return USTCMD_ERR_GEN
;
119 * Starts an UST trace (and setups it) according to a PID.
121 * @param pid Traced process ID
122 * @return 0 if successful, or error USTCMD_ERR_GEN
124 int ustcmd_setup_and_start(pid_t pid
) {
127 if (tres
= ustcmd_shoot("start", pid
, NULL
)) {
128 return USTCMD_ERR_GEN
;
135 * Starts an UST trace according to a PID.
137 * @param pid Traced process ID
138 * @return 0 if successful, or error USTCMD_ERR_GEN
140 int ustcmd_start_trace(pid_t pid
) {
143 if (tres
= ustcmd_shoot("trace_start", pid
, NULL
)) {
144 return USTCMD_ERR_GEN
;
151 * Stops an UST trace according to a PID.
153 * @param pid Traced process ID
154 * @return 0 if successful, or error USTCMD_ERR_GEN
156 int ustcmd_stop_trace(pid_t pid
) {
159 if (tres
= ustcmd_shoot("trace_stop", pid
, NULL
)) {
160 return USTCMD_ERR_GEN
;
167 * Counts newlines ('\n') in a string.
169 * @param str String to search in
170 * @return Total newlines count
172 unsigned int ustcmd_count_nl(const char* str
) {
173 unsigned int i
= 0, tot
= 0;
175 while (str
[i
] != '\0') {
176 if (str
[i
] == '\n') {
186 * Frees a CMSF array.
188 * @param cmsf CMSF array to free
189 * @return 0 if successful, or error USTCMD_ERR_ARG
191 int ustcmd_free_cmsf(struct USTcmd_cmsf
* cmsf
) {
193 return USTCMD_ERR_ARG
;
197 while (cmsf
[i
].channel
!= NULL
) {
198 free(cmsf
[i
].channel
);
199 free(cmsf
[i
].marker
);
209 * Gets channel/marker/state/format string for a given PID.
211 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
212 * frees with `ustcmd_free_cmsf')
213 * @param pid Targeted PID
214 * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_GEN}
216 int ustcmd_get_cmsf(struct USTcmd_cmsf
** cmsf
, const pid_t pid
) {
218 return USTCMD_ERR_ARG
;
220 char* big_str
= NULL
;
223 if (tres
= ustcmd_shoot("list_markers", pid
, &big_str
)) {
224 return USTCMD_ERR_GEN
;
227 if (big_str
== NULL
) {
228 fprintf(stderr
, "ustcmd: error while getting markers list\n");
229 return USTCMD_ERR_GEN
;
232 struct USTcmd_cmsf
* tmp_cmsf
= NULL
;
233 tmp_cmsf
= (struct USTcmd_cmsf
*) malloc(sizeof(struct USTcmd_cmsf
) *
234 (ustcmd_count_nl(big_str
) + 1));
235 if (tmp_cmsf
== NULL
) {
236 return USTCMD_ERR_GEN
;
239 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
240 unsigned int i
= 0, cur_st
, cmsf_ind
= 0;
241 while (big_str
[i
] != '\0') {
243 sscanf(big_str
+ i
, "%a[^/]/%a[^ ] %c %a[^\n]",
244 &tmp_cmsf
[cmsf_ind
].channel
,
245 &tmp_cmsf
[cmsf_ind
].marker
,
247 &tmp_cmsf
[cmsf_ind
].fs
);
248 tmp_cmsf
[cmsf_ind
].state
= (state
== USTCMD_MS_CHR_ON
?
249 USTCMD_MS_ON
: USTCMD_MS_OFF
); /* Marker state */
251 while (big_str
[i
] != '\n') {
252 ++i
; /* Go to next '\n' */
254 ++i
; /* Skip current pointed '\n' */
257 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
258 tmp_cmsf
[cmsf_ind
].marker
= NULL
;
259 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
268 * Shoots a given command using ustcomm.
270 * @param cmd Null-terminated command to shoot
271 * @param pid Targeted PID
272 * @param reply Pointer to string to be filled with a reply string (must
273 * be NULL if no reply is needed for the given command).
274 * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_CONN}
276 int ustcmd_shoot(const char* cmd
, const pid_t pid
, char** reply
) {
278 return USTCMD_ERR_ARG
;
281 struct ustcomm_connection conn
;
282 if (ustcomm_connect_app(pid
, &conn
)) {
283 fprintf(stderr
, "ustcmd_shoot: could not connect to PID %u\n",
285 return USTCMD_ERR_CONN
;
288 ustcomm_send_request(&conn
, cmd
, reply
);
This page took 0.071799 seconds and 5 git commands to generate.