1 /* 2 * Copyright (C) 2016 Red Hat 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: 23 * Rob Clark <robdclark@gmail.com> 24 */ 25 26 #define DEBUG /* for pr_debug() */ 27 28 #include <sys/stdarg.h> 29 #include <linux/seq_file.h> 30 #include <drm/drmP.h> 31 #include <drm/drm_print.h> 32 33 void __drm_puts_coredump(struct drm_printer *p, const char *str) 34 { 35 struct drm_print_iterator *iterator = p->arg; 36 ssize_t len; 37 38 if (!iterator->remain) 39 return; 40 41 if (iterator->offset < iterator->start) { 42 ssize_t copy; 43 44 len = strlen(str); 45 46 if (iterator->offset + len <= iterator->start) { 47 iterator->offset += len; 48 return; 49 } 50 51 copy = len - (iterator->start - iterator->offset); 52 53 if (copy > iterator->remain) 54 copy = iterator->remain; 55 56 /* Copy out the bit of the string that we need */ 57 memcpy(iterator->data, 58 str + (iterator->start - iterator->offset), copy); 59 60 iterator->offset = iterator->start + copy; 61 iterator->remain -= copy; 62 } else { 63 ssize_t pos = iterator->offset - iterator->start; 64 65 len = min_t(ssize_t, strlen(str), iterator->remain); 66 67 memcpy(iterator->data + pos, str, len); 68 69 iterator->offset += len; 70 iterator->remain -= len; 71 } 72 } 73 EXPORT_SYMBOL(__drm_puts_coredump); 74 75 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf) 76 { 77 struct drm_print_iterator *iterator = p->arg; 78 size_t len; 79 char *buf; 80 81 if (!iterator->remain) 82 return; 83 84 /* Figure out how big the string will be */ 85 len = snprintf(NULL, 0, "%pV", vaf); 86 87 /* This is the easiest path, we've already advanced beyond the offset */ 88 if (iterator->offset + len <= iterator->start) { 89 iterator->offset += len; 90 return; 91 } 92 93 /* Then check if we can directly copy into the target buffer */ 94 if ((iterator->offset >= iterator->start) && (len < iterator->remain)) { 95 ssize_t pos = iterator->offset - iterator->start; 96 97 snprintf(((char *) iterator->data) + pos, 98 iterator->remain, "%pV", vaf); 99 100 iterator->offset += len; 101 iterator->remain -= len; 102 103 return; 104 } 105 106 /* 107 * Finally, hit the slow path and make a temporary string to copy over 108 * using _drm_puts_coredump 109 */ 110 buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); 111 if (!buf) 112 return; 113 114 snprintf(buf, len + 1, "%pV", vaf); 115 __drm_puts_coredump(p, (const char *) buf); 116 117 kfree(buf); 118 } 119 EXPORT_SYMBOL(__drm_printfn_coredump); 120 121 void __drm_puts_seq_file(struct drm_printer *p, const char *str) 122 { 123 seq_puts(p->arg, str); 124 } 125 EXPORT_SYMBOL(__drm_puts_seq_file); 126 127 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf) 128 { 129 seq_printf(p->arg, "%pV", vaf); 130 } 131 EXPORT_SYMBOL(__drm_printfn_seq_file); 132 133 #ifdef __linux__ 134 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf) 135 { 136 dev_info(p->arg, "[" DRM_NAME "] %pV", vaf); 137 } 138 EXPORT_SYMBOL(__drm_printfn_info); 139 140 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf) 141 { 142 pr_debug("%s %pV", p->prefix, vaf); 143 } 144 EXPORT_SYMBOL(__drm_printfn_debug); 145 #else 146 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf) 147 { 148 #ifdef DRMDEBUG 149 printf("[" DRM_NAME "] "); 150 vprintf(vaf->fmt, *vaf->va); 151 #endif 152 } 153 154 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf) 155 { 156 #ifdef DRMDEBUG 157 printf("%s ", p->prefix); 158 vprintf(vaf->fmt, *vaf->va); 159 #endif 160 } 161 #endif 162 163 /** 164 * drm_puts - print a const string to a &drm_printer stream 165 * @p: the &drm printer 166 * @str: const string 167 * 168 * Allow &drm_printer types that have a constant string 169 * option to use it. 170 */ 171 void drm_puts(struct drm_printer *p, const char *str) 172 { 173 if (p->puts) 174 p->puts(p, str); 175 else 176 drm_printf(p, "%s", str); 177 } 178 EXPORT_SYMBOL(drm_puts); 179 180 /** 181 * drm_printf - print to a &drm_printer stream 182 * @p: the &drm_printer 183 * @f: format string 184 */ 185 void drm_printf(struct drm_printer *p, const char *f, ...) 186 { 187 va_list args; 188 189 va_start(args, f); 190 drm_vprintf(p, f, &args); 191 va_end(args); 192 } 193 EXPORT_SYMBOL(drm_printf); 194 195 #ifdef __linux__ 196 void drm_dev_printk(const struct device *dev, const char *level, 197 const char *format, ...) 198 { 199 struct va_format vaf; 200 va_list args; 201 202 va_start(args, format); 203 vaf.fmt = format; 204 vaf.va = &args; 205 206 if (dev) 207 dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV", 208 __builtin_return_address(0), &vaf); 209 else 210 printk("%s" "[" DRM_NAME ":%ps] %pV", 211 level, __builtin_return_address(0), &vaf); 212 213 va_end(args); 214 } 215 EXPORT_SYMBOL(drm_dev_printk); 216 217 void drm_dev_dbg(const struct device *dev, unsigned int category, 218 const char *format, ...) 219 { 220 struct va_format vaf; 221 va_list args; 222 223 if (!(drm_debug & category)) 224 return; 225 226 va_start(args, format); 227 vaf.fmt = format; 228 vaf.va = &args; 229 230 if (dev) 231 dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV", 232 __builtin_return_address(0), &vaf); 233 else 234 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV", 235 __builtin_return_address(0), &vaf); 236 237 va_end(args); 238 } 239 EXPORT_SYMBOL(drm_dev_dbg); 240 241 void drm_dbg(unsigned int category, const char *format, ...) 242 { 243 struct va_format vaf; 244 va_list args; 245 246 if (!(drm_debug & category)) 247 return; 248 249 va_start(args, format); 250 vaf.fmt = format; 251 vaf.va = &args; 252 253 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV", 254 __builtin_return_address(0), &vaf); 255 256 va_end(args); 257 } 258 EXPORT_SYMBOL(drm_dbg); 259 260 void drm_err(const char *format, ...) 261 { 262 struct va_format vaf; 263 va_list args; 264 265 va_start(args, format); 266 vaf.fmt = format; 267 vaf.va = &args; 268 269 printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV", 270 __builtin_return_address(0), &vaf); 271 272 va_end(args); 273 } 274 EXPORT_SYMBOL(drm_err); 275 276 #else 277 278 void drm_dev_printk(const struct device *dev, const char *level, 279 const char *format, ...) 280 { 281 va_list args; 282 283 va_start(args, format); 284 printk("%s" "[" DRM_NAME "] ", level); 285 vprintf(format, args); 286 va_end(args); 287 } 288 289 void drm_dev_dbg(const struct device *dev, unsigned int category, 290 const char *format, ...) 291 { 292 va_list args; 293 294 if (!(drm_debug & category)) 295 return; 296 297 va_start(args, format); 298 printf(KERN_DEBUG "[" DRM_NAME "] "); 299 vprintf(format, args); 300 va_end(args); 301 } 302 303 void drm_dbg(unsigned int category, const char *format, ...) 304 { 305 va_list args; 306 307 if (!(drm_debug & category)) 308 return; 309 310 va_start(args, format); 311 printf(KERN_DEBUG "[" DRM_NAME "] "); 312 vprintf(format, args); 313 va_end(args); 314 } 315 316 void drm_err(const char *format, ...) 317 { 318 va_list args; 319 320 va_start(args, format); 321 printf(KERN_ERR "[" DRM_NAME "] *ERROR* "); 322 vprintf(format, args); 323 va_end(args); 324 } 325 #endif 326