printf.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. static inline int
  22. debug_fputs (const char * buf, int len)
  23. {
  24. if (DkStreamWrite(debug_handle, 0, len, (void *) buf, NULL) == (PAL_NUM) len)
  25. return 0;
  26. else
  27. return -1;
  28. }
  29. static int
  30. debug_fputch (void * f, int ch, void * b)
  31. {
  32. __UNUSED(f);
  33. struct debug_buf * buf = (struct debug_buf *) b;
  34. buf->buf[buf->end++] = ch;
  35. if (ch == '\n') {
  36. int ret = debug_fputs(buf->buf, buf->end);
  37. buf->end = buf->start;
  38. return ret;
  39. }
  40. #if DEBUGBUF_BREAK == 1
  41. if (buf->end == DEBUGBUF_SIZE - 4) {
  42. buf->buf[buf->end++] = '.';
  43. buf->buf[buf->end++] = '.';
  44. buf->buf[buf->end++] = '\n';
  45. debug_fputs(buf->buf, buf->end);
  46. buf->end = buf->start;
  47. buf->buf[buf->end++] = '.';
  48. buf->buf[buf->end++] = '.';
  49. }
  50. #else
  51. if (buf->end == DEBUGBUF_SIZE) {
  52. debug_fputs(buf->buf, buf->end);
  53. buf->end = buf->start;
  54. }
  55. #endif
  56. return 0;
  57. }
  58. void debug_puts (const char * str)
  59. {
  60. int len = strlen(str);
  61. struct debug_buf * buf = shim_get_tls()->debug_buf;
  62. while (len) {
  63. int rem = DEBUGBUF_SIZE - 4 - buf->end;
  64. bool isfull = true;
  65. if (rem > len) {
  66. rem = len;
  67. isfull = false;
  68. }
  69. for (int i = 0 ; i < rem ; i++)
  70. buf->buf[buf->end + i] = str[i];
  71. buf->end += rem;
  72. str += rem;
  73. len -= rem;
  74. if (isfull) {
  75. buf->buf[buf->end++] = '.';
  76. buf->buf[buf->end++] = '.';
  77. buf->buf[buf->end++] = '\n';
  78. debug_fputs(buf->buf, buf->end);
  79. buf->end = buf->start;
  80. buf->buf[buf->end++] = '.';
  81. buf->buf[buf->end++] = '.';
  82. }
  83. }
  84. }
  85. void debug_putch (int ch)
  86. {
  87. debug_fputch(NULL, ch, shim_get_tls()->debug_buf);
  88. }
  89. void debug_vprintf (const char * fmt, va_list * ap)
  90. {
  91. vfprintfmt((void *) debug_fputch, NULL, shim_get_tls()->debug_buf,
  92. fmt, ap);
  93. }
  94. void debug_printf (const char * fmt, ...)
  95. {
  96. va_list ap;
  97. va_start(ap, fmt);
  98. debug_vprintf(fmt, &ap);
  99. va_end(ap);
  100. }
  101. void debug_setprefix (shim_tcb_t * tcb)
  102. {
  103. if (!debug_handle)
  104. return;
  105. struct debug_buf * buf = tcb->debug_buf;
  106. buf->start = buf->end = 0;
  107. if (tcb->tid && !is_internal_tid(tcb->tid))
  108. fprintfmt(debug_fputch, NULL, buf, TID_PREFIX, tcb->tid);
  109. else if (cur_process.vmid)
  110. fprintfmt(debug_fputch, NULL, buf, VMID_PREFIX,
  111. cur_process.vmid & 0xFFFF);
  112. else
  113. fprintfmt(debug_fputch, NULL, buf, NOID_PREFIX);
  114. buf->start = buf->end;
  115. }
  116. struct sysbuf {
  117. int cnt;
  118. char buf[SYSPRINT_BUFFER_SIZE];
  119. } sys_putdat;
  120. static inline void
  121. sys_fputs (void * f, const char * str, int len)
  122. {
  123. DkStreamWrite((PAL_HANDLE) f, 0, len, (void *) str, NULL);
  124. }
  125. static void
  126. sys_fputch (void * f, int ch, void * b)
  127. {
  128. __UNUSED(b);
  129. sys_putdat.buf[sys_putdat.cnt++] = ch;
  130. if (ch == '\n') {
  131. sys_fputs(f, sys_putdat.buf, sys_putdat.cnt);
  132. sys_putdat.cnt = 0;
  133. }
  134. if (sys_putdat.cnt == SYSPRINT_BUFFER_SIZE - 2) {
  135. sys_putdat.buf[sys_putdat.cnt++] = '\n';
  136. sys_fputs(f, sys_putdat.buf, sys_putdat.cnt);
  137. sys_putdat.cnt = 0;
  138. }
  139. }
  140. static void
  141. sys_vfprintf (PAL_HANDLE hdl, const char * fmt, va_list * ap)
  142. {
  143. vfprintfmt((void *) &sys_fputch, hdl, NULL, fmt, ap);
  144. }
  145. void handle_printf (PAL_HANDLE hdl, const char * fmt, ...)
  146. {
  147. va_list ap;
  148. va_start(ap, fmt);
  149. sys_vfprintf(hdl, fmt, &ap);
  150. va_end(ap);
  151. }
  152. void handle_vprintf (PAL_HANDLE hdl, const char * fmt, va_list * ap)
  153. {
  154. sys_vfprintf(hdl, fmt, ap);
  155. }