blob: 780a82d2f1b4b33d91397e35d5957dc251493859 [file] [log] [blame]
Chris Lew3c8356762016-08-01 15:18:55 -07001/* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#ifndef _IPC_LOGGING_H
14#define _IPC_LOGGING_H
15
16#include <linux/types.h>
17
18#define MAX_MSG_SIZE 255
19
20enum {
21 TSV_TYPE_MSG_START = 1,
22 TSV_TYPE_SKB = TSV_TYPE_MSG_START,
23 TSV_TYPE_STRING,
24 TSV_TYPE_MSG_END = TSV_TYPE_STRING,
25};
26
27struct tsv_header {
28 unsigned char type;
29 unsigned char size; /* size of data field */
30};
31
32struct encode_context {
33 struct tsv_header hdr;
34 char buff[MAX_MSG_SIZE];
35 int offset;
36};
37
38struct decode_context {
39 int output_format; /* 0 = debugfs */
40 char *buff; /* output buffer */
41 int size; /* size of output buffer */
42};
43
44#if defined(CONFIG_IPC_LOGGING)
45/*
46 * ipc_log_context_create: Create a debug log context
47 * Should not be called from atomic context
48 *
49 * @max_num_pages: Number of pages of logging space required (max. 10)
50 * @mod_name : Name of the directory entry under DEBUGFS
51 * @user_version : Version number of user-defined message formats
52 *
53 * returns context id on success, NULL on failure
54 */
55void *ipc_log_context_create(int max_num_pages, const char *modname,
56 uint16_t user_version);
57
58/*
59 * msg_encode_start: Start encoding a log message
60 *
61 * @ectxt: Temporary storage to hold the encoded message
62 * @type: Root event type defined by the module which is logging
63 */
64void msg_encode_start(struct encode_context *ectxt, uint32_t type);
65
66/*
67 * tsv_timestamp_write: Writes the current timestamp count
68 *
69 * @ectxt: Context initialized by calling msg_encode_start()
70 */
71int tsv_timestamp_write(struct encode_context *ectxt);
72
73/*
74 * tsv_qtimer_write: Writes the current QTimer timestamp count
75 *
76 * @ectxt: Context initialized by calling msg_encode_start()
77 */
78int tsv_qtimer_write(struct encode_context *ectxt);
79
80/*
81 * tsv_pointer_write: Writes a data pointer
82 *
83 * @ectxt: Context initialized by calling msg_encode_start()
84 * @pointer: Pointer value to write
85 */
86int tsv_pointer_write(struct encode_context *ectxt, void *pointer);
87
88/*
89 * tsv_int32_write: Writes a 32-bit integer value
90 *
91 * @ectxt: Context initialized by calling msg_encode_start()
92 * @n: Integer to write
93 */
94int tsv_int32_write(struct encode_context *ectxt, int32_t n);
95
96/*
97 * tsv_int32_write: Writes a 32-bit integer value
98 *
99 * @ectxt: Context initialized by calling msg_encode_start()
100 * @n: Integer to write
101 */
102int tsv_byte_array_write(struct encode_context *ectxt,
103 void *data, int data_size);
104
105/*
106 * msg_encode_end: Complete the message encode process
107 *
108 * @ectxt: Temporary storage which holds the encoded message
109 */
110void msg_encode_end(struct encode_context *ectxt);
111
112/*
113 * msg_encode_end: Complete the message encode process
114 *
115 * @ectxt: Temporary storage which holds the encoded message
116 */
117void ipc_log_write(void *ctxt, struct encode_context *ectxt);
118
119/*
120 * ipc_log_string: Helper function to log a string
121 *
122 * @ilctxt: Debug Log Context created using ipc_log_context_create()
123 * @fmt: Data specified using format specifiers
124 */
125int ipc_log_string(void *ilctxt, const char *fmt, ...) __printf(2, 3);
126
127/**
128 * ipc_log_extract - Reads and deserializes log
129 *
130 * @ilctxt: logging context
131 * @buff: buffer to receive the data
132 * @size: size of the buffer
133 * @returns: 0 if no data read; >0 number of bytes read; < 0 error
134 *
135 * If no data is available to be read, then the ilctxt::read_avail
136 * completion is reinitialized. This allows clients to block
137 * until new log data is save.
138 */
139int ipc_log_extract(void *ilctxt, char *buff, int size);
140
141/*
142 * Print a string to decode context.
143 * @dctxt Decode context
144 * @args printf args
145 */
146#define IPC_SPRINTF_DECODE(dctxt, args...) \
147do { \
148 int i; \
149 i = scnprintf(dctxt->buff, dctxt->size, args); \
150 dctxt->buff += i; \
151 dctxt->size -= i; \
152} while (0)
153
154/*
155 * tsv_timestamp_read: Reads a timestamp
156 *
157 * @ectxt: Context retrieved by reading from log space
158 * @dctxt: Temporary storage to hold the decoded message
159 * @format: Output format while dumping through DEBUGFS
160 */
161void tsv_timestamp_read(struct encode_context *ectxt,
162 struct decode_context *dctxt, const char *format);
163
164/*
165 * tsv_qtimer_read: Reads a QTimer timestamp
166 *
167 * @ectxt: Context retrieved by reading from log space
168 * @dctxt: Temporary storage to hold the decoded message
169 * @format: Output format while dumping through DEBUGFS
170 */
171void tsv_qtimer_read(struct encode_context *ectxt,
172 struct decode_context *dctxt, const char *format);
173
174/*
175 * tsv_pointer_read: Reads a data pointer
176 *
177 * @ectxt: Context retrieved by reading from log space
178 * @dctxt: Temporary storage to hold the decoded message
179 * @format: Output format while dumping through DEBUGFS
180 */
181void tsv_pointer_read(struct encode_context *ectxt,
182 struct decode_context *dctxt, const char *format);
183
184/*
185 * tsv_int32_read: Reads a 32-bit integer value
186 *
187 * @ectxt: Context retrieved by reading from log space
188 * @dctxt: Temporary storage to hold the decoded message
189 * @format: Output format while dumping through DEBUGFS
190 */
191int32_t tsv_int32_read(struct encode_context *ectxt,
192 struct decode_context *dctxt, const char *format);
193
194/*
195 * tsv_int32_read: Reads a 32-bit integer value
196 *
197 * @ectxt: Context retrieved by reading from log space
198 * @dctxt: Temporary storage to hold the decoded message
199 * @format: Output format while dumping through DEBUGFS
200 */
201void tsv_byte_array_read(struct encode_context *ectxt,
202 struct decode_context *dctxt, const char *format);
203
204/*
205 * add_deserialization_func: Register a deserialization function to
206 * to unpack the subevents of a main event
207 *
208 * @ctxt: Debug log context to which the deserialization function has
209 * to be registered
210 * @type: Main/Root event, defined by the module which is logging, to
211 * which this deserialization function has to be registered.
212 * @dfune: Deserialization function to be registered
213 *
214 * return 0 on success, -ve value on FAILURE
215 */
216int add_deserialization_func(void *ctxt, int type,
217 void (*dfunc)(struct encode_context *,
218 struct decode_context *));
219
220/*
221 * ipc_log_context_destroy: Destroy debug log context
222 *
223 * @ctxt: debug log context created by calling ipc_log_context_create API.
224 */
225int ipc_log_context_destroy(void *ctxt);
226
227#else
228
229static inline void *ipc_log_context_create(int max_num_pages,
230 const char *modname, uint16_t user_version)
231{ return NULL; }
232
233static inline void msg_encode_start(struct encode_context *ectxt,
234 uint32_t type) { }
235
236static inline int tsv_timestamp_write(struct encode_context *ectxt)
237{ return -EINVAL; }
238
239static inline int tsv_qtimer_write(struct encode_context *ectxt)
240{ return -EINVAL; }
241
242static inline int tsv_pointer_write(struct encode_context *ectxt, void *pointer)
243{ return -EINVAL; }
244
245static inline int tsv_int32_write(struct encode_context *ectxt, int32_t n)
246{ return -EINVAL; }
247
248static inline int tsv_byte_array_write(struct encode_context *ectxt,
249 void *data, int data_size)
250{ return -EINVAL; }
251
252static inline void msg_encode_end(struct encode_context *ectxt) { }
253
254static inline void ipc_log_write(void *ctxt, struct encode_context *ectxt) { }
255
256static inline int ipc_log_string(void *ilctxt, const char *fmt, ...)
257{ return -EINVAL; }
258
259static inline int ipc_log_extract(void *ilctxt, char *buff, int size)
260{ return -EINVAL; }
261
262#define IPC_SPRINTF_DECODE(dctxt, args...) do { } while (0)
263
264static inline void tsv_timestamp_read(struct encode_context *ectxt,
265 struct decode_context *dctxt, const char *format) { }
266
267static inline void tsv_qtimer_read(struct encode_context *ectxt,
268 struct decode_context *dctxt, const char *format) { }
269
270static inline void tsv_pointer_read(struct encode_context *ectxt,
271 struct decode_context *dctxt, const char *format) { }
272
273static inline int32_t tsv_int32_read(struct encode_context *ectxt,
274 struct decode_context *dctxt, const char *format)
275{ return 0; }
276
277static inline void tsv_byte_array_read(struct encode_context *ectxt,
278 struct decode_context *dctxt, const char *format) { }
279
280static inline int add_deserialization_func(void *ctxt, int type,
281 void (*dfunc)(struct encode_context *,
282 struct decode_context *))
283{ return 0; }
284
285static inline int ipc_log_context_destroy(void *ctxt)
286{ return 0; }
287
288#endif
289
290#endif