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"
42 /* The pipe file descriptor */
48 /* the buffer memory */
52 /* number of subbuffers in buffer */
54 /* size of each subbuffer */
57 /* the buffer information struct */
67 struct libustd_callbacks
;
70 * struct libustd_instance - Contains the data associated with a trace instance.
71 * The lib user can read but MUST NOT change any attributes but callbacks.
72 * @callbacks: Contains the necessary callbacks for a tracing session.
74 struct libustd_instance
{
75 struct libustd_callbacks
*callbacks
;
78 struct list_head connections
;
80 struct ustcomm_sock
*listen_sock
;
82 pthread_mutex_t mutex
;
87 * struct libustd_callbacks - Contains the necessary callbacks for a tracing
88 * session. The user can set the unnecessary functions to NULL if he does not
91 struct libustd_callbacks
{
93 * on_open_buffer - Is called after a buffer is attached to process memory
95 * @data: pointer to the callbacks structure that has been passed to the
97 * @buf: structure that contains the data associated with the buffer
99 * Returns 0 if the callback succeeds else not 0.
101 * It has to be thread safe, because it is called by many threads.
103 int (*on_open_buffer
)(struct libustd_callbacks
*data
,
104 struct buffer_info
*buf
);
107 * on_close_buffer - Is called after a buffer is detached from process memory
109 * @data: pointer to the callbacks structure that has been passed to the
111 * @buf: structure that contains the data associated with the buffer
113 * Returns 0 if the callback succeeds else not 0.
115 * It has to be thread safe, because it is called by many threads.
117 int (*on_close_buffer
)(struct libustd_callbacks
*data
,
118 struct buffer_info
*buf
);
121 * on_read_subbuffer - Is called after a subbuffer is a reserved.
123 * @data: pointer to the callbacks structure that has been passed to the
125 * @buf: structure that contains the data associated with the buffer
127 * Returns 0 if the callback succeeds else not 0.
129 * It has to be thread safe, because it is called by many threads.
131 int (*on_read_subbuffer
)(struct libustd_callbacks
*data
,
132 struct buffer_info
*buf
);
135 * on_read_partial_subbuffer - Is called when an incomplete subbuffer
136 * is being salvaged from an app crash
138 * @data: pointer to the callbacks structure that has been passed to the
140 * @buf: structure that contains the data associated with the buffer
141 * @subbuf_index: index of the subbuffer to read in the buffer
142 * @valid_length: number of bytes considered safe to read
144 * Returns 0 if the callback succeeds else not 0.
146 * It has to be thread safe, because it is called by many threads.
148 int (*on_read_partial_subbuffer
)(struct libustd_callbacks
*data
,
149 struct buffer_info
*buf
,
151 unsigned long valid_length
);
154 * on_put_error - Is called when a put error has occured and the last
155 * subbuffer read is no longer safe to keep
157 * @data: pointer to the callbacks structure that has been passed to the
159 * @buf: structure that contains the data associated with the buffer
161 * Returns 0 if the callback succeeds else not 0.
163 * It has to be thread safe, because it is called by many threads.
165 int (*on_put_error
)(struct libustd_callbacks
*data
,
166 struct buffer_info
*buf
);
169 * on_new_thread - Is called when a new thread is created
171 * @data: pointer to the callbacks structure that has been passed to the
174 * Returns 0 if the callback succeeds else not 0.
176 * It has to be thread safe, because it is called by many threads.
178 int (*on_new_thread
)(struct libustd_callbacks
*data
);
181 * on_close_thread - Is called just before a thread is destroyed
183 * @data: pointer to the callbacks structure that has been passed to the
186 * Returns 0 if the callback succeeds else not 0.
188 * It has to be thread safe, because it is called by many threads.
190 int (*on_close_thread
)(struct libustd_callbacks
*data
);
193 * on_trace_end - Is called at the very end of the tracing session. At
194 * this time, everything has been closed and the threads have
197 * @instance: pointer to the instance structure that has been passed to
200 * Returns 0 if the callback succeeds else not 0.
202 * After this callback is called, no other callback will be called
203 * again and the tracing instance will be deleted automatically by
204 * libustd. After this call, the user must not use the libustd instance.
206 int (*on_trace_end
)(struct libustd_instance
*instance
);
209 * The library's data.
215 * libustd_new_instance - Is called to create a new tracing session.
217 * @callbacks: Pointer to a callbacks structure that contain the user
218 * callbacks and data.
219 * @sock_path: Path to the socket used for communication with the traced app
221 * Returns the instance if the function succeeds else NULL.
223 struct libustd_instance
*
224 libustd_new_instance(
225 struct libustd_callbacks
*callbacks
, char *sock_path
);
228 * libustd_delete_instance - Is called to free a libustd_instance struct
230 * @instance: The tracing session instance that needs to be freed.
232 * This function should only be called if the instance has not been started,
233 * as it will automatically be called at the end of libustd_start_instance.
235 void libustd_delete_instance(struct libustd_instance
*instance
);
238 * libustd_init_instance - Is called to initiliaze a new tracing session
240 * @instance: The tracing session instance that needs to be started.
242 * Returns 0 if the function succeeds.
244 * This function must be called between libustd_new_instance and
245 * libustd_start_instance. It sets up the communication between the library
246 * and the tracing application.
248 int libustd_init_instance(struct libustd_instance
*instance
);
251 * libustd_start_instance - Is called to start a new tracing session.
253 * @instance: The tracing session instance that needs to be started.
255 * Returns 0 if the function succeeds.
257 * This is a blocking function. The caller will be blocked on it until the
258 * tracing session is stopped by the user using libustd_stop_instance or until
259 * the traced application terminates
261 int libustd_start_instance(struct libustd_instance
*instance
);
264 * libustd_stop_instance - Is called to stop a tracing session.
266 * @instance: The tracing session instance that needs to be stoped.
267 * @send_msg: If true, a message will be sent to the listening thread through
268 * the daemon socket to force it to return from the poll syscall
269 * and realize that it must close. This is not necessary if the
270 * instance is being stopped as part of an interrupt handler, as
271 * the interrupt itself will cause poll to return.
273 * Returns 0 if the function succeeds.
275 * This function returns immediately, it only tells libustd to stop the
276 * instance. The on_trace_end callback will be called when the tracing session
277 * will really be stopped. The instance is deleted automatically by libustd
278 * after on_trace_end is called.
280 int libustd_stop_instance(struct libustd_instance
*instance
, int send_msg
);