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
30 pid_t
*ustcmd_get_online_pids(void)
32 struct dirent
*dirent
;
34 unsigned int ret_size
= 1 * sizeof(pid_t
), i
= 0;
36 dir
= opendir(SOCK_DIR
);
41 pid_t
*ret
= (pid_t
*) malloc(ret_size
);
43 while ((dirent
= readdir(dir
))) {
44 if (!strcmp(dirent
->d_name
, ".") ||
45 !strcmp(dirent
->d_name
, "..")) {
50 if (dirent
->d_type
!= DT_DIR
&&
51 !!strcmp(dirent
->d_name
, "ustd")) {
53 sscanf(dirent
->d_name
, "%u", (unsigned int *) &ret
[i
]);
54 if (pid_is_online(ret
[i
])) {
55 ret_size
+= sizeof(pid_t
);
56 ret
= (pid_t
*) realloc(ret
, ret_size
);
62 ret
[i
] = 0; /* Array end */
75 * Sets marker state (USTCMD_MS_ON or USTCMD_MS_OFF).
77 * @param mn Marker name
78 * @param state Marker's new state
79 * @param pid Traced process ID
80 * @return 0 if successful, or errors {USTCMD_ERR_GEN, USTCMD_ERR_ARG}
82 int ustcmd_set_marker_state(const char *mn
, int state
, pid_t pid
)
84 char *cmd_str
[] = {"disable_marker", "enable_marker"};
89 return USTCMD_ERR_ARG
;
92 asprintf(&cmd
, "%s %s", cmd_str
[state
], mn
);
94 result
= ustcmd_send_cmd(cmd
, pid
, NULL
);
97 return USTCMD_ERR_GEN
;
105 * Set subbuffer size.
107 * @param channel_size Channel name and size
108 * @param pid Traced process ID
109 * @return 0 if successful, or error
111 int ustcmd_set_subbuf_size(const char *channel_size
, pid_t pid
)
116 asprintf(&cmd
, "%s %s", "set_subbuf_size", channel_size
);
118 result
= ustcmd_send_cmd(cmd
, pid
, NULL
);
131 * @param channel_num Channel name and num
132 * @param pid Traced process ID
133 * @return 0 if successful, or error
135 int ustcmd_set_subbuf_num(const char *channel_size
, pid_t pid
)
140 asprintf(&cmd
, "%s %s", "set_subbuf_num", channel_size
);
142 result
= ustcmd_send_cmd(cmd
, pid
, NULL
);
154 * Destroys an UST trace according to a PID.
156 * @param pid Traced process ID
157 * @return 0 if successful, or error USTCMD_ERR_GEN
159 int ustcmd_destroy_trace(pid_t pid
)
163 result
= ustcmd_send_cmd("trace_destroy", pid
, NULL
);
165 return USTCMD_ERR_GEN
;
172 * Starts an UST trace (and setups it) according to a PID.
174 * @param pid Traced process ID
175 * @return 0 if successful, or error USTCMD_ERR_GEN
177 int ustcmd_setup_and_start(pid_t pid
)
181 result
= ustcmd_send_cmd("start", pid
, NULL
);
183 return USTCMD_ERR_GEN
;
190 * Creates an UST trace according to a PID.
192 * @param pid Traced process ID
193 * @return 0 if successful, or error USTCMD_ERR_GEN
195 int ustcmd_create_trace(pid_t pid
)
199 result
= ustcmd_send_cmd("trace_create", pid
, NULL
);
201 return USTCMD_ERR_GEN
;
208 * Starts an UST trace according to a PID.
210 * @param pid Traced process ID
211 * @return 0 if successful, or error USTCMD_ERR_GEN
213 int ustcmd_start_trace(pid_t pid
)
217 result
= ustcmd_send_cmd("trace_start", pid
, NULL
);
219 return USTCMD_ERR_GEN
;
226 * Alloc an UST trace according to a PID.
228 * @param pid Traced process ID
229 * @return 0 if successful, or error USTCMD_ERR_GEN
231 int ustcmd_alloc_trace(pid_t pid
)
235 result
= ustcmd_send_cmd("trace_alloc", pid
, NULL
);
237 return USTCMD_ERR_GEN
;
244 * Stops an UST trace according to a PID.
246 * @param pid Traced process ID
247 * @return 0 if successful, or error USTCMD_ERR_GEN
249 int ustcmd_stop_trace(pid_t pid
)
253 result
= ustcmd_send_cmd("trace_stop", pid
, NULL
);
255 return USTCMD_ERR_GEN
;
262 * Counts newlines ('\n') in a string.
264 * @param str String to search in
265 * @return Total newlines count
267 unsigned int ustcmd_count_nl(const char *str
)
269 unsigned int i
= 0, tot
= 0;
271 while (str
[i
] != '\0') {
272 if (str
[i
] == '\n') {
282 * Frees a CMSF array.
284 * @param cmsf CMSF array to free
285 * @return 0 if successful, or error USTCMD_ERR_ARG
287 int ustcmd_free_cmsf(struct marker_status
*cmsf
)
290 return USTCMD_ERR_ARG
;
294 while (cmsf
[i
].channel
!= NULL
) {
295 free(cmsf
[i
].channel
);
296 free(cmsf
[i
].marker
);
306 * Gets channel/marker/state/format string for a given PID.
308 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
309 * frees with `ustcmd_free_cmsf')
310 * @param pid Targeted PID
311 * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_GEN}
313 int ustcmd_get_cmsf(struct marker_status
**cmsf
, const pid_t pid
)
315 char *big_str
= NULL
;
317 struct marker_status
*tmp_cmsf
= NULL
;
318 unsigned int i
= 0, cmsf_ind
= 0;
321 return USTCMD_ERR_ARG
;
323 result
= ustcmd_send_cmd("list_markers", pid
, &big_str
);
325 return USTCMD_ERR_GEN
;
328 if (big_str
== NULL
) {
329 fprintf(stderr
, "ustcmd: error while getting markers list\n");
330 return USTCMD_ERR_GEN
;
333 tmp_cmsf
= (struct marker_status
*) malloc(sizeof(struct marker_status
) *
334 (ustcmd_count_nl(big_str
) + 1));
335 if (tmp_cmsf
== NULL
) {
336 return USTCMD_ERR_GEN
;
339 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
340 while (big_str
[i
] != '\0') {
343 sscanf(big_str
+ i
, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
344 &tmp_cmsf
[cmsf_ind
].channel
,
345 &tmp_cmsf
[cmsf_ind
].marker
,
347 &tmp_cmsf
[cmsf_ind
].fs
);
348 tmp_cmsf
[cmsf_ind
].state
= (state
== USTCMD_MS_CHR_ON
?
349 USTCMD_MS_ON
: USTCMD_MS_OFF
); /* Marker state */
351 while (big_str
[i
] != '\n') {
352 ++i
; /* Go to next '\n' */
354 ++i
; /* Skip current pointed '\n' */
357 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
358 tmp_cmsf
[cmsf_ind
].marker
= NULL
;
359 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
368 * Shoots a given command using ustcomm.
370 * @param cmd Null-terminated command to shoot
371 * @param pid Targeted PID
372 * @param reply Pointer to string to be filled with a reply string (must
373 * be NULL if no reply is needed for the given command).
374 * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_CONN}
377 int ustcmd_send_cmd(const char *cmd
, const pid_t pid
, char **reply
)
379 struct ustcomm_connection conn
;
382 return USTCMD_ERR_ARG
;
385 if (ustcomm_connect_app(pid
, &conn
)) {
386 fprintf(stderr
, "ustcmd_send_cmd: could not connect to PID %u\n",
388 return USTCMD_ERR_CONN
;
391 ustcomm_send_request(&conn
, cmd
, reply
);
This page took 0.055887 seconds and 4 git commands to generate.