print.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. #include <complex.h>
  5. #include <math.h>
  6. #include <stdarg.h>
  7. #include "runtime.h"
  8. #include "array.h"
  9. #include "go-type.h"
  10. //static Lock debuglock;
  11. // Clang requires this function to not be inlined (see below).
  12. static void go_vprintf(const char*, va_list)
  13. __attribute__((noinline));
  14. // write to goroutine-local buffer if diverting output,
  15. // or else standard error.
  16. static void
  17. gwrite(const void *v, intgo n)
  18. {
  19. G* g = runtime_g();
  20. if(g == nil || g->writebuf == nil) {
  21. // Avoid -D_FORTIFY_SOURCE problems.
  22. int rv __attribute__((unused));
  23. rv = runtime_write(2, v, n);
  24. return;
  25. }
  26. if(g->writenbuf == 0)
  27. return;
  28. if(n > g->writenbuf)
  29. n = g->writenbuf;
  30. runtime_memmove(g->writebuf, v, n);
  31. g->writebuf += n;
  32. g->writenbuf -= n;
  33. }
  34. void
  35. runtime_dump(byte *p, int32 n)
  36. {
  37. int32 i;
  38. for(i=0; i<n; i++) {
  39. runtime_printpointer((byte*)(uintptr)(p[i]>>4));
  40. runtime_printpointer((byte*)(uintptr)(p[i]&0xf));
  41. if((i&15) == 15)
  42. runtime_prints("\n");
  43. else
  44. runtime_prints(" ");
  45. }
  46. if(n & 15)
  47. runtime_prints("\n");
  48. }
  49. void
  50. runtime_prints(const char *s)
  51. {
  52. gwrite(s, runtime_findnull((const byte*)s));
  53. }
  54. #if defined (__clang__) && (defined (__i386__) || defined (__x86_64__))
  55. // LLVM's code generator does not currently support split stacks for vararg
  56. // functions, so we disable the feature for this function under Clang. This
  57. // appears to be OK as long as:
  58. // - this function only calls non-inlined, internal-linkage (hence no dynamic
  59. // loader) functions compiled with split stacks (i.e. go_vprintf), which can
  60. // allocate more stack space as required;
  61. // - this function itself does not occupy more than BACKOFF bytes of stack space
  62. // (see libgcc/config/i386/morestack.S).
  63. // These conditions are currently known to be satisfied by Clang on x86-32 and
  64. // x86-64. Note that signal handlers receive slightly less stack space than they
  65. // would normally do if they happen to be called while this function is being
  66. // run. If this turns out to be a problem we could consider increasing BACKOFF.
  67. void
  68. runtime_printf(const char *s, ...)
  69. __attribute__((no_split_stack));
  70. int32
  71. runtime_snprintf(byte *buf, int32 n, const char *s, ...)
  72. __attribute__((no_split_stack));
  73. #endif
  74. void
  75. runtime_printf(const char *s, ...)
  76. {
  77. va_list va;
  78. va_start(va, s);
  79. go_vprintf(s, va);
  80. va_end(va);
  81. }
  82. int32
  83. runtime_snprintf(byte *buf, int32 n, const char *s, ...)
  84. {
  85. G *g = runtime_g();
  86. va_list va;
  87. int32 m;
  88. g->writebuf = buf;
  89. g->writenbuf = n-1;
  90. va_start(va, s);
  91. go_vprintf(s, va);
  92. va_end(va);
  93. *g->writebuf = '\0';
  94. m = g->writebuf - buf;
  95. g->writenbuf = 0;
  96. g->writebuf = nil;
  97. return m;
  98. }
  99. // Very simple printf. Only for debugging prints.
  100. // Do not add to this without checking with Rob.
  101. static void
  102. go_vprintf(const char *s, va_list va)
  103. {
  104. const char *p, *lp;
  105. //runtime_lock(&debuglock);
  106. lp = p = s;
  107. for(; *p; p++) {
  108. if(*p != '%')
  109. continue;
  110. if(p > lp)
  111. gwrite(lp, p-lp);
  112. p++;
  113. switch(*p) {
  114. case 'a':
  115. runtime_printslice(va_arg(va, Slice));
  116. break;
  117. case 'c':
  118. runtime_printbyte(va_arg(va, int32));
  119. break;
  120. case 'd':
  121. runtime_printint(va_arg(va, int32));
  122. break;
  123. case 'D':
  124. runtime_printint(va_arg(va, int64));
  125. break;
  126. case 'e':
  127. runtime_printeface(va_arg(va, Eface));
  128. break;
  129. case 'f':
  130. runtime_printfloat(va_arg(va, float64));
  131. break;
  132. case 'C':
  133. runtime_printcomplex(va_arg(va, complex double));
  134. break;
  135. case 'i':
  136. runtime_printiface(va_arg(va, Iface));
  137. break;
  138. case 'p':
  139. runtime_printpointer(va_arg(va, void*));
  140. break;
  141. case 's':
  142. runtime_prints(va_arg(va, char*));
  143. break;
  144. case 'S':
  145. runtime_printstring(va_arg(va, String));
  146. break;
  147. case 't':
  148. runtime_printbool(va_arg(va, int));
  149. break;
  150. case 'U':
  151. runtime_printuint(va_arg(va, uint64));
  152. break;
  153. case 'x':
  154. runtime_printhex(va_arg(va, uint32));
  155. break;
  156. case 'X':
  157. runtime_printhex(va_arg(va, uint64));
  158. break;
  159. }
  160. lp = p+1;
  161. }
  162. if(p > lp)
  163. gwrite(lp, p-lp);
  164. //runtime_unlock(&debuglock);
  165. }
  166. void
  167. runtime_printpc(void *p __attribute__ ((unused)))
  168. {
  169. runtime_prints("PC=");
  170. runtime_printhex((uint64)(uintptr)runtime_getcallerpc(p));
  171. }
  172. void
  173. runtime_printbool(_Bool v)
  174. {
  175. if(v) {
  176. gwrite("true", 4);
  177. return;
  178. }
  179. gwrite("false", 5);
  180. }
  181. void
  182. runtime_printbyte(int8 c)
  183. {
  184. gwrite(&c, 1);
  185. }
  186. void
  187. runtime_printfloat(double v)
  188. {
  189. byte buf[20];
  190. int32 e, s, i, n;
  191. float64 h;
  192. if(ISNAN(v)) {
  193. gwrite("NaN", 3);
  194. return;
  195. }
  196. if(isinf(v)) {
  197. if(signbit(v)) {
  198. gwrite("-Inf", 4);
  199. } else {
  200. gwrite("+Inf", 4);
  201. }
  202. return;
  203. }
  204. n = 7; // digits printed
  205. e = 0; // exp
  206. s = 0; // sign
  207. if(v == 0) {
  208. if(isinf(1/v) && 1/v < 0)
  209. s = 1;
  210. } else {
  211. // sign
  212. if(v < 0) {
  213. v = -v;
  214. s = 1;
  215. }
  216. // normalize
  217. while(v >= 10) {
  218. e++;
  219. v /= 10;
  220. }
  221. while(v < 1) {
  222. e--;
  223. v *= 10;
  224. }
  225. // round
  226. h = 5;
  227. for(i=0; i<n; i++)
  228. h /= 10;
  229. v += h;
  230. if(v >= 10) {
  231. e++;
  232. v /= 10;
  233. }
  234. }
  235. // format +d.dddd+edd
  236. buf[0] = '+';
  237. if(s)
  238. buf[0] = '-';
  239. for(i=0; i<n; i++) {
  240. s = v;
  241. buf[i+2] = s+'0';
  242. v -= s;
  243. v *= 10.;
  244. }
  245. buf[1] = buf[2];
  246. buf[2] = '.';
  247. buf[n+2] = 'e';
  248. buf[n+3] = '+';
  249. if(e < 0) {
  250. e = -e;
  251. buf[n+3] = '-';
  252. }
  253. buf[n+4] = (e/100) + '0';
  254. buf[n+5] = (e/10)%10 + '0';
  255. buf[n+6] = (e%10) + '0';
  256. gwrite(buf, n+7);
  257. }
  258. void
  259. runtime_printcomplex(complex double v)
  260. {
  261. gwrite("(", 1);
  262. runtime_printfloat(creal(v));
  263. runtime_printfloat(cimag(v));
  264. gwrite("i)", 2);
  265. }
  266. void
  267. runtime_printuint(uint64 v)
  268. {
  269. byte buf[100];
  270. int32 i;
  271. for(i=nelem(buf)-1; i>0; i--) {
  272. buf[i] = v%10 + '0';
  273. if(v < 10)
  274. break;
  275. v = v/10;
  276. }
  277. gwrite(buf+i, nelem(buf)-i);
  278. }
  279. void
  280. runtime_printint(int64 v)
  281. {
  282. if(v < 0) {
  283. gwrite("-", 1);
  284. v = -v;
  285. }
  286. runtime_printuint(v);
  287. }
  288. void
  289. runtime_printhex(uint64 v)
  290. {
  291. static const char *dig = "0123456789abcdef";
  292. byte buf[100];
  293. int32 i;
  294. i=nelem(buf);
  295. for(; v>0; v/=16)
  296. buf[--i] = dig[v%16];
  297. if(i == nelem(buf))
  298. buf[--i] = '0';
  299. buf[--i] = 'x';
  300. buf[--i] = '0';
  301. gwrite(buf+i, nelem(buf)-i);
  302. }
  303. void
  304. runtime_printpointer(void *p)
  305. {
  306. runtime_printhex((uintptr)p);
  307. }
  308. void
  309. runtime_printstring(String v)
  310. {
  311. // if(v.len > runtime_maxstring) {
  312. // gwrite("[string too long]", 17);
  313. // return;
  314. // }
  315. if(v.len > 0)
  316. gwrite(v.str, v.len);
  317. }
  318. void
  319. __go_print_space(void)
  320. {
  321. gwrite(" ", 1);
  322. }
  323. void
  324. __go_print_nl(void)
  325. {
  326. gwrite("\n", 1);
  327. }