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