Commit | Line | Data |
---|---|---|
d159ac37 AH |
1 | /* |
2 | * libustd header file | |
3 | * | |
4 | * Copyright 2005-2010 - | |
5 | * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
6 | * Copyright 2010- | |
7 | * Oumarou Dicko <oumarou.dicko@polymtl.ca> | |
8 | * Michael Sills-Lavoie <michael.sills-lavoie@polymtl.ca> | |
9 | * Alexis Halle <alexis.halle@polymtl.ca> | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | */ | |
25 | ||
d6c9f207 AH |
26 | #ifndef USTD_H |
27 | #define USTD_H | |
d159ac37 AH |
28 | |
29 | #include <pthread.h> | |
30 | #include <dirent.h> | |
f3f8cc91 | 31 | #include <ust/kcompat/kcompat.h> |
4723ca09 | 32 | #include <urcu/list.h> |
d159ac37 AH |
33 | |
34 | #define USTD_DEFAULT_TRACE_PATH "/tmp/usttrace" | |
35 | ||
4723ca09 | 36 | struct ustcomm_sock; |
f3f8cc91 | 37 | |
d159ac37 | 38 | struct buffer_info { |
72098143 | 39 | char *name; |
d89b8191 | 40 | char *trace; |
72098143 NC |
41 | char *channel; |
42 | int channel_cpu; | |
43 | ||
d159ac37 | 44 | pid_t pid; |
4723ca09 NC |
45 | int app_sock; |
46 | /* The pipe file descriptor */ | |
47 | int pipe_fd; | |
d159ac37 AH |
48 | |
49 | int shmid; | |
50 | int bufstruct_shmid; | |
51 | ||
52 | /* the buffer memory */ | |
53 | void *mem; | |
54 | /* buffer size */ | |
55 | int memlen; | |
56 | /* number of subbuffers in buffer */ | |
57 | int n_subbufs; | |
58 | /* size of each subbuffer */ | |
59 | int subbuf_size; | |
60 | ||
61 | /* the buffer information struct */ | |
62 | void *bufstruct_mem; | |
63 | ||
64 | long consumed_old; | |
65 | ||
66 | s64 pidunique; | |
67 | ||
68 | void *user_data; | |
69 | }; | |
70 | ||
71 | struct libustd_callbacks; | |
72 | ||
73 | /** | |
74 | * struct libustd_instance - Contains the data associated with a trace instance. | |
75 | * The lib user can read but MUST NOT change any attributes but callbacks. | |
76 | * @callbacks: Contains the necessary callbacks for a tracing session. | |
77 | */ | |
78 | struct libustd_instance { | |
79 | struct libustd_callbacks *callbacks; | |
80 | int quit_program; | |
81 | int is_init; | |
0222e121 | 82 | struct cds_list_head connections; |
4723ca09 NC |
83 | int epoll_fd; |
84 | struct ustcomm_sock *listen_sock; | |
d159ac37 AH |
85 | char *sock_path; |
86 | pthread_mutex_t mutex; | |
87 | int active_buffers; | |
88 | }; | |
89 | ||
90 | /** | |
91 | * struct libustd_callbacks - Contains the necessary callbacks for a tracing | |
92 | * session. The user can set the unnecessary functions to NULL if he does not | |
93 | * need them. | |
94 | */ | |
95 | struct libustd_callbacks { | |
96 | /** | |
97 | * on_open_buffer - Is called after a buffer is attached to process memory | |
98 | * | |
99 | * @data: pointer to the callbacks structure that has been passed to the | |
100 | * library. | |
101 | * @buf: structure that contains the data associated with the buffer | |
102 | * | |
103 | * Returns 0 if the callback succeeds else not 0. | |
104 | * | |
105 | * It has to be thread safe, because it is called by many threads. | |
106 | */ | |
107 | int (*on_open_buffer)(struct libustd_callbacks *data, | |
108 | struct buffer_info *buf); | |
109 | ||
110 | /** | |
111 | * on_close_buffer - Is called after a buffer is detached from process memory | |
112 | * | |
113 | * @data: pointer to the callbacks structure that has been passed to the | |
114 | * library. | |
115 | * @buf: structure that contains the data associated with the buffer | |
116 | * | |
117 | * Returns 0 if the callback succeeds else not 0. | |
118 | * | |
119 | * It has to be thread safe, because it is called by many threads. | |
120 | */ | |
121 | int (*on_close_buffer)(struct libustd_callbacks *data, | |
122 | struct buffer_info *buf); | |
123 | ||
124 | /** | |
125 | * on_read_subbuffer - Is called after a subbuffer is a reserved. | |
126 | * | |
127 | * @data: pointer to the callbacks structure that has been passed to the | |
128 | * library. | |
129 | * @buf: structure that contains the data associated with the buffer | |
130 | * | |
131 | * Returns 0 if the callback succeeds else not 0. | |
132 | * | |
133 | * It has to be thread safe, because it is called by many threads. | |
134 | */ | |
135 | int (*on_read_subbuffer)(struct libustd_callbacks *data, | |
136 | struct buffer_info *buf); | |
137 | ||
138 | /** | |
139 | * on_read_partial_subbuffer - Is called when an incomplete subbuffer | |
140 | * is being salvaged from an app crash | |
141 | * | |
142 | * @data: pointer to the callbacks structure that has been passed to the | |
143 | * library. | |
144 | * @buf: structure that contains the data associated with the buffer | |
145 | * @subbuf_index: index of the subbuffer to read in the buffer | |
146 | * @valid_length: number of bytes considered safe to read | |
147 | * | |
148 | * Returns 0 if the callback succeeds else not 0. | |
149 | * | |
150 | * It has to be thread safe, because it is called by many threads. | |
151 | */ | |
152 | int (*on_read_partial_subbuffer)(struct libustd_callbacks *data, | |
153 | struct buffer_info *buf, | |
154 | long subbuf_index, | |
155 | unsigned long valid_length); | |
156 | ||
157 | /** | |
158 | * on_put_error - Is called when a put error has occured and the last | |
159 | * subbuffer read is no longer safe to keep | |
160 | * | |
161 | * @data: pointer to the callbacks structure that has been passed to the | |
162 | * library. | |
163 | * @buf: structure that contains the data associated with the buffer | |
164 | * | |
165 | * Returns 0 if the callback succeeds else not 0. | |
166 | * | |
167 | * It has to be thread safe, because it is called by many threads. | |
168 | */ | |
169 | int (*on_put_error)(struct libustd_callbacks *data, | |
170 | struct buffer_info *buf); | |
171 | ||
172 | /** | |
173 | * on_new_thread - Is called when a new thread is created | |
174 | * | |
175 | * @data: pointer to the callbacks structure that has been passed to the | |
176 | * library. | |
177 | * | |
178 | * Returns 0 if the callback succeeds else not 0. | |
179 | * | |
180 | * It has to be thread safe, because it is called by many threads. | |
181 | */ | |
182 | int (*on_new_thread)(struct libustd_callbacks *data); | |
183 | ||
184 | /** | |
185 | * on_close_thread - Is called just before a thread is destroyed | |
186 | * | |
187 | * @data: pointer to the callbacks structure that has been passed to the | |
188 | * library. | |
189 | * | |
190 | * Returns 0 if the callback succeeds else not 0. | |
191 | * | |
192 | * It has to be thread safe, because it is called by many threads. | |
193 | */ | |
194 | int (*on_close_thread)(struct libustd_callbacks *data); | |
195 | ||
196 | /** | |
197 | * on_trace_end - Is called at the very end of the tracing session. At | |
198 | * this time, everything has been closed and the threads have | |
199 | * been destroyed. | |
200 | * | |
201 | * @instance: pointer to the instance structure that has been passed to | |
202 | * the library. | |
203 | * | |
204 | * Returns 0 if the callback succeeds else not 0. | |
205 | * | |
206 | * After this callback is called, no other callback will be called | |
207 | * again and the tracing instance will be deleted automatically by | |
208 | * libustd. After this call, the user must not use the libustd instance. | |
209 | */ | |
210 | int (*on_trace_end)(struct libustd_instance *instance); | |
211 | ||
212 | /** | |
213 | * The library's data. | |
214 | */ | |
215 | void *user_data; | |
216 | }; | |
217 | ||
218 | /** | |
219 | * libustd_new_instance - Is called to create a new tracing session. | |
220 | * | |
221 | * @callbacks: Pointer to a callbacks structure that contain the user | |
222 | * callbacks and data. | |
223 | * @sock_path: Path to the socket used for communication with the traced app | |
224 | * | |
225 | * Returns the instance if the function succeeds else NULL. | |
226 | */ | |
227 | struct libustd_instance * | |
228 | libustd_new_instance( | |
229 | struct libustd_callbacks *callbacks, char *sock_path); | |
230 | ||
231 | /** | |
232 | * libustd_delete_instance - Is called to free a libustd_instance struct | |
233 | * | |
234 | * @instance: The tracing session instance that needs to be freed. | |
235 | * | |
236 | * This function should only be called if the instance has not been started, | |
237 | * as it will automatically be called at the end of libustd_start_instance. | |
238 | */ | |
239 | void libustd_delete_instance(struct libustd_instance *instance); | |
240 | ||
241 | /** | |
242 | * libustd_init_instance - Is called to initiliaze a new tracing session | |
243 | * | |
244 | * @instance: The tracing session instance that needs to be started. | |
245 | * | |
246 | * Returns 0 if the function succeeds. | |
247 | * | |
248 | * This function must be called between libustd_new_instance and | |
249 | * libustd_start_instance. It sets up the communication between the library | |
250 | * and the tracing application. | |
251 | */ | |
252 | int libustd_init_instance(struct libustd_instance *instance); | |
253 | ||
254 | /** | |
255 | * libustd_start_instance - Is called to start a new tracing session. | |
256 | * | |
257 | * @instance: The tracing session instance that needs to be started. | |
258 | * | |
259 | * Returns 0 if the function succeeds. | |
260 | * | |
261 | * This is a blocking function. The caller will be blocked on it until the | |
262 | * tracing session is stopped by the user using libustd_stop_instance or until | |
263 | * the traced application terminates | |
264 | */ | |
265 | int libustd_start_instance(struct libustd_instance *instance); | |
266 | ||
267 | /** | |
268 | * libustd_stop_instance - Is called to stop a tracing session. | |
269 | * | |
270 | * @instance: The tracing session instance that needs to be stoped. | |
271 | * @send_msg: If true, a message will be sent to the listening thread through | |
272 | * the daemon socket to force it to return from the poll syscall | |
273 | * and realize that it must close. This is not necessary if the | |
274 | * instance is being stopped as part of an interrupt handler, as | |
275 | * the interrupt itself will cause poll to return. | |
276 | * | |
277 | * Returns 0 if the function succeeds. | |
278 | * | |
279 | * This function returns immediately, it only tells libustd to stop the | |
280 | * instance. The on_trace_end callback will be called when the tracing session | |
281 | * will really be stopped. The instance is deleted automatically by libustd | |
282 | * after on_trace_end is called. | |
283 | */ | |
284 | int libustd_stop_instance(struct libustd_instance *instance, int send_msg); | |
285 | ||
d6c9f207 | 286 | #endif /* USTD_H */ |
d159ac37 | 287 |