Skip to content

Commit cfc57a1

Browse files
Jordan Crouserobclark
authored andcommitted
drm: drm_printer: Add printer for devcoredump
Add a drm printer suitable for use with the read callback for devcoredump or other suitable buffer based output format that isn't otherwise covered by seq_file. v2: Add improved documentation per Daniel Vetter Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org> Signed-off-by: Rob Clark <robdclark@gmail.com>
1 parent 489cae6 commit cfc57a1

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

‎drivers/gpu/drm/drm_print.c‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,80 @@
3030
#include <drm/drmP.h>
3131
#include <drm/drm_print.h>
3232

33+
void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
34+
{
35+
struct drm_print_iterator *iterator = p->arg;
36+
ssize_t len;
37+
38+
if (!iterator->remain)
39+
return;
40+
41+
/* Figure out how big the string will be */
42+
len = snprintf(NULL, 0, "%pV", vaf);
43+
44+
if (iterator->offset < iterator->start) {
45+
char *buf;
46+
ssize_t copy;
47+
48+
if (iterator->offset + len <= iterator->start) {
49+
iterator->offset += len;
50+
return;
51+
}
52+
53+
/* Print the string into a temporary buffer */
54+
buf = kmalloc(len + 1,
55+
GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
56+
if (!buf)
57+
return;
58+
59+
snprintf(buf, len + 1, "%pV", vaf);
60+
61+
copy = len - (iterator->start - iterator->offset);
62+
63+
if (copy > iterator->remain)
64+
copy = iterator->remain;
65+
66+
/* Copy out the bit of the string that we need */
67+
memcpy(iterator->data,
68+
buf + (iterator->start - iterator->offset), copy);
69+
70+
iterator->offset = iterator->start + copy;
71+
iterator->remain -= copy;
72+
73+
kfree(buf);
74+
} else {
75+
char *buf;
76+
ssize_t pos = iterator->offset - iterator->start;
77+
78+
if (len < iterator->remain) {
79+
snprintf(((char *) iterator->data) + pos,
80+
iterator->remain, "%pV", vaf);
81+
82+
iterator->offset += len;
83+
iterator->remain -= len;
84+
85+
return;
86+
}
87+
88+
/* Print the string into a temporary buffer */
89+
buf = kmalloc(len + 1,
90+
GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
91+
if (!buf)
92+
return;
93+
94+
snprintf(buf, len + 1, "%pV", vaf);
95+
96+
/* Copy out the remaining bits */
97+
memcpy(iterator->data + pos, buf, iterator->remain);
98+
99+
iterator->offset += iterator->remain;
100+
iterator->remain = 0;
101+
102+
kfree(buf);
103+
}
104+
}
105+
EXPORT_SYMBOL(__drm_printfn_coredump);
106+
33107
void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
34108
{
35109
seq_printf(p->arg, "%pV", vaf);

‎include/drm/drm_print.h‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct drm_printer {
7373
const char *prefix;
7474
};
7575

76+
void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf);
7677
void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf);
7778
void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
7879
void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf);
@@ -104,6 +105,70 @@ drm_vprintf(struct drm_printer *p, const char *fmt, va_list *va)
104105
#define drm_printf_indent(printer, indent, fmt, ...) \
105106
drm_printf((printer), "%.*s" fmt, (indent), "\t\t\t\t\tX", ##__VA_ARGS__)
106107

108+
/**
109+
* struct drm_print_iterator - local struct used with drm_printer_coredump
110+
* @data: Pointer to the devcoredump output buffer
111+
* @start: The offset within the buffer to start writing
112+
* @remain: The number of bytes to write for this iteration
113+
*/
114+
struct drm_print_iterator {
115+
void *data;
116+
ssize_t start;
117+
ssize_t remain;
118+
/* private: */
119+
ssize_t offset;
120+
};
121+
122+
/**
123+
* drm_coredump_printer - construct a &drm_printer that can output to a buffer
124+
* from the read function for devcoredump
125+
* @iter: A pointer to a struct drm_print_iterator for the read instance
126+
*
127+
* This wrapper extends drm_printf() to work with a dev_coredumpm() callback
128+
* function. The passed in drm_print_iterator struct contains the buffer
129+
* pointer, size and offset as passed in from devcoredump.
130+
*
131+
* For example::
132+
*
133+
* void coredump_read(char *buffer, loff_t offset, size_t count,
134+
* void *data, size_t datalen)
135+
* {
136+
* struct drm_print_iterator iter;
137+
* struct drm_printer p;
138+
*
139+
* iter.data = buffer;
140+
* iter.start = offset;
141+
* iter.remain = count;
142+
*
143+
* p = drm_coredump_printer(&iter);
144+
*
145+
* drm_printf(p, "foo=%d\n", foo);
146+
* }
147+
*
148+
* void makecoredump(...)
149+
* {
150+
* ...
151+
* dev_coredumpm(dev, THIS_MODULE, data, 0, GFP_KERNEL,
152+
* coredump_read, ...)
153+
* }
154+
*
155+
* RETURNS:
156+
* The &drm_printer object
157+
*/
158+
static inline struct drm_printer
159+
drm_coredump_printer(struct drm_print_iterator *iter)
160+
{
161+
struct drm_printer p = {
162+
.printfn = __drm_printfn_coredump,
163+
.arg = iter,
164+
};
165+
166+
/* Set the internal offset of the iterator to zero */
167+
iter->offset = 0;
168+
169+
return p;
170+
}
171+
107172
/**
108173
* drm_seq_file_printer - construct a &drm_printer that outputs to &seq_file
109174
* @f: the &struct seq_file to output to

0 commit comments

Comments
 (0)