• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Collabora Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include "config.h"
27 
28 #include <libweston/weston-log.h>
29 #include "shared/helpers.h"
30 #include <libweston/libweston.h>
31 
32 #include "weston-log-internal.h"
33 
34 #include <assert.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/time.h>
40 
41 struct weston_ring_buffer {
42 	uint32_t append_pos;	/**< where in the buffer we are */
43 	uint32_t size;		/**< max length of the ring buffer */
44 	char *buf;		/**< the buffer itself */
45 	FILE *file;		/**< where to write in case we need to dump the buf */
46 	bool overlap;		/**< in case buff overlaps, hint from where to print buf contents */
47 };
48 
49 /** allows easy access to the ring buffer in case of a core dump
50  */
51 WL_EXPORT struct weston_ring_buffer *weston_primary_flight_recorder_ring_buffer = NULL;
52 
53 /** A black box type of stream, used to aggregate data continuously, and
54  * when needed, to dump its contents for inspection.
55  */
56 struct weston_debug_log_flight_recorder {
57 	struct weston_log_subscriber base;
58 	struct weston_ring_buffer rb;
59 };
60 
61 static void
weston_ring_buffer_init(struct weston_ring_buffer * rb,size_t size,char * buf)62 weston_ring_buffer_init(struct weston_ring_buffer *rb, size_t size, char *buf)
63 {
64 	rb->append_pos = 0;
65 	rb->size = size - 1;
66 	rb->buf = buf;
67 	rb->overlap = false;
68 	rb->file = stderr;
69 }
70 
71 static struct weston_debug_log_flight_recorder *
to_flight_recorder(struct weston_log_subscriber * sub)72 to_flight_recorder(struct weston_log_subscriber *sub)
73 {
74 	return container_of(sub, struct weston_debug_log_flight_recorder, base);
75 }
76 
77 static void
weston_log_flight_recorder_adjust_end(struct weston_ring_buffer * rb,size_t bytes_to_advance)78 weston_log_flight_recorder_adjust_end(struct weston_ring_buffer *rb,
79 				      size_t bytes_to_advance)
80 {
81 	if (rb->append_pos == rb->size - bytes_to_advance)
82 		rb->append_pos = 0;
83 	else
84 		rb->append_pos += bytes_to_advance;
85 }
86 
87 static void
weston_log_flight_recorder_write_chunks(struct weston_ring_buffer * rb,const char * data,size_t len)88 weston_log_flight_recorder_write_chunks(struct weston_ring_buffer *rb,
89 					const char *data, size_t len)
90 {
91 	/* no of chunks that matches our buffer size */
92 	size_t nr_chunks = len / rb->size;
93 
94 	/* bytes left that do not fill entire buffer */
95 	size_t bytes_left_last_chunk = len % rb->size;
96 	const char *c_data = data;
97 
98 	/* might overlap multiple times, memcpy is redundant,
99 	 * that's why we don't even modify append_pos */
100 	while (nr_chunks-- > 0) {
101 		memcpy(&rb->buf[rb->append_pos], c_data, rb->size);
102 		c_data += rb->size;
103 	}
104 
105 	if (bytes_left_last_chunk)
106 		memcpy(&rb->buf[rb->append_pos], c_data, bytes_left_last_chunk);
107 
108 	/* adjust append_pos */
109 	weston_log_flight_recorder_adjust_end(rb, bytes_left_last_chunk);
110 }
111 
112 static void
weston_log_flight_recorder_write_chunks_overlap(struct weston_ring_buffer * rb,const char * data,size_t len)113 weston_log_flight_recorder_write_chunks_overlap(struct weston_ring_buffer *rb,
114 					 const char *data, size_t len)
115 {
116 	size_t transfer_remains =
117 		(rb->append_pos + len) - rb->size;
118 	size_t transfer_to_end = len - transfer_remains;
119 	const char *c_data = data;
120 
121 	/* transfer what remains until the end of the buffer */
122 	memcpy(&rb->buf[rb->append_pos], c_data, transfer_to_end);
123 	c_data += transfer_to_end;
124 
125 	/* reset append_pos as we filled up the buffer */
126 	rb->append_pos = 0;
127 
128 	/* transfer what remains */
129 	weston_log_flight_recorder_write_chunks(rb, c_data, transfer_remains);
130 	rb->overlap = true;
131 }
132 
133 static void
weston_log_flight_recorder_write_data(struct weston_ring_buffer * rb,const char * data,size_t len)134 weston_log_flight_recorder_write_data(struct weston_ring_buffer *rb,
135 				      const char *data, size_t len)
136 {
137 	/*
138 	 * If append_pos is at the beginning of the buffer, we determine if we
139 	 * should do it in chunks, and if there are any bytes left we transfer
140 	 * those as well.
141 	 *
142 	 * If the append_pos is somewhere inside the buffer we determine how
143 	 * many bytes we need to transfer between we reach the end and overlap,
144 	 * then we proceed as in the first step.
145 	 */
146 	if (rb->append_pos == 0)
147 		weston_log_flight_recorder_write_chunks(rb, data, len);
148 	else
149 		weston_log_flight_recorder_write_chunks_overlap(rb, data, len);
150 }
151 
152 static void
weston_log_flight_recorder_write(struct weston_log_subscriber * sub,const char * data,size_t len)153 weston_log_flight_recorder_write(struct weston_log_subscriber *sub,
154 				 const char *data, size_t len)
155 {
156 	struct weston_debug_log_flight_recorder *flight_rec =
157 		to_flight_recorder(sub);
158 	struct weston_ring_buffer *rb = &flight_rec->rb;
159 
160 	/* in case the data is bigger than the size of the buf */
161 	if (rb->size < len) {
162 		weston_log_flight_recorder_write_data(rb, data, len);
163 	} else {
164 		/* if we can transfer it without wrapping it do it at once */
165 		if (rb->append_pos <= rb->size - len) {
166 			memcpy(&rb->buf[rb->append_pos], data, len);
167 
168 			/*
169 			 * adjust append_pos, take care of the situation were
170 			 * to fill up the entire buf
171 			 */
172 			weston_log_flight_recorder_adjust_end(rb, len);
173 		} else {
174 			weston_log_flight_recorder_write_data(rb, data, len);
175 		}
176 	}
177 
178 }
179 
180 static void
weston_log_flight_recorder_map_memory(struct weston_debug_log_flight_recorder * flight_rec)181 weston_log_flight_recorder_map_memory(struct weston_debug_log_flight_recorder *flight_rec)
182 {
183 	size_t i = 0;
184 
185 	for (i = 0; i < flight_rec->rb.size; i++)
186 		flight_rec->rb.buf[i] = 0xff;
187 }
188 
189 static void
weston_log_subscriber_display_flight_rec_data(struct weston_ring_buffer * rb,FILE * file)190 weston_log_subscriber_display_flight_rec_data(struct weston_ring_buffer *rb,
191 					      FILE *file)
192 {
193 	FILE *file_d = stderr;
194 	if (file)
195 		file_d = file;
196 
197 	if (!rb->overlap) {
198 		if (rb->append_pos)
199 			fwrite(rb->buf, sizeof(char), rb->append_pos, file_d);
200 		else
201 			fwrite(rb->buf, sizeof(char), rb->size, file_d);
202 	} else {
203 		/* from append_pos to size */
204 		fwrite(&rb->buf[rb->append_pos], sizeof(char),
205 		       rb->size - rb->append_pos, file_d);
206 		/* from 0 to append_pos */
207 		fwrite(rb->buf, sizeof(char), rb->append_pos, file_d);
208 	}
209 }
210 
211 WL_EXPORT void
weston_log_subscriber_display_flight_rec(struct weston_log_subscriber * sub)212 weston_log_subscriber_display_flight_rec(struct weston_log_subscriber *sub)
213 {
214 	struct weston_debug_log_flight_recorder *flight_rec =
215 		to_flight_recorder(sub);
216 	struct weston_ring_buffer *rb = &flight_rec->rb;
217 
218 	weston_log_subscriber_display_flight_rec_data(rb, rb->file);
219 }
220 
221 static void
weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber * sub)222 weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber *sub)
223 {
224 	struct weston_debug_log_flight_recorder *flight_rec = to_flight_recorder(sub);
225 
226 	/* Resets weston_primary_flight_recorder_ring_buffer to NULL if it
227 	 * is the destroyed subscriber */
228 	if (weston_primary_flight_recorder_ring_buffer == &flight_rec->rb)
229 		weston_primary_flight_recorder_ring_buffer = NULL;
230 
231 	weston_log_subscriber_release(sub);
232 	free(flight_rec->rb.buf);
233 	free(flight_rec);
234 }
235 
236 /** Create a flight recorder type of subscriber
237  *
238  * Allocates both the flight recorder and the underlying ring buffer. Use
239  * weston_log_subscriber_destroy() to clean-up.
240  *
241  * @param size specify the maximum size (in bytes) of the backing storage
242  * for the flight recorder
243  * @returns a weston_log_subscriber object or NULL in case of failure
244  */
245 WL_EXPORT struct weston_log_subscriber *
weston_log_subscriber_create_flight_rec(size_t size)246 weston_log_subscriber_create_flight_rec(size_t size)
247 {
248 	struct weston_debug_log_flight_recorder *flight_rec;
249 	char *weston_rb;
250 
251 	assert("Can't create more than one flight recorder." &&
252 			!weston_primary_flight_recorder_ring_buffer);
253 
254 	flight_rec = zalloc(sizeof(*flight_rec));
255 	if (!flight_rec)
256 		return NULL;
257 
258 	flight_rec->base.write = weston_log_flight_recorder_write;
259 	flight_rec->base.destroy = weston_log_subscriber_destroy_flight_rec;
260 	flight_rec->base.destroy_subscription = NULL;
261 	flight_rec->base.complete = NULL;
262 	wl_list_init(&flight_rec->base.subscription_list);
263 
264 	weston_rb = zalloc(sizeof(char) * size);
265 	if (!weston_rb) {
266 		free(flight_rec);
267 		return NULL;
268 	}
269 
270 	weston_ring_buffer_init(&flight_rec->rb, size, weston_rb);
271 	weston_primary_flight_recorder_ring_buffer = &flight_rec->rb;
272 
273 	/* write some data to the rb such that the memory gets mapped */
274 	weston_log_flight_recorder_map_memory(flight_rec);
275 
276 	return &flight_rec->base;
277 }
278 
279 /** Retrieve flight recorder ring buffer contents, could be useful when
280  * implementing an assert()-like wrapper.
281  *
282  * @param file a FILE type already opened. Can also pass stderr/stdout under gdb
283  * if the program is loaded into memory.
284  *
285  * Uses the global exposed weston_primary_flight_recorder_ring_buffer.
286  *
287  */
288 WL_EXPORT void
weston_log_flight_recorder_display_buffer(FILE * file)289 weston_log_flight_recorder_display_buffer(FILE *file)
290 {
291 	if (!weston_primary_flight_recorder_ring_buffer)
292 		return;
293 
294 	weston_log_subscriber_display_flight_rec_data(weston_primary_flight_recorder_ring_buffer,
295 						      file);
296 }
297