Commit | Line | Data |
---|---|---|
ab33e65c | 1 | /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette |
8b26d56b | 2 | * Copyright (C) 2011 Ericsson AB |
ab33e65c PP |
3 | * |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; either | |
7 | * version 2.1 of the License, or (at your option) any later version. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public | |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
ef290fca | 19 | #define _GNU_SOURCE |
ab33e65c PP |
20 | #include <stdio.h> |
21 | #include <unistd.h> | |
22 | #include <getopt.h> | |
23 | #include <stdlib.h> | |
24 | #include <fcntl.h> | |
25 | #include <string.h> | |
26 | #include <dirent.h> | |
ab33e65c PP |
27 | |
28 | #include "ustcomm.h" | |
2298f329 | 29 | #include "ust/ustctl.h" |
2a79ceeb | 30 | #include "usterr.h" |
ab33e65c | 31 | |
8b26d56b | 32 | static int do_cmd(int sock, |
72098143 NC |
33 | const struct ustcomm_header *req_header, |
34 | const char *req_data, | |
35 | struct ustcomm_header *res_header, | |
36 | char **res_data) | |
37 | { | |
8b26d56b | 38 | int result, saved_errno = 0; |
72098143 NC |
39 | char *recv_buf; |
40 | ||
72098143 NC |
41 | recv_buf = zmalloc(USTCOMM_BUFFER_SIZE); |
42 | if (!recv_buf) { | |
43 | saved_errno = ENOMEM; | |
8b26d56b | 44 | goto out; |
72098143 NC |
45 | } |
46 | ||
8b26d56b | 47 | result = ustcomm_req(sock, req_header, req_data, res_header, recv_buf); |
72098143 NC |
48 | if (result > 0) { |
49 | saved_errno = -res_header->result; | |
50 | if (res_header->size == 0 || saved_errno > 0) { | |
51 | free(recv_buf); | |
52 | } else { | |
53 | if (res_data) { | |
54 | *res_data = recv_buf; | |
55 | } else { | |
56 | free(recv_buf); | |
57 | } | |
58 | } | |
59 | } else { | |
60 | ERR("ustcomm req failed"); | |
61 | if (result == 0) { | |
62 | saved_errno = ENOTCONN; | |
63 | } else { | |
64 | saved_errno = -result; | |
65 | } | |
66 | free(recv_buf); | |
67 | } | |
68 | ||
8b26d56b | 69 | out: |
72098143 | 70 | errno = saved_errno; |
72098143 NC |
71 | if (errno) { |
72 | return -1; | |
73 | } | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
8b26d56b NC |
78 | int ustctl_connect_pid(pid_t pid) |
79 | { | |
80 | int sock; | |
81 | ||
82 | if (ustcomm_connect_app(pid, &sock)) { | |
83 | ERR("could not connect to PID %u", (unsigned int) pid); | |
84 | errno = ENOTCONN; | |
85 | return -1; | |
86 | } | |
87 | ||
88 | return sock; | |
89 | } | |
90 | ||
dde0eeef NC |
91 | static void get_pids_in_dir(DIR *dir, pid_t **pid_list, |
92 | unsigned int *pid_list_size) | |
772030fe | 93 | { |
08230db7 | 94 | struct dirent *dirent; |
dde0eeef | 95 | unsigned int read_pid; |
ab33e65c | 96 | |
772030fe | 97 | while ((dirent = readdir(dir))) { |
ab33e65c | 98 | if (!strcmp(dirent->d_name, ".") || |
dde0eeef NC |
99 | !strcmp(dirent->d_name, "..") || |
100 | !strcmp(dirent->d_name, "ust-consumer") || | |
101 | dirent->d_type == DT_DIR) { | |
ab33e65c PP |
102 | |
103 | continue; | |
104 | } | |
105 | ||
dde0eeef NC |
106 | sscanf(dirent->d_name, "%u", &read_pid); |
107 | ||
108 | (*pid_list)[*pid_list_size - 1] = read_pid; | |
109 | /* FIXME: Here we previously called pid_is_online, which | |
110 | * always returned 1, now I replaced it with just 1. | |
111 | * We need to figure out an intelligent way of solving | |
112 | * this, maybe connect-disconnect. | |
113 | */ | |
114 | if (1) { | |
115 | (*pid_list_size)++; | |
116 | *pid_list = realloc(*pid_list, | |
117 | *pid_list_size * sizeof(pid_t)); | |
ab33e65c PP |
118 | } |
119 | } | |
120 | ||
dde0eeef NC |
121 | (*pid_list)[*pid_list_size - 1] = 0; /* Array end */ |
122 | } | |
ab33e65c | 123 | |
1031eea2 | 124 | static pid_t *get_pids_non_root(void) |
dde0eeef NC |
125 | { |
126 | char *dir_name; | |
127 | DIR *dir; | |
128 | unsigned int pid_list_size = 1; | |
129 | pid_t *pid_list = NULL; | |
130 | ||
131 | dir_name = ustcomm_user_sock_dir(); | |
132 | if (!dir_name) { | |
ab33e65c PP |
133 | return NULL; |
134 | } | |
135 | ||
dde0eeef NC |
136 | dir = opendir(dir_name); |
137 | if (!dir) { | |
138 | goto free_dir_name; | |
139 | } | |
140 | ||
141 | pid_list = malloc(pid_list_size * sizeof(pid_t)); | |
1031eea2 NC |
142 | if (!pid_list) { |
143 | goto close_dir; | |
144 | } | |
dde0eeef NC |
145 | |
146 | get_pids_in_dir(dir, &pid_list, &pid_list_size); | |
147 | ||
148 | if (pid_list[0] == 0) { | |
149 | /* No PID at all */ | |
150 | free(pid_list); | |
151 | pid_list = NULL; | |
152 | goto close_dir; | |
153 | } | |
154 | ||
155 | close_dir: | |
ab33e65c | 156 | closedir(dir); |
dde0eeef NC |
157 | |
158 | free_dir_name: | |
159 | free(dir_name); | |
160 | ||
161 | return pid_list; | |
ab33e65c PP |
162 | } |
163 | ||
1031eea2 NC |
164 | static pid_t *get_pids_root(void) |
165 | { | |
166 | char *dir_name; | |
167 | DIR *tmp_dir, *dir; | |
168 | unsigned int pid_list_size = 1; | |
169 | pid_t *pid_list = NULL; | |
170 | struct dirent *dirent; | |
171 | ||
172 | tmp_dir = opendir(USER_TMP_DIR); | |
173 | if (!tmp_dir) { | |
174 | return NULL; | |
175 | } | |
176 | ||
177 | pid_list = malloc(pid_list_size * sizeof(pid_t)); | |
178 | if (!pid_list) { | |
179 | goto close_tmp_dir; | |
180 | } | |
181 | ||
182 | while ((dirent = readdir(tmp_dir))) { | |
183 | /* Compare the dir to check for the USER_SOCK_DIR_BASE prefix */ | |
184 | if (!strncmp(dirent->d_name, USER_SOCK_DIR_BASE, | |
185 | strlen(USER_SOCK_DIR_BASE))) { | |
186 | ||
187 | if (asprintf(&dir_name, USER_TMP_DIR "/%s", dirent->d_name) < 0) { | |
188 | goto close_tmp_dir; | |
189 | } | |
190 | ||
191 | dir = opendir(dir_name); | |
192 | ||
193 | free(dir_name); | |
194 | ||
195 | if (!dir) { | |
196 | continue; | |
197 | } | |
198 | ||
199 | get_pids_in_dir(dir, &pid_list, &pid_list_size); | |
200 | ||
201 | closedir(dir); | |
202 | } | |
203 | } | |
204 | ||
205 | close_tmp_dir: | |
206 | closedir(tmp_dir); | |
207 | ||
208 | return pid_list; | |
209 | } | |
210 | ||
211 | pid_t *ustctl_get_online_pids(void) | |
212 | { | |
213 | if (geteuid()) { | |
214 | return get_pids_non_root(); | |
215 | } else { | |
216 | return get_pids_root(); | |
217 | } | |
218 | } | |
219 | ||
ab33e65c | 220 | /** |
2298f329 | 221 | * Sets marker state (USTCTL_MS_ON or USTCTL_MS_OFF). |
ab33e65c PP |
222 | * |
223 | * @param mn Marker name | |
224 | * @param state Marker's new state | |
225 | * @param pid Traced process ID | |
2298f329 | 226 | * @return 0 if successful, or errors {USTCTL_ERR_GEN, USTCTL_ERR_ARG} |
ab33e65c | 227 | */ |
8b26d56b NC |
228 | int ustctl_set_marker_state(int sock, const char *trace, const char *channel, |
229 | const char *marker, int state) | |
ef290fca | 230 | { |
72098143 NC |
231 | struct ustcomm_header req_header, res_header; |
232 | struct ustcomm_marker_info marker_inf; | |
ef290fca PMF |
233 | int result; |
234 | ||
72098143 NC |
235 | result = ustcomm_pack_marker_info(&req_header, |
236 | &marker_inf, | |
d89b8191 | 237 | trace, |
72098143 NC |
238 | channel, |
239 | marker); | |
240 | if (result < 0) { | |
241 | errno = -result; | |
242 | return -1; | |
08b8805e | 243 | } |
ab33e65c | 244 | |
72098143 | 245 | req_header.command = state ? ENABLE_MARKER : DISABLE_MARKER; |
ab33e65c | 246 | |
8b26d56b | 247 | return do_cmd(sock, &req_header, (char *)&marker_inf, |
72098143 | 248 | &res_header, NULL); |
ab33e65c PP |
249 | } |
250 | ||
763f41e5 DS |
251 | /** |
252 | * Set subbuffer size. | |
253 | * | |
254 | * @param channel_size Channel name and size | |
255 | * @param pid Traced process ID | |
256 | * @return 0 if successful, or error | |
257 | */ | |
8b26d56b NC |
258 | int ustctl_set_subbuf_size(int sock, const char *trace, const char *channel, |
259 | unsigned int subbuf_size) | |
763f41e5 | 260 | { |
72098143 NC |
261 | struct ustcomm_header req_header, res_header; |
262 | struct ustcomm_channel_info ch_inf; | |
763f41e5 DS |
263 | int result; |
264 | ||
72098143 NC |
265 | result = ustcomm_pack_channel_info(&req_header, |
266 | &ch_inf, | |
d89b8191 | 267 | trace, |
72098143 NC |
268 | channel); |
269 | if (result < 0) { | |
270 | errno = -result; | |
08b8805e DG |
271 | return -1; |
272 | } | |
763f41e5 | 273 | |
72098143 NC |
274 | req_header.command = SET_SUBBUF_SIZE; |
275 | ch_inf.subbuf_size = subbuf_size; | |
763f41e5 | 276 | |
8b26d56b | 277 | return do_cmd(sock, &req_header, (char *)&ch_inf, |
72098143 | 278 | &res_header, NULL); |
763f41e5 DS |
279 | } |
280 | ||
281 | /** | |
282 | * Set subbuffer num. | |
283 | * | |
284 | * @param channel_num Channel name and num | |
285 | * @param pid Traced process ID | |
286 | * @return 0 if successful, or error | |
287 | */ | |
8b26d56b NC |
288 | int ustctl_set_subbuf_num(int sock, const char *trace, const char *channel, |
289 | unsigned int num) | |
763f41e5 | 290 | { |
72098143 NC |
291 | struct ustcomm_header req_header, res_header; |
292 | struct ustcomm_channel_info ch_inf; | |
763f41e5 DS |
293 | int result; |
294 | ||
72098143 NC |
295 | result = ustcomm_pack_channel_info(&req_header, |
296 | &ch_inf, | |
d89b8191 | 297 | trace, |
72098143 NC |
298 | channel); |
299 | if (result < 0) { | |
300 | errno = -result; | |
08b8805e DG |
301 | return -1; |
302 | } | |
763f41e5 | 303 | |
72098143 NC |
304 | req_header.command = SET_SUBBUF_NUM; |
305 | ch_inf.subbuf_num = num; | |
306 | ||
8b26d56b | 307 | return do_cmd(sock, &req_header, (char *)&ch_inf, |
72098143 | 308 | &res_header, NULL); |
763f41e5 | 309 | |
763f41e5 DS |
310 | } |
311 | ||
8b26d56b NC |
312 | |
313 | static int ustctl_get_subbuf_num_size(int sock, const char *trace, const char *channel, | |
314 | int *num, int *size) | |
e77b8e8e | 315 | { |
72098143 NC |
316 | struct ustcomm_header req_header, res_header; |
317 | struct ustcomm_channel_info ch_inf, *ch_inf_res; | |
e77b8e8e DS |
318 | int result; |
319 | ||
72098143 NC |
320 | |
321 | result = ustcomm_pack_channel_info(&req_header, | |
322 | &ch_inf, | |
d89b8191 | 323 | trace, |
72098143 NC |
324 | channel); |
325 | if (result < 0) { | |
326 | errno = -result; | |
08b8805e DG |
327 | return -1; |
328 | } | |
e77b8e8e | 329 | |
72098143 NC |
330 | req_header.command = GET_SUBBUF_NUM_SIZE; |
331 | ||
8b26d56b | 332 | result = do_cmd(sock, &req_header, (char *)&ch_inf, |
72098143 NC |
333 | &res_header, (char **)&ch_inf_res); |
334 | if (result < 0) { | |
e77b8e8e DS |
335 | return -1; |
336 | } | |
337 | ||
72098143 NC |
338 | *num = ch_inf_res->subbuf_num; |
339 | *size = ch_inf_res->subbuf_size; | |
340 | ||
341 | free(ch_inf_res); | |
342 | ||
343 | return 0; | |
e77b8e8e DS |
344 | } |
345 | ||
346 | /** | |
347 | * Get subbuffer num. | |
348 | * | |
349 | * @param channel Channel name | |
350 | * @param pid Traced process ID | |
351 | * @return subbuf cnf if successful, or error | |
352 | */ | |
8b26d56b | 353 | int ustctl_get_subbuf_num(int sock, const char *trace, const char *channel) |
e77b8e8e | 354 | { |
72098143 | 355 | int num, size, result; |
e77b8e8e | 356 | |
8b26d56b | 357 | result = ustctl_get_subbuf_num_size(sock, trace, channel, |
72098143 NC |
358 | &num, &size); |
359 | if (result < 0) { | |
360 | errno = -result; | |
08b8805e DG |
361 | return -1; |
362 | } | |
e77b8e8e | 363 | |
72098143 NC |
364 | return num; |
365 | } | |
366 | ||
367 | /** | |
368 | * Get subbuffer size. | |
369 | * | |
370 | * @param channel Channel name | |
371 | * @param pid Traced process ID | |
372 | * @return subbuf size if successful, or error | |
373 | */ | |
8b26d56b | 374 | int ustctl_get_subbuf_size(int sock, const char *trace, const char *channel) |
72098143 NC |
375 | { |
376 | int num, size, result; | |
377 | ||
8b26d56b | 378 | result = ustctl_get_subbuf_num_size(sock, trace, channel, |
72098143 NC |
379 | &num, &size); |
380 | if (result < 0) { | |
381 | errno = -result; | |
e77b8e8e DS |
382 | return -1; |
383 | } | |
384 | ||
72098143 | 385 | return size; |
e77b8e8e | 386 | } |
763f41e5 | 387 | |
8b26d56b | 388 | static int do_trace_cmd(int sock, const char *trace, int command) |
d89b8191 NC |
389 | { |
390 | struct ustcomm_header req_header, res_header; | |
28c1bb40 | 391 | struct ustcomm_single_field trace_inf; |
d89b8191 NC |
392 | int result; |
393 | ||
28c1bb40 NC |
394 | result = ustcomm_pack_single_field(&req_header, |
395 | &trace_inf, | |
396 | trace); | |
d89b8191 NC |
397 | if (result < 0) { |
398 | errno = -result; | |
399 | return -1; | |
400 | } | |
401 | ||
402 | req_header.command = command; | |
403 | ||
8b26d56b | 404 | return do_cmd(sock, &req_header, (char *)&trace_inf, &res_header, NULL); |
d89b8191 NC |
405 | } |
406 | ||
ab33e65c PP |
407 | /** |
408 | * Destroys an UST trace according to a PID. | |
409 | * | |
410 | * @param pid Traced process ID | |
2298f329 | 411 | * @return 0 if successful, or error USTCTL_ERR_GEN |
ab33e65c | 412 | */ |
8b26d56b | 413 | int ustctl_destroy_trace(int sock, const char *trace) |
772030fe | 414 | { |
8b26d56b | 415 | return do_trace_cmd(sock, trace, DESTROY_TRACE); |
ab33e65c PP |
416 | } |
417 | ||
418 | /** | |
419 | * Starts an UST trace (and setups it) according to a PID. | |
420 | * | |
421 | * @param pid Traced process ID | |
2298f329 | 422 | * @return 0 if successful, or error USTCTL_ERR_GEN |
ab33e65c | 423 | */ |
8b26d56b | 424 | int ustctl_setup_and_start(int sock, const char *trace) |
772030fe | 425 | { |
8b26d56b | 426 | return do_trace_cmd(sock, trace, START); |
ab33e65c PP |
427 | } |
428 | ||
62ec620f PMF |
429 | /** |
430 | * Creates an UST trace according to a PID. | |
431 | * | |
432 | * @param pid Traced process ID | |
2298f329 | 433 | * @return 0 if successful, or error USTCTL_ERR_GEN |
62ec620f | 434 | */ |
8b26d56b | 435 | int ustctl_create_trace(int sock, const char *trace) |
62ec620f | 436 | { |
8b26d56b | 437 | return do_trace_cmd(sock, trace, CREATE_TRACE); |
62ec620f PMF |
438 | } |
439 | ||
ab33e65c PP |
440 | /** |
441 | * Starts an UST trace according to a PID. | |
442 | * | |
443 | * @param pid Traced process ID | |
2298f329 | 444 | * @return 0 if successful, or error USTCTL_ERR_GEN |
ab33e65c | 445 | */ |
8b26d56b | 446 | int ustctl_start_trace(int sock, const char *trace) |
772030fe | 447 | { |
8b26d56b | 448 | return do_trace_cmd(sock, trace, START_TRACE); |
ab33e65c PP |
449 | } |
450 | ||
763f41e5 DS |
451 | /** |
452 | * Alloc an UST trace according to a PID. | |
453 | * | |
454 | * @param pid Traced process ID | |
2298f329 | 455 | * @return 0 if successful, or error USTCTL_ERR_GEN |
763f41e5 | 456 | */ |
8b26d56b | 457 | int ustctl_alloc_trace(int sock, const char *trace) |
763f41e5 | 458 | { |
8b26d56b | 459 | return do_trace_cmd(sock, trace, ALLOC_TRACE); |
763f41e5 DS |
460 | } |
461 | ||
e005efaa NC |
462 | |
463 | int ustctl_force_switch(int sock, const char *trace) | |
464 | { | |
465 | return do_trace_cmd(sock, trace, FORCE_SUBBUF_SWITCH); | |
466 | } | |
467 | ||
ab33e65c PP |
468 | /** |
469 | * Stops an UST trace according to a PID. | |
470 | * | |
471 | * @param pid Traced process ID | |
2298f329 | 472 | * @return 0 if successful, or error USTCTL_ERR_GEN |
ab33e65c | 473 | */ |
8b26d56b | 474 | int ustctl_stop_trace(int sock, const char *trace) |
772030fe | 475 | { |
8b26d56b | 476 | return do_trace_cmd(sock, trace, STOP_TRACE); |
ab33e65c PP |
477 | } |
478 | ||
479 | /** | |
480 | * Counts newlines ('\n') in a string. | |
481 | * | |
482 | * @param str String to search in | |
483 | * @return Total newlines count | |
484 | */ | |
2298f329 | 485 | unsigned int ustctl_count_nl(const char *str) |
772030fe | 486 | { |
ab33e65c PP |
487 | unsigned int i = 0, tot = 0; |
488 | ||
489 | while (str[i] != '\0') { | |
490 | if (str[i] == '\n') { | |
491 | ++tot; | |
492 | } | |
493 | ++i; | |
494 | } | |
495 | ||
496 | return tot; | |
497 | } | |
498 | ||
499 | /** | |
500 | * Frees a CMSF array. | |
501 | * | |
502 | * @param cmsf CMSF array to free | |
2298f329 | 503 | * @return 0 if successful, or error USTCTL_ERR_ARG |
ab33e65c | 504 | */ |
2298f329 | 505 | int ustctl_free_cmsf(struct marker_status *cmsf) |
772030fe | 506 | { |
ab33e65c | 507 | if (cmsf == NULL) { |
2298f329 | 508 | return USTCTL_ERR_ARG; |
ab33e65c PP |
509 | } |
510 | ||
511 | unsigned int i = 0; | |
512 | while (cmsf[i].channel != NULL) { | |
513 | free(cmsf[i].channel); | |
514 | free(cmsf[i].marker); | |
515 | free(cmsf[i].fs); | |
516 | ++i; | |
517 | } | |
518 | free(cmsf); | |
519 | ||
520 | return 0; | |
521 | } | |
522 | ||
523 | /** | |
524 | * Gets channel/marker/state/format string for a given PID. | |
525 | * | |
526 | * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller | |
2298f329 | 527 | * frees with `ustctl_free_cmsf') |
ab33e65c | 528 | * @param pid Targeted PID |
2a79ceeb | 529 | * @return 0 if successful, or -1 on error |
ab33e65c | 530 | */ |
8b26d56b | 531 | int ustctl_get_cmsf(int sock, struct marker_status **cmsf) |
772030fe | 532 | { |
72098143 | 533 | struct ustcomm_header req_header, res_header; |
08230db7 | 534 | char *big_str = NULL; |
8b26d56b | 535 | int result; |
08230db7 | 536 | struct marker_status *tmp_cmsf = NULL; |
ef290fca PMF |
537 | unsigned int i = 0, cmsf_ind = 0; |
538 | ||
ab33e65c | 539 | if (cmsf == NULL) { |
2a79ceeb | 540 | return -1; |
ab33e65c | 541 | } |
72098143 | 542 | |
72098143 NC |
543 | req_header.command = LIST_MARKERS; |
544 | req_header.size = 0; | |
545 | ||
8b26d56b | 546 | result = ustcomm_send(sock, &req_header, NULL); |
72098143 | 547 | if (result <= 0) { |
8b26d56b | 548 | PERROR("error while requesting markers list"); |
2a79ceeb | 549 | return -1; |
ab33e65c PP |
550 | } |
551 | ||
8b26d56b | 552 | result = ustcomm_recv_alloc(sock, &res_header, &big_str); |
72098143 NC |
553 | if (result <= 0) { |
554 | ERR("error while receiving markers list"); | |
555 | return -1; | |
556 | } | |
557 | ||
72098143 | 558 | tmp_cmsf = (struct marker_status *) zmalloc(sizeof(struct marker_status) * |
2298f329 | 559 | (ustctl_count_nl(big_str) + 1)); |
ab33e65c | 560 | if (tmp_cmsf == NULL) { |
dc46f6e6 | 561 | ERR("Failed to allocate CMSF array"); |
2a79ceeb | 562 | return -1; |
ab33e65c PP |
563 | } |
564 | ||
77957c95 | 565 | /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */ |
ab33e65c | 566 | while (big_str[i] != '\0') { |
ab33e65c | 567 | char state; |
ef290fca | 568 | |
264f6231 | 569 | sscanf(big_str + i, "marker: %a[^/]/%a[^ ] %c %a[^\n]", |
72098143 NC |
570 | &tmp_cmsf[cmsf_ind].channel, |
571 | &tmp_cmsf[cmsf_ind].marker, | |
572 | &state, | |
573 | &tmp_cmsf[cmsf_ind].fs); | |
2298f329 NC |
574 | tmp_cmsf[cmsf_ind].state = (state == USTCTL_MS_CHR_ON ? |
575 | USTCTL_MS_ON : USTCTL_MS_OFF); /* Marker state */ | |
ab33e65c PP |
576 | |
577 | while (big_str[i] != '\n') { | |
77957c95 | 578 | ++i; /* Go to next '\n' */ |
ab33e65c | 579 | } |
77957c95 | 580 | ++i; /* Skip current pointed '\n' */ |
ab33e65c PP |
581 | ++cmsf_ind; |
582 | } | |
583 | tmp_cmsf[cmsf_ind].channel = NULL; | |
584 | tmp_cmsf[cmsf_ind].marker = NULL; | |
585 | tmp_cmsf[cmsf_ind].fs = NULL; | |
586 | ||
587 | *cmsf = tmp_cmsf; | |
588 | ||
589 | free(big_str); | |
590 | return 0; | |
591 | } | |
592 | ||
a3adfb05 NC |
593 | /** |
594 | * Frees a TES array. | |
595 | * | |
596 | * @param tes TES array to free | |
2298f329 | 597 | * @return 0 if successful, or error USTCTL_ERR_ARG |
a3adfb05 | 598 | */ |
2298f329 | 599 | int ustctl_free_tes(struct trace_event_status *tes) |
a3adfb05 NC |
600 | { |
601 | if (tes == NULL) { | |
2298f329 | 602 | return USTCTL_ERR_ARG; |
a3adfb05 NC |
603 | } |
604 | ||
605 | unsigned int i = 0; | |
606 | while (tes[i].name != NULL) { | |
607 | free(tes[i].name); | |
608 | ++i; | |
609 | } | |
610 | free(tes); | |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
615 | /** | |
616 | * Gets trace_events string for a given PID. | |
617 | * | |
618 | * @param tes Pointer to TES array to be filled (callee allocates, caller | |
2298f329 | 619 | * frees with `ustctl_free_tes') |
a3adfb05 NC |
620 | * @param pid Targeted PID |
621 | * @return 0 if successful, or -1 on error | |
622 | */ | |
8b26d56b | 623 | int ustctl_get_tes(int sock, struct trace_event_status **tes) |
a3adfb05 | 624 | { |
72098143 | 625 | struct ustcomm_header req_header, res_header; |
a3adfb05 | 626 | char *big_str = NULL; |
8b26d56b | 627 | int result; |
a3adfb05 NC |
628 | struct trace_event_status *tmp_tes = NULL; |
629 | unsigned int i = 0, tes_ind = 0; | |
630 | ||
631 | if (tes == NULL) { | |
632 | return -1; | |
633 | } | |
634 | ||
72098143 NC |
635 | req_header.command = LIST_TRACE_EVENTS; |
636 | req_header.size = 0; | |
637 | ||
8b26d56b | 638 | result = ustcomm_send(sock, &req_header, NULL); |
72098143 NC |
639 | if (result != 1) { |
640 | ERR("error while requesting trace_event list"); | |
641 | return -1; | |
642 | } | |
643 | ||
8b26d56b | 644 | result = ustcomm_recv_alloc(sock, &res_header, &big_str); |
a3adfb05 | 645 | if (result != 1) { |
72098143 | 646 | ERR("error while receiving markers list"); |
a3adfb05 NC |
647 | return -1; |
648 | } | |
649 | ||
650 | tmp_tes = (struct trace_event_status *) | |
651 | zmalloc(sizeof(struct trace_event_status) * | |
2298f329 | 652 | (ustctl_count_nl(big_str) + 1)); |
a3adfb05 NC |
653 | if (tmp_tes == NULL) { |
654 | ERR("Failed to allocate TES array"); | |
655 | return -1; | |
656 | } | |
657 | ||
658 | /* Parse received reply string (format: "[name]"): */ | |
659 | while (big_str[i] != '\0') { | |
a3adfb05 | 660 | sscanf(big_str + i, "trace_event: %a[^\n]", |
72098143 | 661 | &tmp_tes[tes_ind].name); |
a3adfb05 NC |
662 | while (big_str[i] != '\n') { |
663 | ++i; /* Go to next '\n' */ | |
664 | } | |
665 | ++i; /* Skip current pointed '\n' */ | |
666 | ++tes_ind; | |
667 | } | |
668 | tmp_tes[tes_ind].name = NULL; | |
669 | ||
670 | *tes = tmp_tes; | |
671 | ||
672 | free(big_str); | |
673 | return 0; | |
674 | } | |
675 | ||
b2fb2f91 | 676 | /** |
8b26d56b | 677 | * Set sock path |
b2fb2f91 | 678 | * |
8b26d56b | 679 | * @param sock_path Sock path |
b2fb2f91 AH |
680 | * @param pid Traced process ID |
681 | * @return 0 if successful, or error | |
682 | */ | |
8b26d56b | 683 | int ustctl_set_sock_path(int sock, const char *sock_path) |
b2fb2f91 | 684 | { |
28c1bb40 | 685 | int result; |
72098143 | 686 | struct ustcomm_header req_header, res_header; |
28c1bb40 | 687 | struct ustcomm_single_field sock_path_msg; |
72098143 | 688 | |
28c1bb40 NC |
689 | result = ustcomm_pack_single_field(&req_header, |
690 | &sock_path_msg, | |
691 | sock_path); | |
692 | if (result < 0) { | |
693 | errno = -result; | |
08b8805e DG |
694 | return -1; |
695 | } | |
b2fb2f91 | 696 | |
72098143 | 697 | req_header.command = SET_SOCK_PATH; |
b2fb2f91 | 698 | |
8b26d56b | 699 | return do_cmd(sock, &req_header, (char *)&sock_path_msg, |
72098143 | 700 | &res_header, NULL); |
b2fb2f91 AH |
701 | } |
702 | ||
703 | /** | |
8b26d56b | 704 | * Get sock path |
b2fb2f91 | 705 | * |
8b26d56b | 706 | * @param sock_path Pointer to where the sock path will be returned |
b2fb2f91 AH |
707 | * @param pid Traced process ID |
708 | * @return 0 if successful, or error | |
709 | */ | |
8b26d56b | 710 | int ustctl_get_sock_path(int sock, char **sock_path) |
b2fb2f91 | 711 | { |
b2fb2f91 | 712 | int result; |
72098143 | 713 | struct ustcomm_header req_header, res_header; |
28c1bb40 | 714 | struct ustcomm_single_field *sock_path_msg; |
72098143 NC |
715 | |
716 | req_header.command = GET_SOCK_PATH; | |
717 | req_header.size = 0; | |
b2fb2f91 | 718 | |
8b26d56b | 719 | result = do_cmd(sock, &req_header, NULL, &res_header, |
72098143 NC |
720 | (char **)&sock_path_msg); |
721 | if (result < 0) { | |
722 | return -1; | |
08b8805e | 723 | } |
b2fb2f91 | 724 | |
28c1bb40 | 725 | result = ustcomm_unpack_single_field(sock_path_msg); |
72098143 NC |
726 | if (result < 0) { |
727 | return result; | |
b2fb2f91 AH |
728 | } |
729 | ||
28c1bb40 | 730 | *sock_path = strdup(sock_path_msg->field); |
b2fb2f91 | 731 | |
72098143 | 732 | free(sock_path_msg); |
b9318b35 AH |
733 | |
734 | return 0; | |
735 | } |