| 1 |
// Copyright 2016 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 |
//go:build !gccgo |
| 6 |
// +build !gccgo |
| 7 |
|
| 8 |
package main |
| 9 |
|
| 10 |
// Test the context argument to SetCgoTraceback. |
| 11 |
// Use fake context, traceback, and symbolizer functions. |
| 12 |
|
| 13 |
/* |
| 14 |
// Defined in tracebackctxt_c.c. |
| 15 |
extern void C1(void); |
| 16 |
extern void C2(void); |
| 17 |
extern void tcContext(void*); |
| 18 |
extern void tcContextSimple(void*); |
| 19 |
extern void tcTraceback(void*); |
| 20 |
extern void tcSymbolizer(void*); |
| 21 |
extern int getContextCount(void); |
| 22 |
extern void TracebackContextPreemptionCallGo(int); |
| 23 |
*/ |
| 24 |
import "C" |
| 25 |
|
| 26 |
import ( |
| 27 |
"fmt" |
| 28 |
"runtime" |
| 29 |
"sync" |
| 30 |
"unsafe" |
| 31 |
) |
| 32 |
|
| 33 |
func init() { |
| 34 |
register("TracebackContext", TracebackContext) |
| 35 |
register("TracebackContextPreemption", TracebackContextPreemption) |
| 36 |
} |
| 37 |
|
| 38 |
var tracebackOK bool |
| 39 |
|
| 40 |
func TracebackContext() { |
| 41 |
runtime.SetCgoTraceback(0, unsafe.Pointer(C.tcTraceback), unsafe.Pointer(C.tcContext), unsafe.Pointer(C.tcSymbolizer)) |
| 42 |
C.C1() |
| 43 |
if got := C.getContextCount(); got != 0 { |
| 44 |
fmt.Printf("at end contextCount == %d, expected 0\n", got) |
| 45 |
tracebackOK = false |
| 46 |
} |
| 47 |
if tracebackOK { |
| 48 |
fmt.Println("OK") |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
//export G1 |
| 53 |
func G1() { |
| 54 |
C.C2() |
| 55 |
} |
| 56 |
|
| 57 |
//export G2 |
| 58 |
func G2() { |
| 59 |
pc := make([]uintptr, 32) |
| 60 |
n := runtime.Callers(0, pc) |
| 61 |
cf := runtime.CallersFrames(pc[:n]) |
| 62 |
var frames []runtime.Frame |
| 63 |
for { |
| 64 |
frame, more := cf.Next() |
| 65 |
frames = append(frames, frame) |
| 66 |
if !more { |
| 67 |
break |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
want := []struct { |
| 72 |
function string |
| 73 |
line int |
| 74 |
}{ |
| 75 |
{"main.G2", 0}, |
| 76 |
{"cFunction", 0x10200}, |
| 77 |
{"cFunction", 0x200}, |
| 78 |
{"cFunction", 0x10201}, |
| 79 |
{"cFunction", 0x201}, |
| 80 |
{"main.G1", 0}, |
| 81 |
{"cFunction", 0x10100}, |
| 82 |
{"cFunction", 0x100}, |
| 83 |
{"main.TracebackContext", 0}, |
| 84 |
} |
| 85 |
|
| 86 |
ok := true |
| 87 |
i := 0 |
| 88 |
wantLoop: |
| 89 |
for _, w := range want { |
| 90 |
for ; i < len(frames); i++ { |
| 91 |
if w.function == frames[i].Function { |
| 92 |
if w.line != 0 && w.line != frames[i].Line { |
| 93 |
fmt.Printf("found function %s at wrong line %#x (expected %#x)\n", w.function, frames[i].Line, w.line) |
| 94 |
ok = false |
| 95 |
} |
| 96 |
i++ |
| 97 |
continue wantLoop |
| 98 |
} |
| 99 |
} |
| 100 |
fmt.Printf("did not find function %s in\n", w.function) |
| 101 |
for _, f := range frames { |
| 102 |
fmt.Println(f) |
| 103 |
} |
| 104 |
ok = false |
| 105 |
break |
| 106 |
} |
| 107 |
tracebackOK = ok |
| 108 |
if got := C.getContextCount(); got != 2 { |
| 109 |
fmt.Printf("at bottom contextCount == %d, expected 2\n", got) |
| 110 |
tracebackOK = false |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// Issue 47441. |
| 115 |
func TracebackContextPreemption() { |
| 116 |
runtime.SetCgoTraceback(0, unsafe.Pointer(C.tcTraceback), unsafe.Pointer(C.tcContextSimple), unsafe.Pointer(C.tcSymbolizer)) |
| 117 |
|
| 118 |
const funcs = 10 |
| 119 |
const calls = 1e5 |
| 120 |
var wg sync.WaitGroup |
| 121 |
for i := 0; i < funcs; i++ { |
| 122 |
wg.Add(1) |
| 123 |
go func(i int) { |
| 124 |
defer wg.Done() |
| 125 |
for j := 0; j < calls; j++ { |
| 126 |
C.TracebackContextPreemptionCallGo(C.int(i*calls + j)) |
| 127 |
} |
| 128 |
}(i) |
| 129 |
} |
| 130 |
wg.Wait() |
| 131 |
|
| 132 |
fmt.Println("OK") |
| 133 |
} |
| 134 |
|
| 135 |
//export TracebackContextPreemptionGoFunction |
| 136 |
func TracebackContextPreemptionGoFunction(i C.int) { |
| 137 |
// Do some busy work. |
| 138 |
fmt.Sprintf("%d\n", i) |
| 139 |
} |