4 * Copyright 2005-2010 -
5 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 * Oumarou Dicko <oumarou.dicko@polymtl.ca>
8 * Michael Sills-Lavoie <michael.sills-lavoie@polymtl.ca>
9 * Alexis Halle <alexis.halle@polymtl.ca>
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.
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.
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
31 #include <ust/kcompat/kcompat.h>
32 #include <urcu/list.h>
34 #define USTD_DEFAULT_TRACE_PATH "/tmp/usttrace"
45 /* The pipe file descriptor */
51 /* the buffer memory */
55 /* number of subbuffers in buffer */
57 /* size of each subbuffer */
60 /* the buffer information struct */
70 struct libustd_callbacks
;
73 * struct libustd_instance - Contains the data associated with a trace instance.
74 * The lib user can read but MUST NOT change any attributes but callbacks.
75 * @callbacks: Contains the necessary callbacks for a tracing session.
77 struct libustd_instance
{
78 struct libustd_callbacks
*callbacks
;
81 struct list_head connections
;
83 struct ustcomm_sock
*listen_sock
;
85 pthread_mutex_t mutex
;
90 * struct libustd_callbacks - Contains the necessary callbacks for a tracing
91 * session. The user can set the unnecessary functions to NULL if he does not
94 struct libustd_callbacks
{
96 * on_open_buffer - Is called after a buffer is attached to process memory
98 * @data: pointer to the callbacks structure that has been passed to the
100 * @buf: structure that contains the data associated with the buffer
102 * Returns 0 if the callback succeeds else not 0.
104 * It has to be thread safe, because it is called by many threads.
106 int (*on_open_buffer
)(struct libustd_callbacks
*data
,
107 struct buffer_info
*buf
);
110 * on_close_buffer - Is called after a buffer is detached from process memory
112 * @data: pointer to the callbacks structure that has been passed to the
114 * @buf: structure that contains the data associated with the buffer
116 * Returns 0 if the callback succeeds else not 0.
118 * It has to be thread safe, because it is called by many threads.
120 int (*on_close_buffer
)(struct libustd_callbacks
*data
,
121 struct buffer_info
*buf
);
124 * on_read_subbuffer - Is called after a subbuffer is a reserved.
126 * @data: pointer to the callbacks structure that has been passed to the
128 * @buf: structure that contains the data associated with the buffer
130 * Returns 0 if the callback succeeds else not 0.
132 * It has to be thread safe, because it is called by many threads.
134 int (*on_read_subbuffer
)(struct libustd_callbacks
*data
,
135 struct buffer_info
*buf
);
138 * on_read_partial_subbuffer - Is called when an incomplete subbuffer
139 * is being salvaged from an app crash
141 * @data: pointer to the callbacks structure that has been passed to the
143 * @buf: structure that contains the data associated with the buffer
144 * @subbuf_index: index of the subbuffer to read in the buffer
145 * @valid_length: number of bytes considered safe to read
147 * Returns 0 if the callback succeeds else not 0.
149 * It has to be thread safe, because it is called by many threads.
151 int (*on_read_partial_subbuffer
)(struct libustd_callbacks
*data
,
152 struct buffer_info
*buf
,
154 unsigned long valid_length
);
157 * on_put_error - Is called when a put error has occured and the last
158 * subbuffer read is no longer safe to keep
160 * @data: pointer to the callbacks structure that has been passed to the
162 * @buf: structure that contains the data associated with the buffer
164 * Returns 0 if the callback succeeds else not 0.
166 * It has to be thread safe, because it is called by many threads.
168 int (*on_put_error
)(struct libustd_callbacks
*data
,
169 struct buffer_info
*buf
);
172 * on_new_thread - Is called when a new thread is created
174 * @data: pointer to the callbacks structure that has been passed to the
177 * Returns 0 if the callback succeeds else not 0.
179 * It has to be thread safe, because it is called by many threads.
181 int (*on_new_thread
)(struct libustd_callbacks
*data
);
184 * on_close_thread - Is called just before a thread is destroyed
186 * @data: pointer to the callbacks structure that has been passed to the
189 * Returns 0 if the callback succeeds else not 0.
191 * It has to be thread safe, because it is called by many threads.
193 int (*on_close_thread
)(struct libustd_callbacks
*data
);
196 * on_trace_end - Is called at the very end of the tracing session. At
197 * this time, everything has been closed and the threads have
200 * @instance: pointer to the instance structure that has been passed to
203 * Returns 0 if the callback succeeds else not 0.
205 * After this callback is called, no other callback will be called
206 * again and the tracing instance will be deleted automatically by
207 * libustd. After this call, the user must not use the libustd instance.
209 int (*on_trace_end
)(struct libustd_instance
*instance
);
212 * The library's data.
218 * libustd_new_instance - Is called to create a new tracing session.
220 * @callbacks: Pointer to a callbacks structure that contain the user
221 * callbacks and data.
222 * @sock_path: Path to the socket used for communication with the traced app
224 * Returns the instance if the function succeeds else NULL.
226 struct libustd_instance
*
227 libustd_new_instance(
228 struct libustd_callbacks
*callbacks
, char *sock_path
);
231 * libustd_delete_instance - Is called to free a libustd_instance struct
233 * @instance: The tracing session instance that needs to be freed.
235 * This function should only be called if the instance has not been started,
236 * as it will automatically be called at the end of libustd_start_instance.
238 void libustd_delete_instance(struct libustd_instance
*instance
);
241 * libustd_init_instance - Is called to initiliaze a new tracing session
243 * @instance: The tracing session instance that needs to be started.
245 * Returns 0 if the function succeeds.
247 * This function must be called between libustd_new_instance and
248 * libustd_start_instance. It sets up the communication between the library
249 * and the tracing application.
251 int libustd_init_instance(struct libustd_instance
*instance
);
254 * libustd_start_instance - Is called to start a new tracing session.
256 * @instance: The tracing session instance that needs to be started.
258 * Returns 0 if the function succeeds.
260 * This is a blocking function. The caller will be blocked on it until the
261 * tracing session is stopped by the user using libustd_stop_instance or until
262 * the traced application terminates
264 int libustd_start_instance(struct libustd_instance
*instance
);
267 * libustd_stop_instance - Is called to stop a tracing session.
269 * @instance: The tracing session instance that needs to be stoped.
270 * @send_msg: If true, a message will be sent to the listening thread through
271 * the daemon socket to force it to return from the poll syscall
272 * and realize that it must close. This is not necessary if the
273 * instance is being stopped as part of an interrupt handler, as
274 * the interrupt itself will cause poll to return.
276 * Returns 0 if the function succeeds.
278 * This function returns immediately, it only tells libustd to stop the
279 * instance. The on_trace_end callback will be called when the tracing session
280 * will really be stopped. The instance is deleted automatically by libustd
281 * after on_trace_end is called.
283 int libustd_stop_instance(struct libustd_instance
*instance
, int send_msg
);