1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The __attribute__((weak)) used below doesn't seem to work on Windows.
package main
// Test the context argument to SetCgoTraceback.
// Use fake context, traceback, and symbolizer functions.
/*
// Defined in tracebackctxt_c.c.
extern void C1(void);
extern void C2(void);
extern void tcContext(void*);
extern void tcTraceback(void*);
extern void tcSymbolizer(void*);
extern int getContextCount(void);
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
func init() {
register("TracebackContext", TracebackContext)
}
var tracebackOK bool
func TracebackContext() {
runtime.SetCgoTraceback(0, unsafe.Pointer(C.tcTraceback), unsafe.Pointer(C.tcContext), unsafe.Pointer(C.tcSymbolizer))
C.C1()
if got := C.getContextCount(); got != 0 {
fmt.Printf("at end contextCount == %d, expected 0\n", got)
tracebackOK = false
}
if tracebackOK {
fmt.Println("OK")
}
}
//export G1
func G1() {
C.C2()
}
//export G2
func G2() {
pc := make([]uintptr, 32)
n := runtime.Callers(0, pc)
cf := runtime.CallersFrames(pc[:n])
var frames []runtime.Frame
for {
frame, more := cf.Next()
frames = append(frames, frame)
if !more {
break
}
}
want := []struct {
function string
line int
}{
{"main.G2", 0},
{"cFunction", 0x10200},
{"cFunction", 0x200},
{"cFunction", 0x10201},
{"cFunction", 0x201},
{"main.G1", 0},
{"cFunction", 0x10100},
{"cFunction", 0x100},
{"main.TracebackContext", 0},
}
ok := true
i := 0
wantLoop:
for _, w := range want {
for ; i < len(frames); i++ {
if w.function == frames[i].Function {
if w.line != 0 && w.line != frames[i].Line {
fmt.Printf("found function %s at wrong line %#x (expected %#x)\n", w.function, frames[i].Line, w.line)
ok = false
}
i++
continue wantLoop
}
}
fmt.Printf("did not find function %s in\n", w.function)
for _, f := range frames {
fmt.Println(f)
}
ok = false
break
}
tracebackOK = ok
if got := C.getContextCount(); got != 2 {
fmt.Printf("at bottom contextCount == %d, expected 2\n", got)
tracebackOK = false
}
}
|