drm_print.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #define DEBUG /* for pr_debug() */
  26. #include <stdarg.h>
  27. #include <linux/seq_file.h>
  28. #include <drm/drmP.h>
  29. #include <drm/drm_print.h>
  30. void __drm_puts_coredump(struct drm_printer *p, const char *str)
  31. {
  32. struct drm_print_iterator *iterator = p->arg;
  33. ssize_t len;
  34. if (!iterator->remain)
  35. return;
  36. if (iterator->offset < iterator->start) {
  37. ssize_t copy;
  38. len = strlen(str);
  39. if (iterator->offset + len <= iterator->start) {
  40. iterator->offset += len;
  41. return;
  42. }
  43. copy = len - (iterator->start - iterator->offset);
  44. if (copy > iterator->remain)
  45. copy = iterator->remain;
  46. /* Copy out the bit of the string that we need */
  47. memcpy(iterator->data,
  48. str + (iterator->start - iterator->offset), copy);
  49. iterator->offset = iterator->start + copy;
  50. iterator->remain -= copy;
  51. } else {
  52. ssize_t pos = iterator->offset - iterator->start;
  53. len = min_t(ssize_t, strlen(str), iterator->remain);
  54. memcpy(iterator->data + pos, str, len);
  55. iterator->offset += len;
  56. iterator->remain -= len;
  57. }
  58. }
  59. EXPORT_SYMBOL(__drm_puts_coredump);
  60. void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
  61. {
  62. struct drm_print_iterator *iterator = p->arg;
  63. size_t len;
  64. char *buf;
  65. if (!iterator->remain)
  66. return;
  67. /* Figure out how big the string will be */
  68. len = snprintf(NULL, 0, "%pV", vaf);
  69. /* This is the easiest path, we've already advanced beyond the offset */
  70. if (iterator->offset + len <= iterator->start) {
  71. iterator->offset += len;
  72. return;
  73. }
  74. /* Then check if we can directly copy into the target buffer */
  75. if ((iterator->offset >= iterator->start) && (len < iterator->remain)) {
  76. ssize_t pos = iterator->offset - iterator->start;
  77. snprintf(((char *) iterator->data) + pos,
  78. iterator->remain, "%pV", vaf);
  79. iterator->offset += len;
  80. iterator->remain -= len;
  81. return;
  82. }
  83. /*
  84. * Finally, hit the slow path and make a temporary string to copy over
  85. * using _drm_puts_coredump
  86. */
  87. buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  88. if (!buf)
  89. return;
  90. snprintf(buf, len + 1, "%pV", vaf);
  91. __drm_puts_coredump(p, (const char *) buf);
  92. kfree(buf);
  93. }
  94. EXPORT_SYMBOL(__drm_printfn_coredump);
  95. void __drm_puts_seq_file(struct drm_printer *p, const char *str)
  96. {
  97. seq_puts(p->arg, str);
  98. }
  99. EXPORT_SYMBOL(__drm_puts_seq_file);
  100. void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
  101. {
  102. seq_printf(p->arg, "%pV", vaf);
  103. }
  104. EXPORT_SYMBOL(__drm_printfn_seq_file);
  105. void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
  106. {
  107. dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
  108. }
  109. EXPORT_SYMBOL(__drm_printfn_info);
  110. void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
  111. {
  112. pr_debug("%s %pV", p->prefix, vaf);
  113. }
  114. EXPORT_SYMBOL(__drm_printfn_debug);
  115. /**
  116. * drm_puts - print a const string to a &drm_printer stream
  117. * @p: the &drm printer
  118. * @str: const string
  119. *
  120. * Allow &drm_printer types that have a constant string
  121. * option to use it.
  122. */
  123. void drm_puts(struct drm_printer *p, const char *str)
  124. {
  125. if (p->puts)
  126. p->puts(p, str);
  127. else
  128. drm_printf(p, "%s", str);
  129. }
  130. EXPORT_SYMBOL(drm_puts);
  131. /**
  132. * drm_printf - print to a &drm_printer stream
  133. * @p: the &drm_printer
  134. * @f: format string
  135. */
  136. void drm_printf(struct drm_printer *p, const char *f, ...)
  137. {
  138. va_list args;
  139. va_start(args, f);
  140. drm_vprintf(p, f, &args);
  141. va_end(args);
  142. }
  143. EXPORT_SYMBOL(drm_printf);
  144. void drm_dev_printk(const struct device *dev, const char *level,
  145. const char *format, ...)
  146. {
  147. struct va_format vaf;
  148. va_list args;
  149. va_start(args, format);
  150. vaf.fmt = format;
  151. vaf.va = &args;
  152. if (dev)
  153. dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
  154. __builtin_return_address(0), &vaf);
  155. else
  156. printk("%s" "[" DRM_NAME ":%ps] %pV",
  157. level, __builtin_return_address(0), &vaf);
  158. va_end(args);
  159. }
  160. EXPORT_SYMBOL(drm_dev_printk);
  161. void drm_dev_dbg(const struct device *dev, unsigned int category,
  162. const char *format, ...)
  163. {
  164. struct va_format vaf;
  165. va_list args;
  166. if (!(drm_debug & category))
  167. return;
  168. va_start(args, format);
  169. vaf.fmt = format;
  170. vaf.va = &args;
  171. if (dev)
  172. dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV",
  173. __builtin_return_address(0), &vaf);
  174. else
  175. printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
  176. __builtin_return_address(0), &vaf);
  177. va_end(args);
  178. }
  179. EXPORT_SYMBOL(drm_dev_dbg);
  180. void drm_dbg(unsigned int category, const char *format, ...)
  181. {
  182. struct va_format vaf;
  183. va_list args;
  184. if (!(drm_debug & category))
  185. return;
  186. va_start(args, format);
  187. vaf.fmt = format;
  188. vaf.va = &args;
  189. printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
  190. __builtin_return_address(0), &vaf);
  191. va_end(args);
  192. }
  193. EXPORT_SYMBOL(drm_dbg);
  194. void drm_err(const char *format, ...)
  195. {
  196. struct va_format vaf;
  197. va_list args;
  198. va_start(args, format);
  199. vaf.fmt = format;
  200. vaf.va = &args;
  201. printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
  202. __builtin_return_address(0), &vaf);
  203. va_end(args);
  204. }
  205. EXPORT_SYMBOL(drm_err);