From: Nils Carlson Date: Fri, 4 Feb 2011 07:24:51 +0000 (+0100) Subject: libustctl: use direct socket communication X-Git-Tag: v0.12~25 X-Git-Url: http://git.lttng.org/?p=ust.git;a=commitdiff_plain;h=8b26d56b9988ac0c09999c14a08bc28d42551367 libustctl: use direct socket communication This patch changes libustctl to be socket instead of pid oriented. The user is expected to connect to a pid using ustctl_connect_pid(pid_t) which returns a socket file-descriptor and then use the socket for the rest of the api. This reduces the amount of open and closing systemcalls and also makes it possible for a session daemon to detect process shutdown by the socket closing. David, this ones for you. :-) Signed-off-by: Nils Carlson Acked-by: Mathieu Desnoyers --- diff --git a/include/ust/ustctl.h b/include/ust/ustctl.h index 195ee26..35f7617 100644 --- a/include/ust/ustctl.h +++ b/include/ust/ustctl.h @@ -48,29 +48,51 @@ struct trace_event_status { }; extern pid_t *ustctl_get_online_pids(void); -extern int ustctl_set_marker_state(const char *trace, const char *channel, - const char *marker, int state, pid_t pid); -extern int ustctl_set_subbuf_size(const char *trace, const char *channel, - unsigned int subbuf_size, pid_t pid); -extern int ustctl_set_subbuf_num(const char *trace, const char *channel, - unsigned int num, pid_t pid); -extern int ustctl_get_subbuf_size(const char *trace, const char *channel, - pid_t pid); -extern int ustctl_get_subbuf_num(const char *trace, const char *channel, - pid_t pid); -extern int ustctl_destroy_trace(const char *trace, pid_t pid); -extern int ustctl_setup_and_start(const char *trace, pid_t pid); -extern int ustctl_stop_trace(const char *trace, pid_t pid); -extern int ustctl_create_trace(const char *trace, pid_t pid); -extern int ustctl_start_trace(const char *trace, pid_t pid); -extern int ustctl_alloc_trace(const char *trace, pid_t pid); + +extern int ustctl_connect_pid(pid_t pid); + +extern int ustctl_set_marker_state(int sock, const char *trace, + const char *channel, const char *marker, + int state); + +extern int ustctl_set_subbuf_size(int sock, const char *trace, + const char *channel, + unsigned int subbuf_size); + +extern int ustctl_set_subbuf_num(int sock, const char *trace, + const char *channel, + unsigned int num); + +extern int ustctl_get_subbuf_size(int sock, const char *trace, + const char *channel); + +extern int ustctl_get_subbuf_num(pid_t pid, const char *trace, + const char *channel); + +extern int ustctl_destroy_trace(int sock, const char *trace); + +extern int ustctl_setup_and_start(int sock, const char *trace); + +extern int ustctl_stop_trace(int sock, const char *trace); + +extern int ustctl_create_trace(int sock, const char *trace); + +extern int ustctl_start_trace(int sock, const char *trace); + +extern int ustctl_alloc_trace(int sock, const char *trace); + extern int ustctl_free_cmsf(struct marker_status *); -extern unsigned int ustctl_count_nl(const char *); -extern int ustctl_get_cmsf(struct marker_status **, pid_t); extern int ustctl_free_tes(struct trace_event_status *); -extern int ustctl_get_tes(struct trace_event_status **, pid_t); -extern int ustctl_set_sock_path(const char *sock_path, pid_t pid); -extern int ustctl_get_sock_path(char **sock_path, pid_t pid); -extern int ustctl_force_switch(pid_t pid); +extern unsigned int ustctl_count_nl(const char *); + +extern int ustctl_get_cmsf(int sock, struct marker_status **); + +extern int ustctl_get_tes(int sock, struct trace_event_status **); + +extern int ustctl_set_sock_path(int sock, const char *sock_path); + +extern int ustctl_get_sock_path(int sock, char **sock_path); + +extern int ustctl_force_switch(int sock, const char *trace); #endif /* _USTCTL_H */ diff --git a/libustctl/libustctl.c b/libustctl/libustctl.c index 9c4ced2..e9709c5 100644 --- a/libustctl/libustctl.c +++ b/libustctl/libustctl.c @@ -1,4 +1,5 @@ /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette + * Copyright (C) 2011 Ericsson AB * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -28,28 +29,22 @@ #include "ust/ustctl.h" #include "usterr.h" -static int do_cmd(const pid_t pid, +static int do_cmd(int sock, const struct ustcomm_header *req_header, const char *req_data, struct ustcomm_header *res_header, char **res_data) { - int app_fd, result, saved_errno = 0; + int result, saved_errno = 0; char *recv_buf; - if (ustcomm_connect_app(pid, &app_fd)) { - ERR("could not connect to PID %u", (unsigned int) pid); - errno = ENOTCONN; - return -1; - } - recv_buf = zmalloc(USTCOMM_BUFFER_SIZE); if (!recv_buf) { saved_errno = ENOMEM; - goto close_app_fd; + goto out; } - result = ustcomm_req(app_fd, req_header, req_data, res_header, recv_buf); + result = ustcomm_req(sock, req_header, req_data, res_header, recv_buf); if (result > 0) { saved_errno = -res_header->result; if (res_header->size == 0 || saved_errno > 0) { @@ -71,11 +66,8 @@ static int do_cmd(const pid_t pid, free(recv_buf); } -close_app_fd: - close(app_fd); - +out: errno = saved_errno; - if (errno) { return -1; } @@ -83,6 +75,19 @@ close_app_fd: return 0; } +int ustctl_connect_pid(pid_t pid) +{ + int sock; + + if (ustcomm_connect_app(pid, &sock)) { + ERR("could not connect to PID %u", (unsigned int) pid); + errno = ENOTCONN; + return -1; + } + + return sock; +} + pid_t *ustctl_get_online_pids(void) { struct dirent *dirent; @@ -140,8 +145,8 @@ pid_t *ustctl_get_online_pids(void) * @param pid Traced process ID * @return 0 if successful, or errors {USTCTL_ERR_GEN, USTCTL_ERR_ARG} */ -int ustctl_set_marker_state(const char *trace, const char *channel, - const char *marker, int state, pid_t pid) +int ustctl_set_marker_state(int sock, const char *trace, const char *channel, + const char *marker, int state) { struct ustcomm_header req_header, res_header; struct ustcomm_marker_info marker_inf; @@ -159,7 +164,7 @@ int ustctl_set_marker_state(const char *trace, const char *channel, req_header.command = state ? ENABLE_MARKER : DISABLE_MARKER; - return do_cmd(pid, &req_header, (char *)&marker_inf, + return do_cmd(sock, &req_header, (char *)&marker_inf, &res_header, NULL); } @@ -170,8 +175,8 @@ int ustctl_set_marker_state(const char *trace, const char *channel, * @param pid Traced process ID * @return 0 if successful, or error */ -int ustctl_set_subbuf_size(const char *trace, const char *channel, - unsigned int subbuf_size, pid_t pid) +int ustctl_set_subbuf_size(int sock, const char *trace, const char *channel, + unsigned int subbuf_size) { struct ustcomm_header req_header, res_header; struct ustcomm_channel_info ch_inf; @@ -189,7 +194,7 @@ int ustctl_set_subbuf_size(const char *trace, const char *channel, req_header.command = SET_SUBBUF_SIZE; ch_inf.subbuf_size = subbuf_size; - return do_cmd(pid, &req_header, (char *)&ch_inf, + return do_cmd(sock, &req_header, (char *)&ch_inf, &res_header, NULL); } @@ -200,8 +205,8 @@ int ustctl_set_subbuf_size(const char *trace, const char *channel, * @param pid Traced process ID * @return 0 if successful, or error */ -int ustctl_set_subbuf_num(const char *trace, const char *channel, - unsigned int num, pid_t pid) +int ustctl_set_subbuf_num(int sock, const char *trace, const char *channel, + unsigned int num) { struct ustcomm_header req_header, res_header; struct ustcomm_channel_info ch_inf; @@ -219,13 +224,14 @@ int ustctl_set_subbuf_num(const char *trace, const char *channel, req_header.command = SET_SUBBUF_NUM; ch_inf.subbuf_num = num; - return do_cmd(pid, &req_header, (char *)&ch_inf, + return do_cmd(sock, &req_header, (char *)&ch_inf, &res_header, NULL); } -static int ustctl_get_subbuf_num_size(const char *trace, const char *channel, - pid_t pid, int *num, int *size) + +static int ustctl_get_subbuf_num_size(int sock, const char *trace, const char *channel, + int *num, int *size) { struct ustcomm_header req_header, res_header; struct ustcomm_channel_info ch_inf, *ch_inf_res; @@ -243,7 +249,7 @@ static int ustctl_get_subbuf_num_size(const char *trace, const char *channel, req_header.command = GET_SUBBUF_NUM_SIZE; - result = do_cmd(pid, &req_header, (char *)&ch_inf, + result = do_cmd(sock, &req_header, (char *)&ch_inf, &res_header, (char **)&ch_inf_res); if (result < 0) { return -1; @@ -264,11 +270,11 @@ static int ustctl_get_subbuf_num_size(const char *trace, const char *channel, * @param pid Traced process ID * @return subbuf cnf if successful, or error */ -int ustctl_get_subbuf_num(const char *trace, const char *channel, pid_t pid) +int ustctl_get_subbuf_num(int sock, const char *trace, const char *channel) { int num, size, result; - result = ustctl_get_subbuf_num_size(trace, channel, pid, + result = ustctl_get_subbuf_num_size(sock, trace, channel, &num, &size); if (result < 0) { errno = -result; @@ -285,11 +291,11 @@ int ustctl_get_subbuf_num(const char *trace, const char *channel, pid_t pid) * @param pid Traced process ID * @return subbuf size if successful, or error */ -int ustctl_get_subbuf_size(const char *trace, const char *channel, pid_t pid) +int ustctl_get_subbuf_size(int sock, const char *trace, const char *channel) { int num, size, result; - result = ustctl_get_subbuf_num_size(trace, channel, pid, + result = ustctl_get_subbuf_num_size(sock, trace, channel, &num, &size); if (result < 0) { errno = -result; @@ -299,8 +305,7 @@ int ustctl_get_subbuf_size(const char *trace, const char *channel, pid_t pid) return size; } - -static int do_trace_cmd(const char *trace, pid_t pid, int command) +static int do_trace_cmd(int sock, const char *trace, int command) { struct ustcomm_header req_header, res_header; struct ustcomm_single_field trace_inf; @@ -316,7 +321,7 @@ static int do_trace_cmd(const char *trace, pid_t pid, int command) req_header.command = command; - return do_cmd(pid, &req_header, (char *)&trace_inf, &res_header, NULL); + return do_cmd(sock, &req_header, (char *)&trace_inf, &res_header, NULL); } /** @@ -325,9 +330,9 @@ static int do_trace_cmd(const char *trace, pid_t pid, int command) * @param pid Traced process ID * @return 0 if successful, or error USTCTL_ERR_GEN */ -int ustctl_destroy_trace(const char *trace, pid_t pid) +int ustctl_destroy_trace(int sock, const char *trace) { - return do_trace_cmd(trace, pid, DESTROY_TRACE); + return do_trace_cmd(sock, trace, DESTROY_TRACE); } /** @@ -336,9 +341,9 @@ int ustctl_destroy_trace(const char *trace, pid_t pid) * @param pid Traced process ID * @return 0 if successful, or error USTCTL_ERR_GEN */ -int ustctl_setup_and_start(const char *trace, pid_t pid) +int ustctl_setup_and_start(int sock, const char *trace) { - return do_trace_cmd(trace, pid, START); + return do_trace_cmd(sock, trace, START); } /** @@ -347,9 +352,9 @@ int ustctl_setup_and_start(const char *trace, pid_t pid) * @param pid Traced process ID * @return 0 if successful, or error USTCTL_ERR_GEN */ -int ustctl_create_trace(const char *trace, pid_t pid) +int ustctl_create_trace(int sock, const char *trace) { - return do_trace_cmd(trace, pid, CREATE_TRACE); + return do_trace_cmd(sock, trace, CREATE_TRACE); } /** @@ -358,9 +363,9 @@ int ustctl_create_trace(const char *trace, pid_t pid) * @param pid Traced process ID * @return 0 if successful, or error USTCTL_ERR_GEN */ -int ustctl_start_trace(const char *trace, pid_t pid) +int ustctl_start_trace(int sock, const char *trace) { - return do_trace_cmd(trace, pid, START_TRACE); + return do_trace_cmd(sock, trace, START_TRACE); } /** @@ -369,9 +374,9 @@ int ustctl_start_trace(const char *trace, pid_t pid) * @param pid Traced process ID * @return 0 if successful, or error USTCTL_ERR_GEN */ -int ustctl_alloc_trace(const char *trace, pid_t pid) +int ustctl_alloc_trace(int sock, const char *trace) { - return do_trace_cmd(trace, pid, ALLOC_TRACE); + return do_trace_cmd(sock, trace, ALLOC_TRACE); } /** @@ -380,9 +385,9 @@ int ustctl_alloc_trace(const char *trace, pid_t pid) * @param pid Traced process ID * @return 0 if successful, or error USTCTL_ERR_GEN */ -int ustctl_stop_trace(const char *trace, pid_t pid) +int ustctl_stop_trace(int sock, const char *trace) { - return do_trace_cmd(trace, pid, STOP_TRACE); + return do_trace_cmd(sock, trace, STOP_TRACE); } /** @@ -437,11 +442,11 @@ int ustctl_free_cmsf(struct marker_status *cmsf) * @param pid Targeted PID * @return 0 if successful, or -1 on error */ -int ustctl_get_cmsf(struct marker_status **cmsf, const pid_t pid) +int ustctl_get_cmsf(int sock, struct marker_status **cmsf) { struct ustcomm_header req_header, res_header; char *big_str = NULL; - int result, app_fd; + int result; struct marker_status *tmp_cmsf = NULL; unsigned int i = 0, cmsf_ind = 0; @@ -449,28 +454,21 @@ int ustctl_get_cmsf(struct marker_status **cmsf, const pid_t pid) return -1; } - if (ustcomm_connect_app(pid, &app_fd)) { - ERR("could not connect to PID %u", (unsigned int) pid); - return -1; - } - req_header.command = LIST_MARKERS; req_header.size = 0; - result = ustcomm_send(app_fd, &req_header, NULL); + result = ustcomm_send(sock, &req_header, NULL); if (result <= 0) { - PERROR("error while requesting markers list for process %d", pid); + PERROR("error while requesting markers list"); return -1; } - result = ustcomm_recv_alloc(app_fd, &res_header, &big_str); + result = ustcomm_recv_alloc(sock, &res_header, &big_str); if (result <= 0) { ERR("error while receiving markers list"); return -1; } - close(app_fd); - tmp_cmsf = (struct marker_status *) zmalloc(sizeof(struct marker_status) * (ustctl_count_nl(big_str) + 1)); if (tmp_cmsf == NULL) { @@ -506,7 +504,6 @@ int ustctl_get_cmsf(struct marker_status **cmsf, const pid_t pid) return 0; } - /** * Frees a TES array. * @@ -537,12 +534,11 @@ int ustctl_free_tes(struct trace_event_status *tes) * @param pid Targeted PID * @return 0 if successful, or -1 on error */ -int ustctl_get_tes(struct trace_event_status **tes, - const pid_t pid) +int ustctl_get_tes(int sock, struct trace_event_status **tes) { struct ustcomm_header req_header, res_header; char *big_str = NULL; - int result, app_fd; + int result; struct trace_event_status *tmp_tes = NULL; unsigned int i = 0, tes_ind = 0; @@ -550,28 +546,21 @@ int ustctl_get_tes(struct trace_event_status **tes, return -1; } - if (ustcomm_connect_app(pid, &app_fd)) { - ERR("could not connect to PID %u", (unsigned int) pid); - return -1; - } - req_header.command = LIST_TRACE_EVENTS; req_header.size = 0; - result = ustcomm_send(app_fd, &req_header, NULL); + result = ustcomm_send(sock, &req_header, NULL); if (result != 1) { ERR("error while requesting trace_event list"); return -1; } - result = ustcomm_recv_alloc(app_fd, &res_header, &big_str); + result = ustcomm_recv_alloc(sock, &res_header, &big_str); if (result != 1) { ERR("error while receiving markers list"); return -1; } - close(app_fd); - tmp_tes = (struct trace_event_status *) zmalloc(sizeof(struct trace_event_status) * (ustctl_count_nl(big_str) + 1)); @@ -599,13 +588,13 @@ int ustctl_get_tes(struct trace_event_status **tes, } /** - * Set socket path + * Set sock path * - * @param sock_path Socket path + * @param sock_path Sock path * @param pid Traced process ID * @return 0 if successful, or error */ -int ustctl_set_sock_path(const char *sock_path, pid_t pid) +int ustctl_set_sock_path(int sock, const char *sock_path) { int result; struct ustcomm_header req_header, res_header; @@ -621,18 +610,18 @@ int ustctl_set_sock_path(const char *sock_path, pid_t pid) req_header.command = SET_SOCK_PATH; - return do_cmd(pid, &req_header, (char *)&sock_path_msg, + return do_cmd(sock, &req_header, (char *)&sock_path_msg, &res_header, NULL); } /** - * Get socket path + * Get sock path * - * @param sock_path Pointer to where the socket path will be returned + * @param sock_path Pointer to where the sock path will be returned * @param pid Traced process ID * @return 0 if successful, or error */ -int ustctl_get_sock_path(char **sock_path, pid_t pid) +int ustctl_get_sock_path(int sock, char **sock_path) { int result; struct ustcomm_header req_header, res_header; @@ -641,7 +630,7 @@ int ustctl_get_sock_path(char **sock_path, pid_t pid) req_header.command = GET_SOCK_PATH; req_header.size = 0; - result = do_cmd(pid, &req_header, NULL, &res_header, + result = do_cmd(sock, &req_header, NULL, &res_header, (char **)&sock_path_msg); if (result < 0) { return -1; @@ -659,13 +648,12 @@ int ustctl_get_sock_path(char **sock_path, pid_t pid) return 0; } -int ustctl_force_switch(pid_t pid) +int ustctl_force_switch(int sock, const char *trace) { struct ustcomm_header req_header, res_header; req_header.command = FORCE_SUBBUF_SWITCH; req_header.size = 0; - return do_cmd(pid, &req_header, NULL, &res_header, NULL); + return do_cmd(sock, &req_header, NULL, &res_header, NULL); } - diff --git a/tests/libustctl_function_tests/libustctl_function_tests.c b/tests/libustctl_function_tests/libustctl_function_tests.c index 1461997..7406243 100644 --- a/tests/libustctl_function_tests/libustctl_function_tests.c +++ b/tests/libustctl_function_tests/libustctl_function_tests.c @@ -30,7 +30,7 @@ static void ustctl_function_tests(pid_t pid) { - int result; + int result, sock; unsigned int subbuf_size, subbuf_num; unsigned int new_subbuf_size, new_subbuf_num; struct marker_status *marker_status, *ms_ptr; @@ -40,8 +40,11 @@ static void ustctl_function_tests(pid_t pid) printf("Connecting to pid %d\n", pid); + sock = ustctl_connect_pid(pid); + tap_ok(sock > 0, "ustctl_connect_pid"); + /* marker status array functions */ - result = ustctl_get_cmsf(&marker_status, pid); + result = ustctl_get_cmsf(sock, &marker_status); tap_ok(!result, "ustctl_get_cmsf"); result = 0; @@ -56,15 +59,15 @@ static void ustctl_function_tests(pid_t pid) tap_ok(!ustctl_free_cmsf(marker_status), "ustctl_free_cmsf"); /* Get and set the socket path */ - tap_ok(!ustctl_get_sock_path(&old_socket_path, pid), + tap_ok(!ustctl_get_sock_path(sock, &old_socket_path), "ustctl_get_sock_path"); printf("Socket path: %s\n", old_socket_path); - tap_ok(!ustctl_set_sock_path(tmp_ustd_socket, pid), + tap_ok(!ustctl_set_sock_path(sock, tmp_ustd_socket), "ustctl_set_sock_path - set a new path"); - tap_ok(!ustctl_get_sock_path(&new_socket_path, pid), + tap_ok(!ustctl_get_sock_path(sock, &new_socket_path), "ustctl_get_sock_path - get the new path"); tap_ok(!strcmp(new_socket_path, tmp_ustd_socket), @@ -72,90 +75,91 @@ static void ustctl_function_tests(pid_t pid) free(new_socket_path); - tap_ok(!ustctl_set_sock_path(old_socket_path, pid), + tap_ok(!ustctl_set_sock_path(sock, old_socket_path), "Reset the socket path"); free(old_socket_path); /* Enable, disable markers */ - tap_ok(!ustctl_set_marker_state(trace, "ust", "bar", 1, pid), + tap_ok(!ustctl_set_marker_state(sock, trace, "ust", "bar", 1), "ustctl_set_marker_state - existing marker ust bar"); /* Create and allocate a trace */ - tap_ok(!ustctl_create_trace(trace, pid), "ustctl_create_trace"); + tap_ok(!ustctl_create_trace(sock, trace), "ustctl_create_trace"); - tap_ok(!ustctl_alloc_trace(trace, pid), "ustctl_alloc_trace"); + tap_ok(!ustctl_alloc_trace(sock, trace), "ustctl_alloc_trace"); /* Get subbuf size and number */ - subbuf_num = ustctl_get_subbuf_num(trace, "ust", pid); + subbuf_num = ustctl_get_subbuf_num(sock, trace, "ust"); tap_ok(subbuf_num > 0, "ustctl_get_subbuf_num - %d sub-buffers", subbuf_num); - subbuf_size = ustctl_get_subbuf_size(trace, "ust", pid); + subbuf_size = ustctl_get_subbuf_size(sock, trace, "ust"); tap_ok(subbuf_size, "ustctl_get_subbuf_size - sub-buffer size is %d", subbuf_size); /* Start the trace */ - tap_ok(!ustctl_start_trace(trace, pid), "ustctl_start_trace"); + tap_ok(!ustctl_start_trace(sock, trace), "ustctl_start_trace"); /* Stop the trace and destroy it*/ - tap_ok(!ustctl_stop_trace(trace, pid), "ustctl_stop_trace"); + tap_ok(!ustctl_stop_trace(sock, trace), "ustctl_stop_trace"); - tap_ok(!ustctl_destroy_trace(trace, pid), "ustctl_destroy_trace"); + tap_ok(!ustctl_destroy_trace(sock, trace), "ustctl_destroy_trace"); /* Create a new trace */ - tap_ok(!ustctl_create_trace(trace, pid), "ustctl_create_trace - create a new trace"); + tap_ok(!ustctl_create_trace(sock, trace), "ustctl_create_trace - create a new trace"); printf("Setting new subbufer number and sizes (doubling)\n"); new_subbuf_num = 2 * subbuf_num; new_subbuf_size = 2 * subbuf_size; - tap_ok(!ustctl_set_subbuf_num(trace, "ust", new_subbuf_num, pid), + tap_ok(!ustctl_set_subbuf_num(sock, trace, "ust", new_subbuf_num), "ustctl_set_subbuf_num"); - tap_ok(!ustctl_set_subbuf_size(trace, "ust", new_subbuf_size, pid), + tap_ok(!ustctl_set_subbuf_size(sock, trace, "ust", new_subbuf_size), "ustctl_set_subbuf_size"); /* Allocate the new trace */ - tap_ok(!ustctl_alloc_trace(trace, pid), "ustctl_alloc_trace - allocate the new trace"); + tap_ok(!ustctl_alloc_trace(sock, trace), "ustctl_alloc_trace - allocate the new trace"); /* Get subbuf size and number and compare with what was set */ - subbuf_num = ustctl_get_subbuf_num(trace, "ust", pid); + subbuf_num = ustctl_get_subbuf_num(sock, trace, "ust"); - subbuf_size = ustctl_get_subbuf_size(trace, "ust", pid); + subbuf_size = ustctl_get_subbuf_size(sock, trace, "ust"); tap_ok(subbuf_num == new_subbuf_num, "Set a new subbuf number, %d == %d", subbuf_num, new_subbuf_num); - result = ustctl_get_subbuf_size(trace, "ust", pid); + result = ustctl_get_subbuf_size(sock, trace, "ust"); tap_ok(subbuf_size == new_subbuf_size, "Set a new subbuf size, %d == %d", subbuf_size, new_subbuf_size); - tap_ok(!ustctl_destroy_trace(trace, pid), "ustctl_destroy_trace - without ever starting"); + tap_ok(!ustctl_destroy_trace(sock, trace), "ustctl_destroy_trace - without ever starting"); /* * Activate a non-existent marker, this should be possible as the marker * can be loaded at a later time. */ - tap_ok(ustctl_set_marker_state(trace, "ustl", "blar", 1, pid) == 0, + tap_ok(ustctl_set_marker_state(sock, trace, "ustl", "blar", 1) == 0, "Enable non-existent marker ustl blar"); printf("##### Tests that definetly should work are completed #####\n"); printf("############## Start expected failure cases ##############\n"); - tap_ok(ustctl_set_marker_state(trace, "ust","bar", 1, pid), + tap_ok(ustctl_set_marker_state(sock, trace, "ust","bar", 1), "Enable already enabled marker ust/bar"); + tap_ok(EEXIST == errno, "Right error code for enabling an already enabled marker"); - tap_ok(ustctl_start_trace(trace, pid), + tap_ok(ustctl_start_trace(sock, trace), "Start a non-existent trace"); - tap_ok(ustctl_destroy_trace(trace, pid), + tap_ok(ustctl_destroy_trace(sock, trace), "Destroy non-existent trace"); exit(tap_status() ? EXIT_FAILURE : EXIT_SUCCESS); @@ -167,7 +171,7 @@ int main(int argc, char **argv) int i, status; pid_t parent_pid, child_pid; - tap_plan(28); + tap_plan(29); printf("Function tests for ustctl\n"); diff --git a/ustctl/channel_cmds.c b/ustctl/channel_cmds.c index 2ccf460..315fd56 100644 --- a/ustctl/channel_cmds.c +++ b/ustctl/channel_cmds.c @@ -24,12 +24,11 @@ static int set_subbuf_size(int argc, char *argv[]) { - int result = 0; - pid_t pid; + int sock, result = 0; char *channel = NULL; unsigned int size; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); if (scan_ch_and_num(argv[3], &channel, &size)) { fprintf(stderr, "Failed to scan channel and size from" @@ -38,10 +37,10 @@ static int set_subbuf_size(int argc, char *argv[]) free(channel); return -1; } - if (ustctl_set_subbuf_size(argv[2], channel, size, pid)) { + if (ustctl_set_subbuf_size(sock, argv[2], channel, size)) { ERR("error while trying to set the size of subbuffers " - "for PID %u\n", - pid); + "for PID %s\n", + argv[1]); result = -1; } @@ -52,12 +51,11 @@ static int set_subbuf_size(int argc, char *argv[]) static int set_subbuf_num(int argc, char *argv[]) { - int result = 0; - pid_t pid; + int sock, result = 0; char *channel = NULL; unsigned int num; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); if (scan_ch_and_num(argv[3], &channel, &num)) { fprintf(stderr, "Failed to scan channel and number from" @@ -66,9 +64,9 @@ static int set_subbuf_num(int argc, char *argv[]) free(channel); return -1; } - if (ustctl_set_subbuf_num(argv[2], channel, num, pid)) { - ERR("error while trying to set the number of subbuffers for PID %u\n", - pid); + if (ustctl_set_subbuf_num(sock, argv[2], channel, num)) { + ERR("error while trying to set the number of subbuffers for PID %s\n", + argv[1]); result = -1; } @@ -79,14 +77,14 @@ static int set_subbuf_num(int argc, char *argv[]) static int get_subbuf_size(int argc, char *argv[]) { - pid_t pid; + int sock; unsigned int size; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if ((size = ustctl_get_subbuf_size(argv[2], argv[3], pid)) < 0) { - ERR("error while trying to get the subbuffer size from PID %u\n", - pid); + if ((size = ustctl_get_subbuf_size(sock, argv[2], argv[3])) < 0) { + ERR("error while trying to get the subbuffer size from PID %s\n", + argv[1]); return -1; } @@ -97,14 +95,14 @@ static int get_subbuf_size(int argc, char *argv[]) static int get_subbuf_num(int argc, char *argv[]) { - pid_t pid; + int sock; unsigned int num; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if ((num = ustctl_get_subbuf_num(argv[2], argv[3], pid)) < 0) { - ERR("error while trying to get the subbuffer size from PID %u\n", - pid); + if ((num = ustctl_get_subbuf_num(sock, argv[2], argv[3])) < 0) { + ERR("error while trying to get the subbuffer size from PID %s\n", + argv[1]); return -1; } diff --git a/ustctl/marker_cmds.c b/ustctl/marker_cmds.c index 6544669..da14358 100644 --- a/ustctl/marker_cmds.c +++ b/ustctl/marker_cmds.c @@ -25,19 +25,18 @@ static int list_markers(int argc, char *argv[]) { struct marker_status *cmsf = NULL; - int i; - pid_t pid; + int i, sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_get_cmsf(&cmsf, pid)) { - ERR("error while trying to list markers for PID %u\n", pid); + if (ustctl_get_cmsf(sock, &cmsf)) { + ERR("error while trying to list markers for PID %s\n", argv[1]); return -1; } for (i = 0; cmsf[i].channel; i++) { - printf("{PID: %u, channel/marker: %s/%s, " + printf("{PID: %s, channel/marker: %s/%s, " "state: %u, fmt: %s}\n", - (unsigned int) pid, + argv[1], cmsf[i].channel, cmsf[i].marker, cmsf[i].state, @@ -49,11 +48,10 @@ static int list_markers(int argc, char *argv[]) static int enable_marker(int argc, char *argv[]) { - int i, result = 0; - pid_t pid; + int i, sock, result = 0; char *channel, *marker; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); for (i = 3; i < argc; i++) { channel = NULL; @@ -68,9 +66,9 @@ static int enable_marker(int argc, char *argv[]) if (marker) free(marker); } - if (ustctl_set_marker_state(argv[2], channel, marker, 1, pid)) { - PERROR("error while trying to enable marker %s with PID %u", - argv[i], pid); + if (ustctl_set_marker_state(sock, argv[2], channel, marker, 1)) { + PERROR("error while trying to enable marker %s with PID %s", + argv[i], argv[1]); result = -1; } free(channel); @@ -82,11 +80,10 @@ static int enable_marker(int argc, char *argv[]) static int disable_marker(int argc, char *argv[]) { - int i, result = 0; - pid_t pid; + int i, sock, result = 0; char *channel, *marker; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); for (i = 3; i < argc; i++) { channel = NULL; @@ -101,9 +98,9 @@ static int disable_marker(int argc, char *argv[]) free(marker); return -1; } - if (ustctl_set_marker_state(argv[2], channel, marker, 0, pid)) { - PERROR("error while trying to disable marker %s with PID %u", - argv[i], pid); + if (ustctl_set_marker_state(sock, argv[2], channel, marker, 0)) { + PERROR("error while trying to disable marker %s with PID %s", + argv[i], argv[1]); result = -1; } free(channel); diff --git a/ustctl/scanning_functions.c b/ustctl/scanning_functions.c index a3eceb5..4aa8c1c 100644 --- a/ustctl/scanning_functions.c +++ b/ustctl/scanning_functions.c @@ -21,9 +21,10 @@ #include "usterr.h" -pid_t parse_pid(const char *pid_string) +int parse_and_connect_pid(const char *pid_string) { pid_t pid; + int sock; errno = 0; pid = strtoull(pid_string, NULL, 10); @@ -32,7 +33,13 @@ pid_t parse_pid(const char *pid_string) exit(EXIT_FAILURE); } - return pid; + sock = ustctl_connect_pid(pid); + if (sock < 0) { + perror("Failed to connect to process"); + exit(EXIT_FAILURE); + } + + return sock; } int scan_ch_marker(const char *channel_marker, char **channel, char **marker) diff --git a/ustctl/scanning_functions.h b/ustctl/scanning_functions.h index c37eba1..0f2d62e 100644 --- a/ustctl/scanning_functions.h +++ b/ustctl/scanning_functions.h @@ -17,7 +17,7 @@ #ifndef __SCANNING_FUNCTIONS_H #define __SCANNING_FUNCTIONS_H -pid_t parse_pid(const char *pid_string); +int parse_and_connect_pid(const char *pid_string); int scan_ch_marker(const char *channel_marker, char **channel, char **marker); diff --git a/ustctl/trace_cmds.c b/ustctl/trace_cmds.c index 020e5b2..74e4660 100644 --- a/ustctl/trace_cmds.c +++ b/ustctl/trace_cmds.c @@ -25,12 +25,12 @@ static int create_trace(int argc, char *argv[]) { - pid_t pid; + int sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_create_trace(argv[2], pid)) { - ERR("Failed to create trace %s for PID %u\n", argv[2], pid); + if (ustctl_create_trace(sock, argv[2])) { + ERR("Failed to create trace %s for PID %u\n", argv[2], argv[1]); return -1; } @@ -39,12 +39,12 @@ static int create_trace(int argc, char *argv[]) static int alloc_trace(int argc, char *argv[]) { - pid_t pid; + int sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_alloc_trace(argv[2], pid)) { - ERR("Failed to allocate trace %s for PID %u\n", argv[2], pid); + if (ustctl_alloc_trace(sock, argv[2])) { + ERR("Failed to allocate trace %s for PID %s\n", argv[2], argv[1]); return -1; } return 0; @@ -52,12 +52,12 @@ static int alloc_trace(int argc, char *argv[]) static int start_trace(int argc, char *argv[]) { - pid_t pid; + int sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_start_trace(argv[2], pid)) { - ERR("Failed to start trace %s for PID %u\n", argv[2], pid); + if (ustctl_start_trace(sock, argv[2])) { + ERR("Failed to start trace %s for PID %s\n", argv[2], argv[1]); return -1; } return 0; @@ -65,12 +65,12 @@ static int start_trace(int argc, char *argv[]) static int stop_trace(int argc, char *argv[]) { - pid_t pid; + int sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_stop_trace(argv[2], pid)) { - ERR("Failed to stop trace %s for PID %u\n", argv[2], pid); + if (ustctl_stop_trace(sock, argv[2])) { + ERR("Failed to stop trace %s for PID %s\n", argv[2], argv[1]); return -1; } return 0; @@ -78,12 +78,12 @@ static int stop_trace(int argc, char *argv[]) static int destroy_trace(int argc, char *argv[]) { - pid_t pid; + int sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_destroy_trace(argv[2], pid)) { - ERR("Failed to destroy trace %s for PID %u\n", argv[2], pid); + if (ustctl_destroy_trace(sock, argv[2])) { + ERR("Failed to destroy trace %s for PID %s\n", argv[2], argv[1]); return -1; } return 0; @@ -91,12 +91,12 @@ static int destroy_trace(int argc, char *argv[]) static int force_subbuf_switch(int argc, char *argv[]) { - pid_t pid; + int sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_force_switch(pid)) { - ERR("error while trying to force switch for PID %u\n", pid); + if (ustctl_force_switch(sock, argv[2])) { + ERR("error while trying to force switch for PID %s\n", argv[1]); return -1; } diff --git a/ustctl/ustctl.c b/ustctl/ustctl.c index c1bbe8c..b9acc23 100644 --- a/ustctl/ustctl.c +++ b/ustctl/ustctl.c @@ -106,21 +106,20 @@ int main(int argc, char *argv[]) static int list_trace_events(int argc, char *argv[]) { struct trace_event_status *tes = NULL; - int i; - pid_t pid; + int i, sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_get_tes(&tes, pid)) { + if (ustctl_get_tes(sock, &tes)) { ERR("error while trying to list " - "trace_events for PID %u\n", - pid); + "trace_events for PID %s\n", + argv[1]); return -1; } i = 0; for (i = 0; tes[i].name; i++) { - printf("{PID: %u, trace_event: %s}\n", - pid, + printf("{PID: %s, trace_event: %s}\n", + argv[1], tes[i].name); } ustctl_free_tes(tes); @@ -130,12 +129,12 @@ static int list_trace_events(int argc, char *argv[]) static int set_sock_path(int argc, char *argv[]) { - pid_t pid; + int sock; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_set_sock_path(argv[2], pid)) { - ERR("error while trying to set sock path for PID %u\n", pid); + if (ustctl_set_sock_path(sock, argv[2])) { + ERR("error while trying to set sock path for PID %s\n", argv[1]); return -1; } @@ -144,13 +143,13 @@ static int set_sock_path(int argc, char *argv[]) static int get_sock_path(int argc, char *argv[]) { - pid_t pid; + int sock; char *sock_path; - pid = parse_pid(argv[1]); + sock = parse_and_connect_pid(argv[1]); - if (ustctl_get_sock_path(&sock_path, pid)) { - ERR("error while trying to get sock path for PID %u\n", pid); + if (ustctl_get_sock_path(sock, &sock_path)) { + ERR("error while trying to get sock path for PID %s\n", argv[1]); return -1; } printf("The socket path is %s\n", sock_path);