printf.c 4.4 KB

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