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)
34 struct dirent
* dirent
;
36 unsigned int ret_size
= 1 * sizeof(pid_t
), i
= 0;
38 dir
= opendir(SOCK_DIR
);
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 char* cmd_str
[] = {"disable_marker", "enable_marker"};
90 return USTCMD_ERR_ARG
;
93 asprintf(&cmd
, "%s %s", cmd_str
[state
], mn
);
95 result
= ustcmd_shoot(cmd
, pid
, NULL
);
98 return USTCMD_ERR_GEN
;
106 * Destroys an UST trace according to a PID.
108 * @param pid Traced process ID
109 * @return 0 if successful, or error USTCMD_ERR_GEN
111 int ustcmd_destroy_trace(pid_t pid
)
115 result
= ustcmd_shoot("destroy", pid
, NULL
);
117 return USTCMD_ERR_GEN
;
124 * Starts an UST trace (and setups it) according to a PID.
126 * @param pid Traced process ID
127 * @return 0 if successful, or error USTCMD_ERR_GEN
129 int ustcmd_setup_and_start(pid_t pid
)
133 result
= ustcmd_shoot("start", pid
, NULL
);
135 return USTCMD_ERR_GEN
;
142 * Starts an UST trace according to a PID.
144 * @param pid Traced process ID
145 * @return 0 if successful, or error USTCMD_ERR_GEN
147 int ustcmd_start_trace(pid_t pid
)
151 result
= ustcmd_shoot("trace_start", pid
, NULL
);
153 return USTCMD_ERR_GEN
;
160 * Stops an UST trace according to a PID.
162 * @param pid Traced process ID
163 * @return 0 if successful, or error USTCMD_ERR_GEN
165 int ustcmd_stop_trace(pid_t pid
)
169 result
= ustcmd_shoot("trace_stop", pid
, NULL
);
171 return USTCMD_ERR_GEN
;
178 * Counts newlines ('\n') in a string.
180 * @param str String to search in
181 * @return Total newlines count
183 unsigned int ustcmd_count_nl(const char* str
)
185 unsigned int i
= 0, tot
= 0;
187 while (str
[i
] != '\0') {
188 if (str
[i
] == '\n') {
198 * Frees a CMSF array.
200 * @param cmsf CMSF array to free
201 * @return 0 if successful, or error USTCMD_ERR_ARG
203 int ustcmd_free_cmsf(struct USTcmd_cmsf
* cmsf
)
206 return USTCMD_ERR_ARG
;
210 while (cmsf
[i
].channel
!= NULL
) {
211 free(cmsf
[i
].channel
);
212 free(cmsf
[i
].marker
);
222 * Gets channel/marker/state/format string for a given PID.
224 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
225 * frees with `ustcmd_free_cmsf')
226 * @param pid Targeted PID
227 * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_GEN}
229 int ustcmd_get_cmsf(struct USTcmd_cmsf
** cmsf
, const pid_t pid
)
231 char* big_str
= NULL
;
233 struct USTcmd_cmsf
* tmp_cmsf
= NULL
;
234 unsigned int i
= 0, cmsf_ind
= 0;
237 return USTCMD_ERR_ARG
;
239 result
= ustcmd_shoot("list_markers", pid
, &big_str
);
241 return USTCMD_ERR_GEN
;
244 if (big_str
== NULL
) {
245 fprintf(stderr
, "ustcmd: error while getting markers list\n");
246 return USTCMD_ERR_GEN
;
249 tmp_cmsf
= (struct USTcmd_cmsf
*) malloc(sizeof(struct USTcmd_cmsf
) *
250 (ustcmd_count_nl(big_str
) + 1));
251 if (tmp_cmsf
== NULL
) {
252 return USTCMD_ERR_GEN
;
255 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
256 while (big_str
[i
] != '\0') {
259 sscanf(big_str
+ i
, "%a[^/]/%a[^ ] %c %a[^\n]",
260 &tmp_cmsf
[cmsf_ind
].channel
,
261 &tmp_cmsf
[cmsf_ind
].marker
,
263 &tmp_cmsf
[cmsf_ind
].fs
);
264 tmp_cmsf
[cmsf_ind
].state
= (state
== USTCMD_MS_CHR_ON
?
265 USTCMD_MS_ON
: USTCMD_MS_OFF
); /* Marker state */
267 while (big_str
[i
] != '\n') {
268 ++i
; /* Go to next '\n' */
270 ++i
; /* Skip current pointed '\n' */
273 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
274 tmp_cmsf
[cmsf_ind
].marker
= NULL
;
275 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
284 * Shoots a given command using ustcomm.
286 * @param cmd Null-terminated command to shoot
287 * @param pid Targeted PID
288 * @param reply Pointer to string to be filled with a reply string (must
289 * be NULL if no reply is needed for the given command).
290 * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_CONN}
293 int ustcmd_shoot(const char* cmd
, const pid_t pid
, char** reply
)
295 struct ustcomm_connection conn
;
298 return USTCMD_ERR_ARG
;
301 if (ustcomm_connect_app(pid
, &conn
)) {
302 fprintf(stderr
, "ustcmd_shoot: could not connect to PID %u\n",
304 return USTCMD_ERR_CONN
;
307 ustcomm_send_request(&conn
, cmd
, reply
);
This page took 0.052263 seconds and 4 git commands to generate.