summaryrefslogtreecommitdiff
path: root/libweston/weston-log-flight-rec.c
blob: a577a38a8991b9f24af95859672b454a18eb5878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright © 2019 Collabora Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <libweston/weston-log.h>
#include "shared/helpers.h"
#include <libweston/libweston.h>

#include "weston-log-internal.h"

#include <assert.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>

struct weston_ring_buffer {
	uint32_t append_pos;	/**< where in the buffer we are */
	uint32_t size;		/**< max length of the ring buffer */
	char *buf;		/**< the buffer itself */
	FILE *file;		/**< where to write in case we need to dump the buf */
	bool overlap;		/**< in case buff overlaps, hint from where to print buf contents */
};

/** allows easy access to the ring buffer in case of a core dump
 */
WL_EXPORT struct weston_ring_buffer *weston_primary_flight_recorder_ring_buffer = NULL;

/** A black box type of stream, used to aggregate data continuously, and
 * when needed, to dump its contents for inspection.
 */
struct weston_debug_log_flight_recorder {
	struct weston_log_subscriber base;
	struct weston_ring_buffer rb;
};

static void
weston_ring_buffer_init(struct weston_ring_buffer *rb, size_t size, char *buf)
{
	rb->append_pos = 0;
	rb->size = size - 1;
	rb->buf = buf;
	rb->overlap = false;
	rb->file = stderr;
}

static struct weston_debug_log_flight_recorder *
to_flight_recorder(struct weston_log_subscriber *sub)
{
	return container_of(sub, struct weston_debug_log_flight_recorder, base);
}

static void
weston_log_flight_recorder_adjust_end(struct weston_ring_buffer *rb,
				      size_t bytes_to_advance)
{
	if (rb->append_pos == rb->size - bytes_to_advance)
		rb->append_pos = 0;
	else
		rb->append_pos += bytes_to_advance;
}

static void
weston_log_flight_recorder_write_chunks(struct weston_ring_buffer *rb,
					const char *data, size_t len)
{
	/* no of chunks that matches our buffer size */
	size_t nr_chunks = len / rb->size;

	/* bytes left that do not fill entire buffer */
	size_t bytes_left_last_chunk = len % rb->size;
	const char *c_data = data;

	/* might overlap multiple times, memcpy is redundant,
	 * that's why we don't even modify append_pos */
	while (nr_chunks-- > 0) {
		memcpy(&rb->buf[rb->append_pos], c_data, rb->size);
		c_data += rb->size;
	}

	if (bytes_left_last_chunk)
		memcpy(&rb->buf[rb->append_pos], c_data, bytes_left_last_chunk);

	/* adjust append_pos */
	weston_log_flight_recorder_adjust_end(rb, bytes_left_last_chunk);
}

static void
weston_log_flight_recorder_write_chunks_overlap(struct weston_ring_buffer *rb,
					 const char *data, size_t len)
{
	size_t transfer_remains =
		(rb->append_pos + len) - rb->size;
	size_t transfer_to_end = len - transfer_remains;
	const char *c_data = data;

	/* transfer what remains until the end of the buffer */
	memcpy(&rb->buf[rb->append_pos], c_data, transfer_to_end);
	c_data += transfer_to_end;

	/* reset append_pos as we filled up the buffer */
	rb->append_pos = 0;

	/* transfer what remains */
	weston_log_flight_recorder_write_chunks(rb, c_data, transfer_remains);
	rb->overlap = true;
}

static void
weston_log_flight_recorder_write_data(struct weston_ring_buffer *rb,
				      const char *data, size_t len)
{
	/*
	 * If append_pos is at the beginning of the buffer, we determine if we
	 * should do it in chunks, and if there are any bytes left we transfer
	 * those as well.
	 *
	 * If the append_pos is somewhere inside the buffer we determine how
	 * many bytes we need to transfer between we reach the end and overlap,
	 * then we proceed as in the first step.
	 */
	if (rb->append_pos == 0)
		weston_log_flight_recorder_write_chunks(rb, data, len);
	else
		weston_log_flight_recorder_write_chunks_overlap(rb, data, len);
}

