don't install libustcomm and libustsnprintf
[ust.git] / libustcmd / ustcmd.c
1 /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette
2 *
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.
7 *
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.
12 *
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
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <dirent.h>
26
27 #include "ustcomm.h"
28 #include "ustcmd.h"
29 #include "usterr.h"
30
31 pid_t *ustcmd_get_online_pids(void)
32 {
33 struct dirent *dirent;
34 DIR *dir;
35 unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
36
37 dir = opendir(SOCK_DIR);
38 if (!dir) {
39 return NULL;
40 }
41
42 pid_t *ret = (pid_t *) malloc(ret_size);
43
44 while ((dirent = readdir(dir))) {
45 if (!strcmp(dirent->d_name, ".") ||
46 !strcmp(dirent->d_name, "..")) {
47
48 continue;
49 }
50
51 if (dirent->d_type != DT_DIR &&
52 !!strcmp(dirent->d_name, "ustd")) {
53
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);
58 ++i;
59 }
60 }
61 }
62
63 ret[i] = 0; /* Array end */
64
65 if (ret[0] == 0) {
66 /* No PID at all */
67 free(ret);
68 return NULL;
69 }
70
71 closedir(dir);
72 return ret;
73 }
74
75 /**
76 * Sets marker state (USTCMD_MS_ON or USTCMD_MS_OFF).
77 *
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}
82 */
83 int ustcmd_set_marker_state(const char *mn, int state, pid_t pid)
84 {
85 char *cmd_str [] = {"disable_marker", "enable_marker"};
86 char *cmd;
87 int result;
88
89 if (mn == NULL) {
90 return USTCMD_ERR_ARG;
91 }
92
93 asprintf(&cmd, "%s %s", cmd_str[state], mn);
94
95 result = ustcmd_send_cmd(cmd, pid, NULL);
96 if (result) {
97 free(cmd);
98 return USTCMD_ERR_GEN;
99 }
100
101 free(cmd);
102 return 0;
103 }
104
105 /**
106 * Set subbuffer size.
107 *
108 * @param channel_size Channel name and size
109 * @param pid Traced process ID
110 * @return 0 if successful, or error
111 */
112 int ustcmd_set_subbuf_size(const char *channel_size, pid_t pid)
113 {
114 char *cmd;
115 int result;
116
117 asprintf(&cmd, "%s %s", "set_subbuf_size", channel_size);
118
119 result = ustcmd_send_cmd(cmd, pid, NULL);
120 if (result != 1) {
121 free(cmd);
122 return 1;
123 }
124
125 free(cmd);
126 return 0;
127 }
128
129 /**
130 * Set subbuffer num.
131 *
132 * @param channel_num Channel name and num
133 * @param pid Traced process ID
134 * @return 0 if successful, or error
135 */
136 int ustcmd_set_subbuf_num(const char *channel_size, pid_t pid)
137 {
138 char *cmd;
139 int result;
140
141 asprintf(&cmd, "%s %s", "set_subbuf_num", channel_size);
142
143 result = ustcmd_send_cmd(cmd, pid, NULL);
144 if (result != 1) {
145 free(cmd);
146 return 1;
147 }
148
149 free(cmd);
150 return 0;
151 }
152
153 /**
154 * Get subbuffer size.
155 *
156 * @param channel Channel name
157 * @param pid Traced process ID
158 * @return subbuf size if successful, or error
159 */
160 int ustcmd_get_subbuf_size(const char *channel, pid_t pid)
161 {
162 char *cmd, *reply;
163 int result;
164
165 /* format: channel_cpu */
166 asprintf(&cmd, "%s %s_0", "get_subbuf_size", channel);
167
168 result = ustcmd_send_cmd(cmd, pid, &reply);
169 if (result) {
170 free(cmd);
171 free(reply);
172 return -1;
173 }
174
175 result = atoi(reply);
176 free(cmd);
177 free(reply);
178 return result;
179 }
180
181 /**
182 * Get subbuffer num.
183 *
184 * @param channel Channel name
185 * @param pid Traced process ID
186 * @return subbuf cnf if successful, or error
187 */
188 int ustcmd_get_subbuf_num(const char *channel, pid_t pid)
189 {
190 char *cmd, *reply;
191 int result;
192
193 /* format: channel_cpu */
194 asprintf(&cmd, "%s %s_0", "get_n_subbufs", channel);
195
196 result = ustcmd_send_cmd(cmd, pid, &reply);
197 if (result) {
198 free(cmd);
199 free(reply);
200 return -1;
201 }
202
203 result = atoi(reply);
204 free(cmd);
205 free(reply);
206 return result;
207 }
208
209 /**
210 * Destroys an UST trace according to a PID.
211 *
212 * @param pid Traced process ID
213 * @return 0 if successful, or error USTCMD_ERR_GEN
214 */
215 int ustcmd_destroy_trace(pid_t pid)
216 {
217 int result;
218
219 result = ustcmd_send_cmd("trace_destroy", pid, NULL);
220 if (result != 1) {
221 return USTCMD_ERR_GEN;
222 }
223
224 return 0;
225 }
226
227 /**
228 * Starts an UST trace (and setups it) according to a PID.
229 *
230 * @param pid Traced process ID
231 * @return 0 if successful, or error USTCMD_ERR_GEN
232 */
233 int ustcmd_setup_and_start(pid_t pid)
234 {
235 int result;
236
237 result = ustcmd_send_cmd("start", pid, NULL);
238 if (result != 1) {
239 return USTCMD_ERR_GEN;
240 }
241
242 return 0;
243 }
244
245 /**
246 * Creates an UST trace according to a PID.
247 *
248 * @param pid Traced process ID
249 * @return 0 if successful, or error USTCMD_ERR_GEN
250 */
251 int ustcmd_create_trace(pid_t pid)
252 {
253 int result;
254
255 result = ustcmd_send_cmd("trace_create", pid, NULL);
256 if (result != 1) {
257 return USTCMD_ERR_GEN;
258 }
259
260 return 0;
261 }
262
263 /**
264 * Starts an UST trace according to a PID.
265 *
266 * @param pid Traced process ID
267 * @return 0 if successful, or error USTCMD_ERR_GEN
268 */
269 int ustcmd_start_trace(pid_t pid)
270 {
271 int result;
272
273 result = ustcmd_send_cmd("trace_start", pid, NULL);
274 if (result != 1) {
275 return USTCMD_ERR_GEN;
276 }
277
278 return 0;
279 }
280
281 /**
282 * Alloc an UST trace according to a PID.
283 *
284 * @param pid Traced process ID
285 * @return 0 if successful, or error USTCMD_ERR_GEN
286 */
287 int ustcmd_alloc_trace(pid_t pid)
288 {
289 int result;
290
291 result = ustcmd_send_cmd("trace_alloc", pid, NULL);
292 if (result != 1) {
293 return USTCMD_ERR_GEN;
294 }
295
296 return 0;
297 }
298
299 /**
300 * Stops an UST trace according to a PID.
301 *
302 * @param pid Traced process ID
303 * @return 0 if successful, or error USTCMD_ERR_GEN
304 */
305 int ustcmd_stop_trace(pid_t pid)
306 {
307 int result;
308
309 result = ustcmd_send_cmd("trace_stop", pid, NULL);
310 if (result != 1) {
311 return USTCMD_ERR_GEN;
312 }
313
314 return 0;
315 }
316
317 /**
318 * Counts newlines ('\n') in a string.
319 *
320 * @param str String to search in
321 * @return Total newlines count
322 */
323 unsigned int ustcmd_count_nl(const char *str)
324 {
325 unsigned int i = 0, tot = 0;
326
327 while (str[i] != '\0') {
328 if (str[i] == '\n') {
329 ++tot;
330 }
331 ++i;
332 }
333
334 return tot;
335 }
336
337 /**
338 * Frees a CMSF array.
339 *
340 * @param cmsf CMSF array to free
341 * @return 0 if successful, or error USTCMD_ERR_ARG
342 */
343 int ustcmd_free_cmsf(struct marker_status *cmsf)
344 {
345 if (cmsf == NULL) {
346 return USTCMD_ERR_ARG;
347 }
348
349 unsigned int i = 0;
350 while (cmsf[i].channel != NULL) {
351 free(cmsf[i].channel);
352 free(cmsf[i].marker);
353 free(cmsf[i].fs);
354 ++i;
355 }
356 free(cmsf);
357
358 return 0;
359 }
360
361 /**
362 * Gets channel/marker/state/format string for a given PID.
363 *
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
368 */
369 int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid)
370 {
371 char *big_str = NULL;
372 int result;
373 struct marker_status *tmp_cmsf = NULL;
374 unsigned int i = 0, cmsf_ind = 0;
375
376 if (cmsf == NULL) {
377 return -1;
378 }
379 result = ustcmd_send_cmd("list_markers", pid, &big_str);
380 if (result != 1) {
381 return -1;
382 }
383
384 if (result != 1) {
385 ERR("error while getting markers list");
386 return -1;
387 }
388
389 tmp_cmsf = (struct marker_status *) malloc(sizeof(struct marker_status) *
390 (ustcmd_count_nl(big_str) + 1));
391 if (tmp_cmsf == NULL) {
392 return -1;
393 }
394
395 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
396 while (big_str[i] != '\0') {
397 char state;
398
399 sscanf(big_str + i, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
400 &tmp_cmsf[cmsf_ind].channel,
401 &tmp_cmsf[cmsf_ind].marker,
402 &state,
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 */
406
407 while (big_str[i] != '\n') {
408 ++i; /* Go to next '\n' */
409 }
410 ++i; /* Skip current pointed '\n' */
411 ++cmsf_ind;
412 }
413 tmp_cmsf[cmsf_ind].channel = NULL;
414 tmp_cmsf[cmsf_ind].marker = NULL;
415 tmp_cmsf[cmsf_ind].fs = NULL;
416
417 *cmsf = tmp_cmsf;
418
419 free(big_str);
420 return 0;
421 }
422
423 /**
424 * Set socket path
425 *
426 * @param sock_path Socket path
427 * @param pid Traced process ID
428 * @return 0 if successful, or error
429 */
430 int ustcmd_set_sock_path(const char *sock_path, pid_t pid)
431 {
432 char *cmd;
433 int result;
434
435 asprintf(&cmd, "%s %s", "set_sock_path", sock_path);
436
437 result = ustcmd_send_cmd(cmd, pid, NULL);
438 if (result != 1) {
439 free(cmd);
440 return USTCMD_ERR_GEN;
441 }
442
443 free(cmd);
444 return 0;
445 }
446
447 /**
448 * Get socket path
449 *
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
453 */
454 int ustcmd_get_sock_path(char **sock_path, pid_t pid)
455 {
456 char *cmd, *reply;
457 int result;
458
459 asprintf(&cmd, "%s", "get_sock_path");
460
461 result = ustcmd_send_cmd(cmd, pid, &reply);
462 if (result != 1) {
463 free(cmd);
464 free(reply);
465 return USTCMD_ERR_GEN;
466 }
467
468 free(cmd);
469 *sock_path = reply;
470 return 0;
471 }
472
473 int ustcmd_force_switch(pid_t pid)
474 {
475 int result;
476
477 result = ustcmd_send_cmd("force_switch", pid, NULL);
478 if (result != 1) {
479 return USTCMD_ERR_GEN;
480 }
481
482 return 0;
483 }
484
485 /**
486 * Sends a given command to a traceable process
487 *
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
493 */
494
495 int ustcmd_send_cmd(const char *cmd, const pid_t pid, char **reply)
496 {
497 struct ustcomm_connection conn;
498 int retval;
499
500 if (ustcomm_connect_app(pid, &conn)) {
501 ERR("could not connect to PID %u", (unsigned int) pid);
502 return -1;
503 }
504
505 retval = ustcomm_send_request(&conn, cmd, reply);
506
507 ustcomm_close_app(&conn);
508
509 return retval;
510 }
This page took 0.044884 seconds and 4 git commands to generate.