LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2014 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 |
| 5 // Issue 7978. Stack tracing didn't work during cgo code after calling a Go |
| 6 // callback. Make sure GC works and the stack trace is correct. |
| 7 |
| 8 package cgotest |
| 9 |
| 10 /* |
| 11 #include <stdint.h> |
| 12 |
| 13 void issue7978cb(void); |
| 14 |
| 15 // use ugly atomic variable sync since that doesn't require calling back into |
| 16 // Go code or OS dependencies |
| 17 static void issue7978c(uint32_t *sync) { |
| 18 while(__sync_fetch_and_add(sync, 0) != 0) |
| 19 ; |
| 20 __sync_fetch_and_add(sync, 1); |
| 21 while(__sync_fetch_and_add(sync, 0) != 2) |
| 22 ; |
| 23 issue7978cb(); |
| 24 __sync_fetch_and_add(sync, 1); |
| 25 while(__sync_fetch_and_add(sync, 0) != 6) |
| 26 ; |
| 27 } |
| 28 */ |
| 29 import "C" |
| 30 |
| 31 import ( |
| 32 "runtime" |
| 33 "strings" |
| 34 "sync/atomic" |
| 35 "testing" |
| 36 ) |
| 37 |
| 38 var issue7978sync uint32 |
| 39 |
| 40 func issue7978check(t *testing.T, wantFunc string, badFunc string, depth int) { |
| 41 runtime.GC() |
| 42 buf := make([]byte, 65536) |
| 43 trace := string(buf[:runtime.Stack(buf, true)]) |
| 44 for _, goroutine := range strings.Split(trace, "\n\n") { |
| 45 if strings.Contains(goroutine, "test.issue7978go") { |
| 46 trace := strings.Split(goroutine, "\n") |
| 47 // look for the expected function in the stack |
| 48 for i := 0; i < depth; i++ { |
| 49 if badFunc != "" && strings.Contains(trace[1+2*i
], badFunc) { |
| 50 t.Errorf("bad stack: found %s in the sta
ck:\n%s", badFunc, goroutine) |
| 51 return |
| 52 } |
| 53 if strings.Contains(trace[1+2*i], wantFunc) { |
| 54 return |
| 55 } |
| 56 } |
| 57 t.Errorf("bad stack: didn't find %s in the stack:\n%s",
wantFunc, goroutine) |
| 58 return |
| 59 } |
| 60 } |
| 61 t.Errorf("bad stack: goroutine not found. Full stack dump:\n%s", trace) |
| 62 } |
| 63 |
| 64 func issue7978wait(store uint32, wait uint32) { |
| 65 if store != 0 { |
| 66 atomic.StoreUint32(&issue7978sync, store) |
| 67 } |
| 68 for atomic.LoadUint32(&issue7978sync) != wait { |
| 69 runtime.Gosched() |
| 70 } |
| 71 } |
| 72 |
| 73 //export issue7978cb |
| 74 func issue7978cb() { |
| 75 issue7978wait(3, 4) |
| 76 } |
| 77 |
| 78 func issue7978go() { |
| 79 C.issue7978c((*C.uint32_t)(&issue7978sync)) |
| 80 issue7978wait(7, 8) |
| 81 } |
| 82 |
| 83 func test7978(t *testing.T) { |
| 84 issue7978sync = 0 |
| 85 go issue7978go() |
| 86 // test in c code, before callback |
| 87 issue7978wait(0, 1) |
| 88 issue7978check(t, "runtime.cgocall_errno(", "", 1) |
| 89 // test in go code, during callback |
| 90 issue7978wait(2, 3) |
| 91 issue7978check(t, "test.issue7978cb(", "test.issue7978go", 3) |
| 92 // test in c code, after callback |
| 93 issue7978wait(4, 5) |
| 94 issue7978check(t, "runtime.cgocall_errno(", "runtime.cgocallback", 1) |
| 95 // test in go code, after return from cgo |
| 96 issue7978wait(6, 7) |
| 97 issue7978check(t, "test.issue7978go(", "", 3) |
| 98 atomic.StoreUint32(&issue7978sync, 8) |
| 99 } |
LEFT | RIGHT |