static void
weston_log_flight_recorder_write(struct weston_log_subscriber *sub,
				 const char *data, size_t len)
{
	struct weston_debug_log_flight_recorder *flight_rec =
		to_flight_recorder(sub);
	struct weston_ring_buffer *rb = &flight_rec->rb;

	/* in case the data is bigger than the size of the buf */
	if (rb->size < len) {
		weston_log_flight_recorder_write_data(rb, data, len);
	} else {
		/* if we can transfer it without wrapping it do it at once */
		if (rb->append_pos <= rb->size - len) {
			memcpy(&rb->buf[rb->append_pos], data, len);

			/*
			 * adjust append_pos, take care of the situation were
			 * to fill up the entire buf
			 */
			weston_log_flight_recorder_adjust_end(rb, len);
		} else {
			weston_log_flight_recorder_write_data(rb, data, len);
		}
	}

}

static void
weston_log_subscriber_display_flight_rec_data(struct weston_ring_buffer *rb,
					      FILE *file)
{
	FILE *file_d = stderr;
	if (file)
		file_d = file;

	if (!rb->overlap) {
		if (rb->append_pos)
			fwrite(rb->buf, sizeof(char), rb->append_pos, file_d);
		else
			fwrite(rb->buf, sizeof(char), rb->size, file_d);
	} else {
		/* from append_pos to size */
		fwrite(&rb->buf[rb->append_pos], sizeof(char),
		       rb->size - rb->append_pos, file_d);
		/* from 0 to append_pos */
		fwrite(rb->buf, sizeof(char), rb->append_pos, file_d);
	}
}

WL_EXPORT void
weston_log_subscriber_display_flight_rec(struct weston_log_subscriber *sub)
{
	struct weston_debug_log_flight_recorder *flight_rec =
		to_flight_recorder(sub);
	struct weston_ring_buffer *rb = &flight_rec->rb;

	weston_log_subscriber_display_flight_rec_data(rb, rb->file);
}

static void
weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber *sub)
{
	struct weston_debug_log_flight_recorder *flight_rec = to_flight_recorder(sub);

	/* Resets weston_primary_flight_recorder_ring_buffer to NULL if it
	 * is the destroyed subscriber */
	if (weston_primary_flight_recorder_ring_buffer == &flight_rec->rb)
		weston_primary_flight_recorder_ring_buffer = NULL;

	weston_log_subscriber_release(sub);
	free(flight_rec->rb.buf);
	free(flight_rec);
}

/** Create a flight recorder type of subscriber
 *
 * Allocates both the flight recorder and the underlying ring buffer. Use
 * weston_log_subscriber_destroy() to clean-up.
 *
 * @param size specify the maximum size (in bytes) of the backing storage
 * for the flight recorder
 * @returns a weston_log_subscriber object or NULL in case of failure
 */
WL_EXPORT struct weston_log_subscriber *
weston_log_subscriber_create_flight_rec(size_t size)
{
	struct weston_debug_log_flight_recorder *flight_rec;
	char *weston_rb;

	assert("Can't create more than one flight recorder." &&
			!weston_primary_flight_recorder_ring_buffer);

	flight_rec = zalloc(sizeof(*flight_rec));
	if (!flight_rec)
		return NULL;

	flight_rec->base.write = weston_log_flight_recorder_write;
	flight_rec->base.destroy = weston_log_subscriber_destroy_flight_rec;
	flight_rec->base.destroy_subscription = NULL;
	flight_rec->base.complete = NULL;
	wl_list_init(&flight_rec->base.subscription_list);

	weston_rb = zalloc(sizeof(char) * size);
	if (!weston_rb) {
		free(flight_rec);
		return NULL;
	}

	weston_ring_buffer_init(&flight_rec->rb, size, weston_rb);
	weston_primary_flight_recorder_ring_buffer = &flight_rec->rb;

	/* write some data to the rb such that the memory gets mapped */
	memset(flight_rec->rb.buf, 0xff, flight_rec->rb.size);

	return &flight_rec->base;
}

/** Retrieve flight recorder ring buffer contents, could be useful when
 * implementing an assert()-like wrapper.
 *
 * @param file a FILE type already opened. Can also pass stderr/stdout under gdb
 * if the program is loaded into memory.
 *
 * Uses the global exposed weston_primary_flight_recorder_ring_buffer.
 *
 */
WL_EXPORT void
weston_log_flight_recorder_display_buffer(FILE *file)
{
	if (!weston_primary_flight_recorder_ring_buffer)
		return;

	weston_log_subscriber_display_flight_rec_data(weston_primary_flight_recorder_ring_buffer,
						      file);
}