| 1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
| 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 | #ifndef USTCOMM_H |
| 19 | #define USTCOMM_H |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/un.h> |
| 23 | #include <urcu/list.h> |
| 24 | |
| 25 | #include <ust/kcompat/kcompat.h> |
| 26 | |
| 27 | #define SOCK_DIR "/tmp/ust-app-socks" |
| 28 | #define USER_TMP_DIR "/tmp" |
| 29 | #define USER_SOCK_DIR_BASE "ust-socks-" |
| 30 | #define USER_SOCK_DIR USER_TMP_DIR "/" USER_SOCK_DIR_BASE |
| 31 | |
| 32 | struct ustcomm_sock { |
| 33 | struct cds_list_head list; |
| 34 | int fd; |
| 35 | int epoll_fd; |
| 36 | }; |
| 37 | |
| 38 | struct ustcomm_header { |
| 39 | int command; |
| 40 | long size; |
| 41 | int result; |
| 42 | int fd_included; |
| 43 | }; |
| 44 | |
| 45 | #define USTCOMM_BUFFER_SIZE ((1 << 12) - sizeof(struct ustcomm_header)) |
| 46 | |
| 47 | /* Specify a sata size that leaves margin at the end of a buffer |
| 48 | * in order to make sure that we never have more data than |
| 49 | * will fit in the buffer AND that the last chars (due to a |
| 50 | * pre-receive memset) will always be 0, terminating any string |
| 51 | */ |
| 52 | #define USTCOMM_DATA_SIZE (USTCOMM_BUFFER_SIZE - 20 * sizeof(void *)) |
| 53 | |
| 54 | enum tracectl_commands { |
| 55 | ALLOC_TRACE, |
| 56 | CONSUME_BUFFER, |
| 57 | CREATE_TRACE, |
| 58 | DESTROY_TRACE, |
| 59 | DISABLE_MARKER, |
| 60 | ENABLE_MARKER, |
| 61 | EXIT, |
| 62 | FORCE_SUBBUF_SWITCH, |
| 63 | GET_BUF_SHMID_PIPE_FD, |
| 64 | GET_PIDUNIQUE, |
| 65 | GET_SOCK_PATH, |
| 66 | GET_SUBBUFFER, |
| 67 | GET_SUBBUF_NUM_SIZE, |
| 68 | LIST_MARKERS, |
| 69 | LIST_TRACE_EVENTS, |
| 70 | LOAD_PROBE_LIB, |
| 71 | NOTIFY_BUF_MAPPED, |
| 72 | PRINT_MARKERS, |
| 73 | PRINT_TRACE_EVENTS, |
| 74 | PUT_SUBBUFFER, |
| 75 | SETUP_TRACE, |
| 76 | SET_SOCK_PATH, |
| 77 | SET_SUBBUF_NUM, |
| 78 | SET_SUBBUF_SIZE, |
| 79 | START, |
| 80 | START_TRACE, |
| 81 | STOP_TRACE, |
| 82 | }; |
| 83 | |
| 84 | struct ustcomm_single_field { |
| 85 | char *field; |
| 86 | char data[USTCOMM_DATA_SIZE]; |
| 87 | }; |
| 88 | |
| 89 | struct ustcomm_channel_info { |
| 90 | char *trace; |
| 91 | char *channel; |
| 92 | unsigned int subbuf_size; |
| 93 | unsigned int subbuf_num; |
| 94 | char data[USTCOMM_DATA_SIZE]; |
| 95 | }; |
| 96 | |
| 97 | struct ustcomm_buffer_info { |
| 98 | char *trace; |
| 99 | char *channel; |
| 100 | int ch_cpu; |
| 101 | pid_t pid; |
| 102 | int buf_shmid; |
| 103 | int buf_struct_shmid; |
| 104 | long consumed_old; |
| 105 | char data[USTCOMM_DATA_SIZE]; |
| 106 | }; |
| 107 | |
| 108 | struct ustcomm_ust_marker_info { |
| 109 | char *trace; |
| 110 | char *channel; |
| 111 | char *ust_marker; |
| 112 | char data[USTCOMM_DATA_SIZE]; |
| 113 | }; |
| 114 | |
| 115 | struct ustcomm_pidunique { |
| 116 | s64 pidunique; |
| 117 | }; |
| 118 | |
| 119 | struct ustcomm_notify_buf_mapped { |
| 120 | char data[USTCOMM_DATA_SIZE]; |
| 121 | }; |
| 122 | |
| 123 | /* Ensure directory existence, usefull for unix sockets */ |
| 124 | extern int ensure_dir_exists(const char *dir, mode_t mode); |
| 125 | |
| 126 | /* Create and delete sockets */ |
| 127 | extern struct ustcomm_sock * ustcomm_init_sock(int fd, int epoll_fd, |
| 128 | struct cds_list_head *list); |
| 129 | extern void ustcomm_del_sock(struct ustcomm_sock *sock, int keep_in_epoll); |
| 130 | |
| 131 | /* Create and delete named sockets */ |
| 132 | extern struct ustcomm_sock * ustcomm_init_named_socket(const char *name, |
| 133 | int epoll_fd); |
| 134 | extern void ustcomm_del_named_sock(struct ustcomm_sock *sock, |
| 135 | int keep_socket_file); |
| 136 | |
| 137 | /* Send and receive functions for file descriptors */ |
| 138 | extern int ustcomm_send_fd(int sock, const struct ustcomm_header *header, |
| 139 | const char *data, int *fd); |
| 140 | extern int ustcomm_recv_fd(int sock, struct ustcomm_header *header, |
| 141 | char *data, int *fd); |
| 142 | |
| 143 | /* Normal send and receive functions */ |
| 144 | extern int ustcomm_send(int sock, const struct ustcomm_header *header, |
| 145 | const char *data); |
| 146 | extern int ustcomm_recv(int sock, struct ustcomm_header *header, |
| 147 | char *data); |
| 148 | |
| 149 | /* Receive and allocate data, not to be used inside libust */ |
| 150 | extern int ustcomm_recv_alloc(int sock, |
| 151 | struct ustcomm_header *header, |
| 152 | char **data); |
| 153 | |
| 154 | /* Request function, send and receive */ |
| 155 | extern int ustcomm_req(int sock, |
| 156 | const struct ustcomm_header *req_header, |
| 157 | const char *req_data, |
| 158 | struct ustcomm_header *res_header, |
| 159 | char *res_data); |
| 160 | |
| 161 | extern int ustcomm_request_consumer(pid_t pid, const char *channel); |
| 162 | |
| 163 | /* Returns the current users socket directory, must be freed */ |
| 164 | extern char *ustcomm_user_sock_dir(void); |
| 165 | |
| 166 | /* Get the st_m_time from proc*/ |
| 167 | extern time_t ustcomm_pid_st_mtime(pid_t pid); |
| 168 | |
| 169 | /* Check that a socket is live */ |
| 170 | extern int ustcomm_is_socket_live(char *sock_name, pid_t *read_pid); |
| 171 | |
| 172 | extern int ustcomm_connect_app(pid_t pid, int *app_fd); |
| 173 | extern int ustcomm_connect_path(const char *path, int *connection_fd); |
| 174 | |
| 175 | extern int nth_token_is(const char *str, const char *token, int tok_no); |
| 176 | |
| 177 | extern char *nth_token(const char *str, int tok_no); |
| 178 | |
| 179 | /* String serialising functions, printf straight into a buffer */ |
| 180 | #define USTCOMM_POISON_PTR (void *)0x19831018 |
| 181 | |
| 182 | extern char * ustcomm_print_data(char *data_field, int field_size, |
| 183 | int *offset, const char *format, ...); |
| 184 | extern char * ustcomm_restore_ptr(char *ptr, char *data_field, |
| 185 | int data_field_size); |
| 186 | |
| 187 | #define COMPUTE_MSG_SIZE(struct_ptr, offset) \ |
| 188 | (size_t) (long)(struct_ptr)->data - (long)(struct_ptr) + (offset) |
| 189 | |
| 190 | /* Packing and unpacking functions, making life easier */ |
| 191 | extern int ustcomm_pack_single_field(struct ustcomm_header *header, |
| 192 | struct ustcomm_single_field *sf, |
| 193 | const char *trace); |
| 194 | |
| 195 | extern int ustcomm_unpack_single_field(struct ustcomm_single_field *sf); |
| 196 | |
| 197 | extern int ustcomm_pack_channel_info(struct ustcomm_header *header, |
| 198 | struct ustcomm_channel_info *ch_inf, |
| 199 | const char *trace, |
| 200 | const char *channel); |
| 201 | |
| 202 | extern int ustcomm_unpack_channel_info(struct ustcomm_channel_info *ch_inf); |
| 203 | |
| 204 | extern int ustcomm_pack_buffer_info(struct ustcomm_header *header, |
| 205 | struct ustcomm_buffer_info *buf_inf, |
| 206 | const char *trace, |
| 207 | const char *channel, |
| 208 | int channel_cpu); |
| 209 | |
| 210 | extern int ustcomm_unpack_buffer_info(struct ustcomm_buffer_info *buf_inf); |
| 211 | |
| 212 | extern int ustcomm_pack_ust_marker_info(struct ustcomm_header *header, |
| 213 | struct ustcomm_ust_marker_info *ust_marker_inf, |
| 214 | const char *trace, |
| 215 | const char *channel, |
| 216 | const char *ust_marker); |
| 217 | |
| 218 | extern int ustcomm_unpack_ust_marker_info(struct ustcomm_ust_marker_info *ust_marker_inf); |
| 219 | |
| 220 | #endif /* USTCOMM_H */ |