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 "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 /** A black box type of stream, used to aggregate data continuously, and
50  * when needed, to dump its contents for inspection.
51  */
52 struct weston_debug_log_flight_recorder {
53 	struct weston_log_subscriber base;
54 	struct weston_ring_buffer rb;
55 };
56 
57 static void
weston_ring_buffer_init(struct weston_ring_buffer * rb,size_t size,char * buf)58 weston_ring_buffer_init(struct weston_ring_buffer *rb, size_t size, char *buf)
59 {
60 	rb->append_pos = 0;
61 	rb->size = size - 1;
62 	rb->buf = buf;
63 	rb->overlap = false;
64 	rb->file = stderr;
65 }
66 
67 static struct weston_debug_log_flight_recorder *
to_flight_recorder(struct weston_log_subscriber * sub)68 to_flight_recorder(struct weston_log_subscriber *sub)
69 {
70 	return container_of(sub, struct weston_debug_log_flight_recorder, base);
71 }
72 
73 static void
weston_log_flight_recorder_adjust_end(struct weston_ring_buffer * rb,size_t bytes_to_advance)74 weston_log_flight_recorder_adjust_end(struct weston_ring_buffer *rb,
75 				      size_t bytes_to_advance)
76 {
77 	if (rb->append_pos == rb->size - bytes_to_advance)
78 		rb->append_pos = 0;
79 	else
80 		rb->append_pos += bytes_to_advance;
81 }
82 
83 static void
weston_log_flight_recorder_write_chunks(struct weston_ring_buffer * rb,const char * data,size_t len)84 weston_log_flight_recorder_write_chunks(struct weston_ring_buffer *rb,
85 					const char *data, size_t len)
86 {
87 	/* no of chunks that matches our buffer size */
88 	size_t nr_chunks = len / rb->size;
89 
90 	/* bytes left that do not fill entire buffer */
91 	size_t bytes_left_last_chunk = len % rb->size;
92 	const char *c_data = data;
93 
94 	/* might overlap multiple times, memcpy is redundant,
95 	 * that's why we don't even modify append_pos */
96 	while (nr_chunks-- > 0) {
97 		memcpy(&rb->buf[rb->append_pos], c_data, rb->size);
98 		c_data += rb->size;
99 	}
100 
101 	if (bytes_left_last_chunk)
102 		memcpy(&rb->buf[rb->append_pos], c_data, bytes_left_last_chunk);
103 
104 	/* adjust append_pos */
105 	weston_log_flight_recorder_adjust_end(rb, bytes_left_last_chunk);
106 }
107 
108 static void
weston_log_flight_recorder_write_chunks_overlap(struct weston_ring_buffer * rb,const char * data,size_t len)109 weston_log_flight_recorder_write_chunks_overlap(struct weston_ring_buffer *rb,
110 					 const char *data, size_t len)
111 {
112 	size_t transfer_remains =
113 		(rb->append_pos + len) - rb->size;
114 	size_t transfer_to_end = len - transfer_remains;
115 	const char *c_data = data;
116 
117 	/* transfer what remains until the end of the buffer */
118 	memcpy(&rb->buf[rb->append_pos], c_data, transfer_to_end);
119 	c_data += transfer_to_end;
120 
121 	/* reset append_pos as we filled up the buffer */
122 	rb->append_pos = 0;
123 
124 	/* transfer what remains */
125 	weston_log_flight_recorder_write_chunks(rb, c_data, transfer_remains);
126 	rb->overlap = true;
127 }
128 
129 static void
weston_log_flight_recorder_write_data(struct weston_ring_buffer * rb,const char * data,size_t len)130 weston_log_flight_recorder_write_data(struct weston_ring_buffer *rb,
131 				      const char *data, size_t len)
132 {
133 	/*
134 	 * If append_pos is at the beginning of the buffer, we determine if we
135 	 * should do it in chunks, and if there are any bytes left we transfer
136 	 * those as well.
137 	 *
138 	 * If the append_pos is somewhere inside the buffer we determine how
139 	 * many bytes we need to transfer between we reach the end and overlap,
140 	 * then we proceed as in the first step.
141 	 */
142 	if (rb->append_pos == 0)
143 		weston_log_flight_recorder_write_chunks(rb, data, len);
144 	else
145 		weston_log_flight_recorder_write_chunks_overlap(rb, data, len);
146 }
147 
148 static void
weston_log_flight_recorder_write(struct weston_log_subscriber * sub,const char * data,size_t len)149 weston_log_flight_recorder_write(struct weston_log_subscriber *sub,
150 				 const char *data, size_t len)
151 {
152 	struct weston_debug_log_flight_recorder *flight_rec =
153 		to_flight_recorder(sub);
154 	struct weston_ring_buffer *rb = &flight_rec->rb;
155 
156 	/* in case the data is bigger than the size of the buf */
157 	if (rb->size < len) {
158 		weston_log_flight_recorder_write_data(rb, data, len);
159 	} else {
160 		/* if we can transfer it without wrapping it do it at once */
161 		if (rb->append_pos <= rb->size - len) {
162 			memcpy(&rb->buf[rb->append_pos], data, len);
163 
164 			/*
165 			 * adjust append_pos, take care of the situation were
166 			 * to fill up the entire buf
167 			 */
168 			weston_log_flight_recorder_adjust_end(rb, len);
169 		} else {
170 			weston_log_flight_recorder_write_data(rb, data, len);
171 		}
172 	}
173 
174 }
175 
176 static void
weston_log_flight_recorder_map_memory(struct weston_debug_log_flight_recorder * flight_rec)177 weston_log_flight_recorder_map_memory(struct weston_debug_log_flight_recorder *flight_rec)
178 {
179 	size_t i = 0;
180 
181 	for (i = 0; i < flight_rec->rb.size; i++)
182 		flight_rec->rb.buf[i] = 0xff;
183 }
184 
185 WL_EXPORT void
weston_log_subscriber_display_flight_rec(struct weston_log_subscriber * sub)186 weston_log_subscriber_display_flight_rec(struct weston_log_subscriber *sub)
187 {
188 	struct weston_debug_log_flight_recorder *flight_rec =
189 		to_flight_recorder(sub);
190 	struct weston_ring_buffer *rb = &flight_rec->rb;
191 
192 	if (rb->append_pos <= rb->size && !rb->overlap) {
193 		if (rb->append_pos)
194 			fwrite(rb->buf, sizeof(char), rb->append_pos, rb->file);
195 		else
196 			fwrite(rb->buf, sizeof(char), rb->size, rb->file);
197 	} else {
198 		/* from append_pos to size */
199 		fwrite(&rb->buf[rb->append_pos], sizeof(char),
200 		       rb->size - rb->append_pos, rb->file);
201 		/* from 0 to append_pos */
202 		fwrite(rb->buf, sizeof(char), rb->append_pos, rb->file);
203 	}
204 }
205 
206 /** Create a flight recorder type of subscriber
207  *
208  * Allocates both the flight recorder and the underlying ring buffer. Use
209  * weston_log_subscriber_destroy_flight_rec() to clean-up.
210  *
211  * @param size specify the maximum size (in bytes) of the backing storage
212  * for the flight recorder
213  * @returns a weston_log_subscriber object or NULL in case of failure
214  */
215 WL_EXPORT struct weston_log_subscriber *
weston_log_subscriber_create_flight_rec(size_t size)216 weston_log_subscriber_create_flight_rec(size_t size)
217 {
218 	struct weston_debug_log_flight_recorder *flight_rec;
219 	char *weston_rb;
220 
221 	flight_rec = zalloc(sizeof(*flight_rec));
222 	if (!flight_rec)
223 		return NULL;
224 
225 	flight_rec->base.write = weston_log_flight_recorder_write;
226 	flight_rec->base.destroy = NULL;
227 	flight_rec->base.complete = NULL;
228 	wl_list_init(&flight_rec->base.subscription_list);
229 
230 	weston_rb = zalloc(sizeof(char) * size);
231 	if (!weston_rb) {
232 		free(flight_rec);
233 		return NULL;
234 	}
235 
236 	weston_ring_buffer_init(&flight_rec->rb, size, weston_rb);
237 
238 	/* write some data to the rb such that the memory gets mapped */
239 	weston_log_flight_recorder_map_memory(flight_rec);
240 
241 	return &flight_rec->base;
242 }
243 
244 /** Destroys the weston_log_subscriber object created with
245  * weston_log_subscriber_create_flight_rec()
246  *
247  * @param sub the weston_log_subscriber object
248  *
249  */
250 WL_EXPORT void
weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber * sub)251 weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber *sub)
252 {
253 	struct weston_debug_log_flight_recorder *flight_rec = to_flight_recorder(sub);
254 	free(flight_rec->rb.buf);
255 	free(flight_rec);
256 }
257