blob: 6c30508fca1989f6f882933388245ae649cfa58a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Steven Rostedt95045042009-04-11 12:59:57 -04002#ifndef _LINUX_TRACE_SEQ_H
3#define _LINUX_TRACE_SEQ_H
4
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04005#include <linux/seq_buf.h>
Steven Rostedt6d723732009-04-10 14:53:50 -04006
Wu Zhangjin78be6912009-06-14 14:52:30 +08007#include <asm/page.h>
8
Steven Rostedt95045042009-04-11 12:59:57 -04009/*
10 * Trace sequences are used to allow a function to call several other functions
Jiri Olsa6d3f1e12009-10-23 19:36:19 -040011 * to create a string of data to use (up to a max of PAGE_SIZE).
Steven Rostedt95045042009-04-11 12:59:57 -040012 */
13
14struct trace_seq {
15 unsigned char buffer[PAGE_SIZE];
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040016 struct seq_buf seq;
Johannes Bergd184b312009-11-25 16:10:14 +010017 int full;
Steven Rostedt95045042009-04-11 12:59:57 -040018};
19
20static inline void
21trace_seq_init(struct trace_seq *s)
22{
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040023 seq_buf_init(&s->seq, s->buffer, PAGE_SIZE);
Johannes Bergd184b312009-11-25 16:10:14 +010024 s->full = 0;
Steven Rostedt95045042009-04-11 12:59:57 -040025}
26
Steven Rostedt (Red Hat)7b039cb2014-06-26 09:42:41 -040027/**
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -050028 * trace_seq_used - amount of actual data written to buffer
29 * @s: trace sequence descriptor
30 *
31 * Returns the amount of data written to the buffer.
32 *
33 * IMPORTANT!
34 *
35 * Use this instead of @s->seq.len if you need to pass the amount
36 * of data from the buffer to another buffer (userspace, or what not).
37 * The @s->seq.len on overflow is bigger than the buffer size and
38 * using it can cause access to undefined memory.
39 */
40static inline int trace_seq_used(struct trace_seq *s)
41{
42 return seq_buf_used(&s->seq);
43}
44
45/**
Steven Rostedt (Red Hat)7b039cb2014-06-26 09:42:41 -040046 * trace_seq_buffer_ptr - return pointer to next location in buffer
47 * @s: trace sequence descriptor
48 *
49 * Returns the pointer to the buffer where the next write to
50 * the buffer will happen. This is useful to save the location
51 * that is about to be written to and then return the result
52 * of that write.
53 */
54static inline unsigned char *
55trace_seq_buffer_ptr(struct trace_seq *s)
56{
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -050057 return s->buffer + seq_buf_used(&s->seq);
Steven Rostedt (Red Hat)7b039cb2014-06-26 09:42:41 -040058}
59
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -050060/**
61 * trace_seq_has_overflowed - return true if the trace_seq took too much
62 * @s: trace sequence descriptor
63 *
64 * Returns true if too much data was added to the trace_seq and it is
65 * now full and will not take anymore.
66 */
67static inline bool trace_seq_has_overflowed(struct trace_seq *s)
68{
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -040069 return s->full || seq_buf_has_overflowed(&s->seq);
Steven Rostedt (Red Hat)19a7fe22014-11-12 10:29:54 -050070}
71
Steven Rostedt95045042009-04-11 12:59:57 -040072/*
73 * Currently only defined when tracing is enabled.
74 */
75#ifdef CONFIG_TRACING
Joe Perchesb9075fa2011-10-31 17:11:33 -070076extern __printf(2, 3)
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -050077void trace_seq_printf(struct trace_seq *s, const char *fmt, ...);
Joe Perchesb9075fa2011-10-31 17:11:33 -070078extern __printf(2, 0)
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -050079void trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args);
80extern void
Steven Rostedt95045042009-04-11 12:59:57 -040081trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary);
Steven Rostedta63ce5b2009-12-07 09:11:39 -050082extern int trace_print_seq(struct seq_file *m, struct trace_seq *s);
Steven Rostedt (Red Hat)36aabff2014-06-20 17:38:01 -040083extern int trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
84 int cnt);
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -050085extern void trace_seq_puts(struct trace_seq *s, const char *str);
86extern void trace_seq_putc(struct trace_seq *s, unsigned char c);
87extern void trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len);
88extern void trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
Steven Rostedt (Red Hat)36aabff2014-06-20 17:38:01 -040089 unsigned int len);
Al Viro38eff282012-03-14 21:51:10 -040090extern int trace_seq_path(struct trace_seq *s, const struct path *path);
Steven Rostedt95045042009-04-11 12:59:57 -040091
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -050092extern void trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
Steven Rostedt (Red Hat)4449bf92014-05-06 13:10:24 -040093 int nmaskbits);
94
Piotr Maziarzef56e042019-11-07 13:45:38 +010095extern int trace_seq_hex_dump(struct trace_seq *s, const char *prefix_str,
96 int prefix_type, int rowsize, int groupsize,
97 const void *buf, size_t len, bool ascii);
98
Steven Rostedt95045042009-04-11 12:59:57 -040099#else /* CONFIG_TRACING */
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -0500100static inline void trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
Steven Rostedt95045042009-04-11 12:59:57 -0400101{
Steven Rostedt95045042009-04-11 12:59:57 -0400102}
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -0500103static inline void
Steven Rostedt95045042009-04-11 12:59:57 -0400104trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
105{
Steven Rostedt95045042009-04-11 12:59:57 -0400106}
107
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -0500108static inline void
Steven Rostedt (Red Hat)4449bf92014-05-06 13:10:24 -0400109trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
110 int nmaskbits)
111{
Steven Rostedt (Red Hat)4449bf92014-05-06 13:10:24 -0400112}
113
Steven Rostedta63ce5b2009-12-07 09:11:39 -0500114static inline int trace_print_seq(struct seq_file *m, struct trace_seq *s)
Steven Rostedt95045042009-04-11 12:59:57 -0400115{
Steven Rostedta63ce5b2009-12-07 09:11:39 -0500116 return 0;
Steven Rostedt95045042009-04-11 12:59:57 -0400117}
Steven Rostedt (Red Hat)36aabff2014-06-20 17:38:01 -0400118static inline int trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
119 int cnt)
Steven Rostedt95045042009-04-11 12:59:57 -0400120{
121 return 0;
122}
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -0500123static inline void trace_seq_puts(struct trace_seq *s, const char *str)
Steven Rostedt95045042009-04-11 12:59:57 -0400124{
Steven Rostedt95045042009-04-11 12:59:57 -0400125}
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -0500126static inline void trace_seq_putc(struct trace_seq *s, unsigned char c)
Steven Rostedt95045042009-04-11 12:59:57 -0400127{
Steven Rostedt95045042009-04-11 12:59:57 -0400128}
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -0500129static inline void
Steven Rostedt (Red Hat)36aabff2014-06-20 17:38:01 -0400130trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
Steven Rostedt95045042009-04-11 12:59:57 -0400131{
Steven Rostedt95045042009-04-11 12:59:57 -0400132}
Steven Rostedt (Red Hat)dba39442014-11-12 18:07:22 -0500133static inline void trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
Steven Rostedt (Red Hat)36aabff2014-06-20 17:38:01 -0400134 unsigned int len)
Steven Rostedt95045042009-04-11 12:59:57 -0400135{
Steven Rostedt95045042009-04-11 12:59:57 -0400136}
Al Viro38eff282012-03-14 21:51:10 -0400137static inline int trace_seq_path(struct trace_seq *s, const struct path *path)
Steven Rostedt95045042009-04-11 12:59:57 -0400138{
139 return 0;
140}
141#endif /* CONFIG_TRACING */
142
143#endif /* _LINUX_TRACE_SEQ_H */