printf.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <shim_defs.h>
  14. #include <shim_internal.h>
  15. #include <shim_ipc.h>
  16. #include <pal.h>
  17. #include <api.h>
  18. #include <stdint.h>
  19. #include <stdarg.h>
  20. PAL_HANDLE debug_handle = NULL;
  21. struct debugbuf {
  22. int cnt;
  23. char buf[DEBUGBUF_SIZE];
  24. };
  25. static inline int
  26. debug_fputs (void * f, const char * buf, int len)
  27. {
  28. if (DkStreamWrite(debug_handle, 0, len, (void *) buf, NULL) == len)
  29. return 0;
  30. else
  31. return -1;
  32. }
  33. static int
  34. debug_fputch (void * f, int ch, void * b)
  35. {
  36. struct debug_buf * buf = (struct debug_buf *) b;
  37. buf->buf[buf->end++] = ch;
  38. if (ch == '\n') {
  39. int ret = debug_fputs(NULL, buf->buf, buf->end);
  40. buf->end = buf->start;
  41. return ret;
  42. }
  43. #if DEBUGBUF_BREAK == 1
  44. if (buf->end == DEBUGBUF_SIZE - 4) {
  45. buf->buf[buf->end++] = '.';
  46. buf->buf[buf->end++] = '.';
  47. buf->buf[buf->end++] = '\n';
  48. debug_fputs(NULL, buf->buf, buf->end);
  49. buf->end = buf->start;
  50. buf->buf[buf->end++] = '.';
  51. buf->buf[buf->end++] = '.';
  52. }
  53. #else
  54. if (buf->end == DEBUGBUF_SIZE) {
  55. debug_fputs(NULL, buf->buf, buf->end);
  56. buf->end = buf->start;
  57. }
  58. #endif
  59. return 0;
  60. }
  61. void debug_puts (const char * str)
  62. {
  63. int len = strlen(str);
  64. struct debug_buf * buf = shim_get_tls()->debug_buf;
  65. while (len) {
  66. int rem = DEBUGBUF_SIZE - 4 - buf->end;
  67. bool isfull = true;
  68. if (rem > len) {
  69. rem = len;
  70. isfull = false;
  71. }
  72. for (int i = 0 ; i < rem ; i++)
  73. buf->buf[buf->end + i] = str[i];
  74. buf->end += rem;
  75. str += rem;
  76. len -= rem;
  77. if (isfull) {
  78. buf->buf[buf->end++] = '.';
  79. buf->buf[buf->end++] = '.';
  80. buf->buf[buf->end++] = '\n';
  81. debug_fputs(NULL, buf->buf, buf->end);
  82. buf->end = buf->start;
  83. buf->buf[buf->end++] = '.';
  84. buf->buf[buf->end++] = '.';
  85. }
  86. }
  87. }
  88. void debug_putch (int ch)
  89. {
  90. debug_fputch(NULL, ch, shim_get_tls()->debug_buf);
  91. }
  92. void debug_vprintf (const char * fmt, va_list * ap)
  93. {
  94. vfprintfmt((void *) debug_fputch, NULL, shim_get_tls()->debug_buf,
  95. fmt, ap);
  96. }
  97. void debug_printf (const char * fmt, ...)
  98. {
  99. va_list ap;
  100. va_start(ap, fmt);
  101. debug_vprintf(fmt, &ap);
  102. va_end(ap);
  103. }
  104. void debug_setprefix (shim_tcb_t * tcb)
  105. {
  106. if (!debug_handle)
  107. return;
  108. struct debug_buf * buf = tcb->debug_buf;
  109. buf->start = buf->end = 0;
  110. if (tcb->tid && !is_internal_tid(tcb->tid))
  111. fprintfmt(debug_fputch, NULL, buf, TID_PREFIX, tcb->tid);
  112. else if (cur_process.vmid)
  113. fprintfmt(debug_fputch, NULL, buf, VMID_PREFIX,
  114. cur_process.vmid & 0xFFFF);
  115. else
  116. fprintfmt(debug_fputch, NULL, buf, NOID_PREFIX);
  117. buf->start = buf->end;
  118. }
  119. struct sysbuf {
  120. int cnt;
  121. char buf[SYSPRINT_BUFFER_SIZE];
  122. } sys_putdat;
  123. static inline void
  124. sys_fputs (void * f, const char * str, int len)
  125. {
  126. DkStreamWrite((PAL_HANDLE) f, 0, len, (void *) str, NULL);
  127. }
  128. static void
  129. sys_fputch (void * f, int ch, void * b)
  130. {
  131. sys_putdat.buf[sys_putdat.cnt++] = ch;
  132. if (ch == '\n') {
  133. sys_fputs(f, sys_putdat.buf, sys_putdat.cnt);
  134. sys_putdat.cnt = 0;
  135. }
  136. if (sys_putdat.cnt == SYSPRINT_BUFFER_SIZE - 2) {
  137. sys_putdat.buf[sys_putdat.cnt++] = '\n';
  138. sys_fputs(f, sys_putdat.buf, sys_putdat.cnt);
  139. sys_putdat.cnt = 0;
  140. }
  141. }
  142. static void
  143. sys_vfprintf (PAL_HANDLE hdl, const char * fmt, va_list * ap)
  144. {
  145. vfprintfmt((void *) &sys_fputch, hdl, NULL, fmt, ap);
  146. }
  147. void handle_printf (PAL_HANDLE hdl, const char * fmt, ...)
  148. {
  149. va_list ap;
  150. va_start(ap, fmt);
  151. sys_vfprintf(hdl, fmt, &ap);
  152. va_end(ap);
  153. }
  154. void handle_vprintf (PAL_HANDLE hdl, const char * fmt, va_list * ap)
  155. {
  156. sys_vfprintf(hdl, fmt, ap);
  157. }