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.
9 // The compiler knows that a print of a value of this type
10 // should use printhex instead of printuint (decimal).
13 func bytes(s
string) (ret
[]byte) {
14 rp
:= (*slice
)(unsafe
.Pointer(&ret
))
15 sp
:= (*_string
)(noescape(unsafe
.Pointer(&s
)))
22 // printf is only called from C code. It has no type information for the args,
23 // but C stacks are ignored by the garbage collector anyway, so having
24 // type information would not add anything.
26 func printf(s
*byte) {
27 vprintf(gostringnocopy(s
), add(unsafe
.Pointer(&s
), unsafe
.Sizeof(s
)))
30 // sprintf is only called from C code. It has no type information for the args,
31 // but C stacks are ignored by the garbage collector anyway, so having
32 // type information would not add anything.
34 func snprintf(dst
*byte, n
int32, s
*byte) {
35 buf
:= (*[1 << 30]byte)(unsafe
.Pointer(dst
))[0:n
:n
]
38 gp
.writebuf
= buf
[0:0 : n
-1] // leave room for NUL, this is called from C
39 vprintf(gostringnocopy(s
), add(unsafe
.Pointer(&s
), unsafe
.Sizeof(s
)))
40 buf
[len(gp
.writebuf
)] = '\x00'
46 // write to goroutine-local buffer if diverting output,
47 // or else standard error.
48 func gwrite(b
[]byte) {
53 if gp
== nil || gp
.writebuf
== nil {
54 write(2, unsafe
.Pointer(&b
[0]), int32(len(b
)))
58 n
:= copy(gp
.writebuf
[len(gp
.writebuf
):cap(gp
.writebuf
)], b
)
59 gp
.writebuf
= gp
.writebuf
[:len(gp
.writebuf
)+n
]
62 func prints(s
*byte) {
63 b
:= (*[1 << 30]byte)(unsafe
.Pointer(s
))
80 // Very simple printf. Only for debugging prints.
81 // Do not add to this without checking with Rob.
82 func vprintf(str
string, arg unsafe
.Pointer
) {
88 for ; i
< len(s
); i
++ {
102 case 'd', 'x': // 32-bit
103 arg
= roundup(arg
, 4)
105 case 'D', 'U', 'X', 'f': // 64-bit
106 arg
= roundup(arg
, unsafe
.Sizeof(uintreg(0)))
109 arg
= roundup(arg
, unsafe
.Sizeof(uintreg(0)))
111 case 'p', 's': // pointer-sized
112 arg
= roundup(arg
, unsafe
.Sizeof(uintptr(0)))
113 siz
= unsafe
.Sizeof(uintptr(0))
114 case 'S': // pointer-aligned but bigger
115 arg
= roundup(arg
, unsafe
.Sizeof(uintptr(0)))
116 siz
= unsafe
.Sizeof(string(""))
117 case 'a': // pointer-aligned but bigger
118 arg
= roundup(arg
, unsafe
.Sizeof(uintptr(0)))
119 siz
= unsafe
.Sizeof([]byte{})
120 case 'i', 'e': // pointer-aligned but bigger
121 arg
= roundup(arg
, unsafe
.Sizeof(uintptr(0)))
122 siz
= unsafe
.Sizeof(interface{}(nil))
126 printslice(*(*[]byte)(arg
))
128 printbyte(*(*byte)(arg
))
130 printint(int64(*(*int32)(arg
)))
132 printint(int64(*(*int64)(arg
)))
134 printeface(*(*interface{})(arg
))
136 printfloat(*(*float64)(arg
))
138 printcomplex(*(*complex128
)(arg
))
140 printiface(*(*fInterface
)(arg
))
142 printpointer(*(*unsafe
.Pointer
)(arg
))
144 prints(*(**byte)(arg
))
146 printstring(*(*string)(arg
))
148 printbool(*(*bool)(arg
))
150 printuint(*(*uint64)(arg
))
152 printhex(uint64(*(*uint32)(arg
)))
154 printhex(*(*uint64)(arg
))
163 //unlock(&debuglock);
166 func printpc(p unsafe
.Pointer
) {
167 print("PC=", hex(uintptr(p
)))
170 func printbool(v
bool) {
178 func printbyte(c
byte) {
179 gwrite((*[1]byte)(unsafe
.Pointer(&c
))[:])
182 func printfloat(v
float64) {
187 case v
+v
== v
&& v
> 0:
190 case v
+v
== v
&& v
< 0:
195 const n
= 7 // digits printed
221 for i
:= 0; i
< n
; i
++ {
231 // format +d.dddd+edd
232 for i
:= 0; i
< n
; i
++ {
234 buf
[i
+2] = byte(s
+ '0')
248 buf
[n
+4] = byte(e
/100) + '0'
249 buf
[n
+5] = byte(e
/10)%10
+ '0'
250 buf
[n
+6] = byte(e%10
) + '0'
254 func printcomplex(c complex128
) {
255 print("(", real(c
), imag(c
), "i)")
258 func printuint(v
uint64) {
261 for i
--; i
> 0; i
-- {
262 buf
[i
] = byte(v%10
+ '0')
271 func printint(v
int64) {
279 func printhex(v
uint64) {
280 const dig
= "0123456789abcdef"
283 for i
--; i
> 0; i
-- {
297 func printpointer(p unsafe
.Pointer
) {
298 printhex(uint64(uintptr(p
)))
301 func printstring(s
string) {
302 if uintptr(len(s
)) > maxstring
{
303 gwrite(bytes("[string too long]"))
309 func printslice(s
[]byte) {
310 sp
:= (*slice
)(unsafe
.Pointer(&s
))
311 print("[", len(s
), "/", cap(s
), "]")
312 printpointer(unsafe
.Pointer(sp
.array
))
315 func printeface(e
interface{}) {
316 ep
:= (*eface
)(unsafe
.Pointer(&e
))
317 print("(", ep
._type
, ",", ep
.data
, ")")
320 func printiface(i fInterface
) {
321 ip
:= (*iface
)(unsafe
.Pointer(&i
))
322 print("(", ip
.tab
, ",", ip
.data
, ")")