^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * trace_seq.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008-2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * The trace_seq is a handy tool that allows you to pass a descriptor around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * to a buffer that other functions can write to. It is similar to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * seq_file functionality but has some differences.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * To use it, the trace_seq must be initialized with trace_seq_init().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This will set up the counters within the descriptor. You can call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * trace_seq_init() more than once to reset the trace_seq to start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * from scratch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * The buffer size is currently PAGE_SIZE, although it may become dynamic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * in the future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * A write to the buffer will either succed or fail. That is, unlike
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * sprintf() there will not be a partial write (well it may write into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * the buffer but it wont update the pointers). This allows users to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * try to write something into the trace_seq buffer and if it fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * they can flush it and try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/trace_seq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* How much buffer is left on the trace_seq? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define TRACE_SEQ_BUF_LEFT(s) seq_buf_buffer_left(&(s)->seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * trace_seq should work with being initialized with 0s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static inline void __trace_seq_init(struct trace_seq *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (unlikely(!s->seq.size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * trace_print_seq - move the contents of trace_seq into a seq_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @m: the seq_file descriptor that is the destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @s: the trace_seq descriptor that is the source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Returns 0 on success and non zero on error. If it succeeds to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * write to the seq_file it will reset the trace_seq, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * it does not modify the trace_seq to let the caller try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int trace_print_seq(struct seq_file *m, struct trace_seq *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ret = seq_buf_print_seq(m, &s->seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Only reset this buffer if we successfully wrote to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * seq_file buffer. This lets the caller try again or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * do something else with the contents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * trace_seq_printf - sequence printing of trace information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * @fmt: printf format string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * The tracer may use either sequence operations or its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * copy to user routines. To simplify formating of a trace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * trace_seq_printf() is used to store strings into a special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * buffer (@s). Then the output may be either used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * the sequencer or pulled into another buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) void trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned int save_len = s->seq.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) va_start(ap, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) seq_buf_vprintf(&s->seq, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* If we can't write it all, don't bother writing anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (unlikely(seq_buf_has_overflowed(&s->seq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) s->seq.len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) EXPORT_SYMBOL_GPL(trace_seq_printf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * trace_seq_bitmask - write a bitmask array in its ASCII representation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * @maskp: points to an array of unsigned longs that represent a bitmask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @nmaskbits: The number of bits that are valid in @maskp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * Writes a ASCII representation of a bitmask string into @s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) void trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int nmaskbits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned int save_len = s->seq.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) seq_buf_printf(&s->seq, "%*pb", nmaskbits, maskp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (unlikely(seq_buf_has_overflowed(&s->seq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) s->seq.len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) EXPORT_SYMBOL_GPL(trace_seq_bitmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * trace_seq_vprintf - sequence printing of trace information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @fmt: printf format string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * The tracer may use either sequence operations or its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * copy to user routines. To simplify formating of a trace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * trace_seq_printf is used to store strings into a special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * buffer (@s). Then the output may be either used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * the sequencer or pulled into another buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned int save_len = s->seq.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) seq_buf_vprintf(&s->seq, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* If we can't write it all, don't bother writing anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (unlikely(seq_buf_has_overflowed(&s->seq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) s->seq.len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) EXPORT_SYMBOL_GPL(trace_seq_vprintf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * trace_seq_bprintf - Write the printf string from binary arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * @fmt: The format string for the @binary arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * @binary: The binary arguments for @fmt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * When recording in a fast path, a printf may be recorded with just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * saving the format and the arguments as they were passed to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * function, instead of wasting cycles converting the arguments into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * ASCII characters. Instead, the arguments are saved in a 32 bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * word array that is defined by the format string constraints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * This function will take the format and the binary array and finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * the conversion into the ASCII string within the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) void trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned int save_len = s->seq.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) seq_buf_bprintf(&s->seq, fmt, binary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* If we can't write it all, don't bother writing anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (unlikely(seq_buf_has_overflowed(&s->seq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) s->seq.len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) EXPORT_SYMBOL_GPL(trace_seq_bprintf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * trace_seq_puts - trace sequence printing of simple string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @str: simple string to record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * The tracer may use either the sequence operations or its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * copy to user routines. This function records a simple string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * into a special buffer (@s) for later retrieval by a sequencer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * or other mechanism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) void trace_seq_puts(struct trace_seq *s, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned int len = strlen(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (len > TRACE_SEQ_BUF_LEFT(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) seq_buf_putmem(&s->seq, str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) EXPORT_SYMBOL_GPL(trace_seq_puts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * trace_seq_putc - trace sequence printing of simple character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * @c: simple character to record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * The tracer may use either the sequence operations or its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * copy to user routines. This function records a simple charater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * into a special buffer (@s) for later retrieval by a sequencer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * or other mechanism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) void trace_seq_putc(struct trace_seq *s, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (TRACE_SEQ_BUF_LEFT(s) < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) seq_buf_putc(&s->seq, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) EXPORT_SYMBOL_GPL(trace_seq_putc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * trace_seq_putmem - write raw data into the trace_seq buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * @mem: The raw memory to copy into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * @len: The length of the raw memory to copy (in bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * There may be cases where raw memory needs to be written into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * buffer and a strcpy() would not work. Using this function allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * for such cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) void trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (len > TRACE_SEQ_BUF_LEFT(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) seq_buf_putmem(&s->seq, mem, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) EXPORT_SYMBOL_GPL(trace_seq_putmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * trace_seq_putmem_hex - write raw memory into the buffer in ASCII hex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @mem: The raw memory to write its hex ASCII representation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * @len: The length of the raw memory to copy (in bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * This is similar to trace_seq_putmem() except instead of just copying the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * raw memory into the buffer it writes its ASCII representation of it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * in hex characters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) void trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) unsigned int save_len = s->seq.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /* Each byte is represented by two chars */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (len * 2 > TRACE_SEQ_BUF_LEFT(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* The added spaces can still cause an overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) seq_buf_putmem_hex(&s->seq, mem, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (unlikely(seq_buf_has_overflowed(&s->seq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) s->seq.len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) EXPORT_SYMBOL_GPL(trace_seq_putmem_hex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * trace_seq_path - copy a path into the sequence buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * @path: path to write into the sequence buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Write a path name into the sequence buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * Returns 1 if we successfully written all the contents to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * Returns 0 if we the length to write is bigger than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * reserved buffer space. In this case, nothing gets written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int trace_seq_path(struct trace_seq *s, const struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) unsigned int save_len = s->seq.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (TRACE_SEQ_BUF_LEFT(s) < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) seq_buf_path(&s->seq, path, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (unlikely(seq_buf_has_overflowed(&s->seq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) s->seq.len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) EXPORT_SYMBOL_GPL(trace_seq_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * trace_seq_to_user - copy the squence buffer to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * @s: trace sequence descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * @ubuf: The userspace memory location to copy to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * @cnt: The amount to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * Copies the sequence buffer into the userspace memory pointed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * by @ubuf. It starts from the last read position (@s->readpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * and writes up to @cnt characters or till it reaches the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * the content in the buffer (@s->len), which ever comes first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * On success, it returns a positive number of the number of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * it copied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * On failure it returns -EBUSY if all of the content in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * sequence has been already read, which includes nothing in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * sequenc (@s->len == @s->readpos).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * Returns -EFAULT if the copy to userspace fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int trace_seq_to_user(struct trace_seq *s, char __user *ubuf, int cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return seq_buf_to_user(&s->seq, ubuf, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) EXPORT_SYMBOL_GPL(trace_seq_to_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int trace_seq_hex_dump(struct trace_seq *s, const char *prefix_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int prefix_type, int rowsize, int groupsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) const void *buf, size_t len, bool ascii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) unsigned int save_len = s->seq.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (s->full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) __trace_seq_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (TRACE_SEQ_BUF_LEFT(s) < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) seq_buf_hex_dump(&(s->seq), prefix_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) prefix_type, rowsize, groupsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) buf, len, ascii);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (unlikely(seq_buf_has_overflowed(&s->seq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) s->seq.len = save_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) s->full = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) EXPORT_SYMBOL(trace_seq_hex_dump);