mich...@chamberlain.net.au writes: > On 25/05/2012 6:45 AM, Ian Lance Taylor wrote: >> This patch to libgo copies the implementation of runtime_printf ... > > The actual patch attached to the message was the one you had earlier > posted for the Go 1 release compatibility with the off-by-1 error in > runtime.Callers, rather than the described patch.
Sorry, here is the correct patch. Ian
Index: libgo/runtime/msize.c =================================================================== --- libgo/runtime/msize.c (revision 187847) +++ libgo/runtime/msize.c (revision 187848) @@ -103,7 +103,7 @@ runtime_InitSizes(void) sizeclass++; } if(sizeclass != NumSizeClasses) { - // runtime_printf("sizeclass=%d NumSizeClasses=%d\n", sizeclass, NumSizeClasses); + runtime_printf("sizeclass=%d NumSizeClasses=%d\n", sizeclass, NumSizeClasses); runtime_throw("InitSizes - bad NumSizeClasses"); } @@ -122,13 +122,13 @@ runtime_InitSizes(void) for(n=0; n < MaxSmallSize; n++) { sizeclass = runtime_SizeToClass(n); if(sizeclass < 1 || sizeclass >= NumSizeClasses || runtime_class_to_size[sizeclass] < n) { - // runtime_printf("size=%d sizeclass=%d runtime_class_to_size=%d\n", n, sizeclass, runtime_class_to_size[sizeclass]); - // runtime_printf("incorrect SizeToClass"); + runtime_printf("size=%d sizeclass=%d runtime_class_to_size=%d\n", n, sizeclass, runtime_class_to_size[sizeclass]); + runtime_printf("incorrect SizeToClass"); goto dump; } if(sizeclass > 1 && runtime_class_to_size[sizeclass-1] >= n) { - // runtime_printf("size=%d sizeclass=%d runtime_class_to_size=%d\n", n, sizeclass, runtime_class_to_size[sizeclass]); - // runtime_printf("SizeToClass too big"); + runtime_printf("size=%d sizeclass=%d runtime_class_to_size=%d\n", n, sizeclass, runtime_class_to_size[sizeclass]); + runtime_printf("SizeToClass too big"); goto dump; } } Index: libgo/runtime/thread-linux.c =================================================================== --- libgo/runtime/thread-linux.c (revision 187847) +++ libgo/runtime/thread-linux.c (revision 187848) @@ -68,7 +68,7 @@ runtime_futexwakeup(uint32 *addr, uint32 // I don't know that futex wakeup can return // EAGAIN or EINTR, but if it does, it would be // safe to loop and call futex again. - runtime_printf("futexwakeup addr=%p returned %lld\n", addr, (long long)ret); + runtime_printf("futexwakeup addr=%p returned %D\n", addr, ret); *(int32*)0x1006 = 0x1006; } Index: libgo/runtime/chan.c =================================================================== --- libgo/runtime/chan.c (revision 187847) +++ libgo/runtime/chan.c (revision 187848) @@ -100,8 +100,8 @@ runtime_makechan_c(ChanType *t, int64 hi c->dataqsiz = hint; if(debug) - runtime_printf("makechan: chan=%p; elemsize=%lld; elemalign=%d; dataqsiz=%d\n", - c, (long long)elem->__size, elem->__align, c->dataqsiz); + runtime_printf("makechan: chan=%p; elemsize=%D; elemalign=%d; dataqsiz=%d\n", + c, (int64)elem->__size, elem->__align, c->dataqsiz); return c; } Index: libgo/runtime/malloc.goc =================================================================== --- libgo/runtime/malloc.goc (revision 187847) +++ libgo/runtime/malloc.goc (revision 187848) @@ -139,7 +139,7 @@ __go_free(void *v) m->mallocing = 1; if(!runtime_mlookup(v, nil, nil, &s)) { - // runtime_printf("free %p: not an allocated block\n", v); + runtime_printf("free %p: not an allocated block\n", v); runtime_throw("free runtime_mlookup"); } prof = runtime_blockspecial(v); @@ -367,7 +367,7 @@ runtime_mallocinit(void) if(p == nil) runtime_throw("runtime: cannot reserve arena virtual address space"); if((uintptr)p & (((uintptr)1<<PageShift)-1)) - runtime_printf("runtime: SysReserve returned unaligned address %p; asked for %p", p, (void*)(bitmap_size+arena_size)); + runtime_printf("runtime: SysReserve returned unaligned address %p; asked for %p", p, bitmap_size+arena_size); } if((uintptr)p & (((uintptr)1<<PageShift)-1)) runtime_throw("runtime: SysReserve returned unaligned address"); @@ -467,15 +467,15 @@ func SetFinalizer(obj Eface, finalizer E const FuncType *ft; if(obj.__type_descriptor == nil) { - // runtime·printf("runtime.SetFinalizer: first argument is nil interface\n"); + runtime_printf("runtime.SetFinalizer: first argument is nil interface\n"); goto throw; } if(obj.__type_descriptor->__code != GO_PTR) { - // runtime_printf("runtime.SetFinalizer: first argument is %S, not pointer\n", *obj.type->string); + runtime_printf("runtime.SetFinalizer: first argument is %S, not pointer\n", *obj.__type_descriptor->__reflection); goto throw; } if(!runtime_mlookup(obj.__object, &base, &size, nil) || obj.__object != base) { - // runtime_printf("runtime.SetFinalizer: pointer not at beginning of allocated block\n"); + runtime_printf("runtime.SetFinalizer: pointer not at beginning of allocated block\n"); goto throw; } ft = nil; @@ -494,7 +494,7 @@ func SetFinalizer(obj Eface, finalizer E return; badfunc: - // runtime_printf("runtime.SetFinalizer: second argument is %S, not func(%S)\n", *finalizer.type->string, *obj.type->string); + runtime_printf("runtime.SetFinalizer: second argument is %S, not func(%S)\n", *finalizer.__type_descriptor->__reflection, *obj.__type_descriptor->__reflection); throw: runtime_throw("runtime.SetFinalizer"); } Index: libgo/runtime/go-print.c =================================================================== --- libgo/runtime/go-print.c (revision 187847) +++ libgo/runtime/go-print.c (revision 187848) @@ -8,6 +8,7 @@ #include <stdint.h> #include <stdio.h> +#include "runtime.h" #include "array.h" #include "go-panic.h" #include "go-string.h" @@ -17,154 +18,20 @@ the predeclared functions print/println/panic/panicln. */ void -__go_print_space () -{ - putc (' ', stderr); -} - -void -__go_print_nl () -{ - putc ('\n', stderr); -} - -void -__go_print_string (struct __go_string val) -{ - fprintf (stderr, "%.*s", (int) val.__length, (const char *) val.__data); -} - -void -__go_print_uint64 (uint64_t val) -{ - fprintf (stderr, "%llu", (unsigned long long) val); -} - -void -__go_print_int64 (int64_t val) -{ - fprintf (stderr, "%lld", (long long) val); -} - -void -__go_print_double (double v) -{ - char buf[20]; - int e, s, i, n; - double h; - - if (isnan (v)) - { - fputs ("NaN", stderr); - return; - } - if (__builtin_isinf (v)) - { - putc (v < 0 ? '-' : '+', stderr); - fputs ("Inf", stderr); - return; - } - - /* The number of digits printed. */ - n = 7; - /* The exponent. */ - e = 0; - /* The sign. */ - s = 0; - if (v != 0) - { - if (v < 0) - { - v = -v; - s = 1; - } - - /* Normalize. */ - while (v >= 10) - { - ++e; - v /= 10; - } - while (v < 1) - { - --e; - v *= 10; - } - - /* Round. */ - h = 5; - for (i = 0; i < n; ++i) - h /= 10; - - v += h; - if (v >= 10) - { - ++e; - v /= 10; - } - } - - /* The format is +d.dddd+edd. */ - buf[0] = s ? '-' : '+'; - for (i = 0; i < n; ++i) - { - int d; - - d = v; - buf[i + 2] = d + '0'; - v -= d; - v *= 10; - } - buf[1] = buf[2]; - buf[2] = '.'; - - buf[n + 2] = 'e'; - buf[n + 3] = e < 0 ? '-' : '+'; - if (e < 0) - e = - e; - buf[n + 4] = e / 100 + '0'; - buf[n + 5] = (e / 10) % 10 + '0'; - buf[n + 6] = e % 10 + '0'; - buf[n + 7] = '\0'; - fputs (buf, stderr); -} - -void -__go_print_complex (__complex double val) -{ - putc ('(', stderr); - __go_print_double (__builtin_creal (val)); - __go_print_double (__builtin_cimag (val)); - fputs ("i)", stderr); -} - -void -__go_print_bool (_Bool val) -{ - fputs (val ? "true" : "false", stderr); -} - -void -__go_print_pointer (void *val) -{ - fprintf (stderr, "0x%lx", (unsigned long) (uintptr_t) val); -} - -void __go_print_empty_interface (struct __go_empty_interface e) { - fprintf (stderr, "(%p,%p)", e.__type_descriptor, e.__object); + runtime_printf ("(%p,%p)", e.__type_descriptor, e.__object); } void __go_print_interface (struct __go_interface i) { - fprintf (stderr, "(%p,%p)", i.__methods, i.__object); + runtime_printf ("(%p,%p)", i.__methods, i.__object); } void __go_print_slice (struct __go_open_array val) { - fprintf (stderr, "[%d/%d]", val.__count, val.__capacity); - __go_print_pointer (val.__values); + runtime_printf ("[%d/%d]", val.__count, val.__capacity); + runtime_printpointer (val.__values); } Index: libgo/runtime/go-assert.c =================================================================== --- libgo/runtime/go-assert.c (revision 187847) +++ libgo/runtime/go-assert.c (revision 187848) @@ -7,12 +7,13 @@ #include <stdio.h> #include <stdlib.h> +#include "runtime.h" #include "go-assert.h" void __go_assert_fail (const char *file, unsigned int lineno) { /* FIXME: Eventually we should dump a stack trace here. */ - fprintf (stderr, "%s:%u: libgo assertion failure\n", file, lineno); + runtime_printf ("%s:%U: libgo assertion failure\n", file, (uint64) lineno); abort (); } Index: libgo/runtime/runtime.c =================================================================== --- libgo/runtime/runtime.c (revision 187847) +++ libgo/runtime/runtime.c (revision 187848) @@ -236,3 +236,15 @@ runtime_showframe(const unsigned char *s traceback = runtime_gotraceback(); return traceback > 1 || (__builtin_strchr((const char*)s, '.') != nil && __builtin_memcmp(s, "runtime.", 7) != 0); } + +bool +runtime_isInf(float64 f, int32 sign) +{ + if(!__builtin_isinf(f)) + return false; + if(sign == 0) + return true; + if(sign > 0) + return f > 0; + return f < 0; +} Index: libgo/runtime/runtime.h =================================================================== --- libgo/runtime/runtime.h (revision 187847) +++ libgo/runtime/runtime.h (revision 187848) @@ -14,6 +14,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#include <unistd.h> #include <pthread.h> #include <semaphore.h> #include <ucontext.h> @@ -275,6 +276,7 @@ int32 runtime_ncpu; * common functions and data */ int32 runtime_findnull(const byte*); +void runtime_dump(byte*, int32); /* * very low level c-called @@ -286,6 +288,8 @@ void runtime_goenvs(void); void runtime_goenvs_unix(void); void runtime_throw(const char*) __attribute__ ((noreturn)); void runtime_panicstring(const char*) __attribute__ ((noreturn)); +void runtime_prints(const char*); +void runtime_printf(const char*, ...); void* runtime_mal(uintptr); void runtime_schedinit(void); void runtime_initsig(void); @@ -374,9 +378,35 @@ void __wrap_rtems_task_variable_add(void #endif /* + * Names generated by gccgo. + */ +#define runtime_printbool __go_print_bool +#define runtime_printfloat __go_print_double +#define runtime_printint __go_print_int64 +#define runtime_printiface __go_print_interface +#define runtime_printeface __go_print_empty_interface +#define runtime_printstring __go_print_string +#define runtime_printpointer __go_print_pointer +#define runtime_printuint __go_print_uint64 +#define runtime_printslice __go_print_slice +#define runtime_printcomplex __go_print_complex + +/* * runtime go-called */ -void runtime_panic(Eface); +void runtime_printbool(_Bool); +void runtime_printfloat(double); +void runtime_printint(int64); +void runtime_printiface(Iface); +void runtime_printeface(Eface); +void runtime_printstring(String); +void runtime_printpc(void*); +void runtime_printpointer(void*); +void runtime_printuint(uint64); +void runtime_printhex(uint64); +void runtime_printslice(Slice); +void runtime_printcomplex(__complex double); + struct __go_func_type; void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool, void **, void **) @@ -384,7 +414,7 @@ void reflect_call(const struct __go_func /* Functions. */ #define runtime_panic __go_panic -#define runtime_printf printf +#define runtime_write(d, v, n) write((d), (v), (n)) #define runtime_malloc(s) __go_alloc(s) #define runtime_free(p) __go_free(p) #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2)) @@ -432,6 +462,8 @@ void runtime_newErrorString(String, Efac /* * wrapped for go users */ +bool runtime_isInf(float64 f, int32 sign); +#define runtime_isNaN(f) __builtin_isnan(f) void runtime_semacquire(uint32 volatile *); void runtime_semrelease(uint32 volatile *); int32 runtime_gomaxprocsfunc(int32 n); Index: libgo/runtime/go-panic.c =================================================================== --- libgo/runtime/go-panic.c (revision 187847) +++ libgo/runtime/go-panic.c (revision 187848) @@ -24,13 +24,13 @@ __printpanics (struct __go_panic_stack * if (p->__next != NULL) { __printpanics (p->__next); - fprintf (stderr, "\t"); + runtime_printf ("\t"); } - fprintf (stderr, "panic: "); + runtime_printf ("panic: "); runtime_printany (p->__arg); if (p->__was_recovered) - fprintf (stderr, " [recovered]"); - fputc ('\n', stderr); + runtime_printf (" [recovered]"); + runtime_printf ("\n"); } /* This implements __go_panic which is used for the panic Index: libgo/runtime/mgc0.c =================================================================== --- libgo/runtime/mgc0.c (revision 187847) +++ libgo/runtime/mgc0.c (revision 187848) @@ -159,7 +159,7 @@ scanblock(byte *b, int64 n) bool keepworking; if((int64)(uintptr)n != n || n < 0) { - // runtime_printf("scanblock %p %lld\n", b, (long long)n); + runtime_printf("scanblock %p %D\n", b, n); runtime_throw("scanblock"); } @@ -190,7 +190,7 @@ scanblock(byte *b, int64 n) // Each iteration scans the block b of length n, queueing pointers in // the work buffer. if(Debug > 1) - runtime_printf("scanblock %p %lld\n", b, (long long) n); + runtime_printf("scanblock %p %D\n", b, n); vp = (void**)b; n >>= (2+PtrSize/8); /* n /= PtrSize (4 or 8) */ @@ -346,7 +346,7 @@ debug_scanblock(byte *b, int64 n) runtime_throw("debug_scanblock without DebugMark"); if((int64)(uintptr)n != n || n < 0) { - //runtime_printf("debug_scanblock %p %D\n", b, n); + runtime_printf("debug_scanblock %p %D\n", b, n); runtime_throw("debug_scanblock"); } @@ -1047,14 +1047,13 @@ runtime_gc(int32 force) mstats.pause_total_ns += t3 - t0; mstats.numgc++; if(mstats.debuggc) - runtime_printf("pause %llu\n", (unsigned long long)t3-t0); + runtime_printf("pause %D\n", t3-t0); if(gctrace) { - runtime_printf("gc%d(%d): %llu+%llu+%llu ms %llu -> %llu MB %llu -> %llu (%llu-%llu) objects %llu handoff\n", - mstats.numgc, work.nproc, (unsigned long long)(t1-t0)/1000000, (unsigned long long)(t2-t1)/1000000, (unsigned long long)(t3-t2)/1000000, - (unsigned long long)heap0>>20, (unsigned long long)heap1>>20, (unsigned long long)obj0, (unsigned long long)obj1, - (unsigned long long) mstats.nmalloc, (unsigned long long)mstats.nfree, - (unsigned long long) nhandoff); + runtime_printf("gc%d(%d): %D+%D+%D ms, %D -> %D MB %D -> %D (%D-%D) objects\n", + mstats.numgc, work.nproc, (t1-t0)/1000000, (t2-t1)/1000000, (t3-t2)/1000000, + heap0>>20, heap1>>20, obj0, obj1, + mstats.nmalloc, mstats.nfree); } runtime_MProf_GC(); @@ -1152,8 +1151,8 @@ runtime_markallocated(void *v, uintptr n { uintptr *b, obits, bits, off, shift; - // if(0) - // runtime_printf("markallocated %p+%p\n", v, n); + if(0) + runtime_printf("markallocated %p+%p\n", v, n); if((byte*)v+n > (byte*)runtime_mheap.arena_used || (byte*)v < runtime_mheap.arena_start) runtime_throw("markallocated: bad pointer"); @@ -1184,8 +1183,8 @@ runtime_markfreed(void *v, uintptr n) { uintptr *b, obits, bits, off, shift; - // if(0) - // runtime_printf("markallocated %p+%p\n", v, n); + if(0) + runtime_printf("markallocated %p+%p\n", v, n); if((byte*)v+n > (byte*)runtime_mheap.arena_used || (byte*)v < runtime_mheap.arena_start) runtime_throw("markallocated: bad pointer"); @@ -1227,7 +1226,7 @@ runtime_checkfreed(void *v, uintptr n) bits = *b>>shift; if((bits & bitAllocated) != 0) { runtime_printf("checkfreed %p+%p: off=%p have=%p\n", - v, (void*)n, (void*)off, (void*)(bits & bitMask)); + v, n, off, bits & bitMask); runtime_throw("checkfreed: not freed"); } } Index: libgo/runtime/mheap.c =================================================================== --- libgo/runtime/mheap.c (revision 187847) +++ libgo/runtime/mheap.c (revision 187848) @@ -191,7 +191,7 @@ MHeap_Grow(MHeap *h, uintptr npage) v = runtime_MHeap_SysAlloc(h, ask); } if(v == nil) { - runtime_printf("runtime: out of memory: cannot allocate %llu-byte block (%llu in use)\n", (unsigned long long)ask, (unsigned long long)mstats.heap_sys); + runtime_printf("runtime: out of memory: cannot allocate %D-byte block (%D in use)\n", (uint64)ask, mstats.heap_sys); return false; } } @@ -277,7 +277,7 @@ MHeap_FreeLocked(MHeap *h, MSpan *s) PageID p; if(s->state != MSpanInUse || s->ref != 0) { - // runtime_printf("MHeap_FreeLocked - span %p ptr %p state %d ref %d\n", s, s->start<<PageShift, s->state, s->ref); + runtime_printf("MHeap_FreeLocked - span %p ptr %p state %d ref %d\n", s, s->start<<PageShift, s->state, s->ref); runtime_throw("MHeap_FreeLocked - invalid free"); } mstats.heap_idle += s->npages<<PageShift; @@ -397,10 +397,10 @@ runtime_MHeap_Scavenger(void* dummy) if(trace) { if(sumreleased > 0) - runtime_printf("scvg%d: %p MB released\n", k, (void*)(sumreleased>>20)); - runtime_printf("scvg%d: inuse: %lld, idle: %lld, sys: %lld, released: %lld, consumed: %lld (MB)\n", - k, (long long)(mstats.heap_inuse>>20), (long long)(mstats.heap_idle>>20), (long long)(mstats.heap_sys>>20), - (long long)(mstats.heap_released>>20), (long long)((mstats.heap_sys - mstats.heap_released)>>20)); + runtime_printf("scvg%d: %p MB released\n", k, sumreleased>>20); + runtime_printf("scvg%d: inuse: %D, idle: %D, sys: %D, released: %D, consumed: %D (MB)\n", + k, mstats.heap_inuse>>20, mstats.heap_idle>>20, mstats.heap_sys>>20, + mstats.heap_released>>20, (mstats.heap_sys - mstats.heap_released)>>20); } } } @@ -451,7 +451,7 @@ void runtime_MSpanList_Insert(MSpan *list, MSpan *span) { if(span->next != nil || span->prev != nil) { - // runtime_printf("failed MSpanList_Insert %p %p %p\n", span, span->next, span->prev); + runtime_printf("failed MSpanList_Insert %p %p %p\n", span, span->next, span->prev); runtime_throw("MSpanList_Insert"); } span->next = list->next; Index: libgo/merge.sh =================================================================== --- libgo/merge.sh (revision 187847) +++ libgo/merge.sh (revision 187848) @@ -163,7 +163,7 @@ done done done -runtime="chan.c cpuprof.c lock_futex.c lock_sema.c mcache.c mcentral.c mfinal.c mfixalloc.c mgc0.c mheap.c msize.c proc.c runtime.c runtime.h signal_unix.c malloc.h malloc.goc mprof.goc runtime1.goc sema.goc sigqueue.goc string.goc time.goc" +runtime="chan.c cpuprof.c lock_futex.c lock_sema.c mcache.c mcentral.c mfinal.c mfixalloc.c mgc0.c mheap.c msize.c print.c proc.c runtime.c runtime.h signal_unix.c malloc.h malloc.goc mprof.goc runtime1.goc sema.goc sigqueue.goc string.goc time.goc" for f in $runtime; do merge_c $f $f done Index: libgo/Makefile.am =================================================================== --- libgo/Makefile.am (revision 187847) +++ libgo/Makefile.am (revision 187848) @@ -465,6 +465,7 @@ runtime_files = \ runtime/mgc0.c \ runtime/mheap.c \ runtime/msize.c \ + runtime/print.c \ runtime/proc.c \ runtime/runtime.c \ runtime/signal_unix.c \