printf.c 4.7 KB

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