/
helper.go
97 lines (93 loc) · 2.9 KB
/
helper.go
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
package sut
import (
"github.com/v2pro/plz/countlog"
"bytes"
)
var helperThreadShutdown = "to-koala!thread-shutdown"
var helperCallFunction = "to-koala!call-function"
var helperReturnFunction = "to-koala!return-function"
var helperReadStorage = "to-koala!read-storage"
var helperSetDelegatedFromThreadId = "to-koala!set-delegated-from-thread-id"
var helperGetTraceHeader = "to-koala!get-trace-header"
var helperGetTraceHeaderKey = "to-koala!get-trace-header-key"
var helperSetTraceHeaderKey = "to-koala!set-trace-header-key"
func SendToKoala(threadID ThreadID, span []byte, flags SendToFlags) {
helperInfo := span
countlog.Trace("event!sut.send_to_koala",
"threadID", threadID,
"flags", flags,
"content", helperInfo)
newlinePos := bytes.IndexByte(helperInfo, '\n')
if newlinePos == -1 {
return
}
body := helperInfo[newlinePos+1:]
switch string(helperInfo[:newlinePos]) {
case helperThreadShutdown:
if flags != 0 {
operateVirtualThread(ThreadID(flags), func(thread *Thread) {
thread.OnShutdown()
})
} else {
OperateThread(threadID, func(thread *Thread) {
thread.OnShutdown()
})
}
case helperCallFunction:
OperateThread(threadID, func(thread *Thread) {
thread.replayingSession.CallFunction(thread, body)
})
case helperReturnFunction:
OperateThread(threadID, func(thread *Thread) {
thread.replayingSession.ReturnFunction(thread, body)
})
case helperReadStorage:
OperateThread(threadID, func(thread *Thread) {
thread.recordingSession.ReadStorage(thread, body)
})
case helperSetDelegatedFromThreadId:
realThreadId := threadID
virtualThreadId := ThreadID(flags)
mapThreadRelation(realThreadId, virtualThreadId)
case helperGetTraceHeader:
OperateThread(threadID, func(thread *Thread) {
if thread.recordingSession != nil {
thread.helperResponse = thread.recordingSession.GetTraceHeader()
}
})
case helperGetTraceHeaderKey:
OperateThread(threadID, func(thread *Thread) {
if thread.recordingSession != nil {
key := body
thread.helperResponse = thread.recordingSession.GetTraceHeader().Get(key)
}
})
case helperSetTraceHeaderKey:
OperateThread(threadID, func(thread *Thread) {
if thread.recordingSession != nil {
newlinePos = bytes.IndexByte(body, '\n')
if newlinePos == -1 {
countlog.Error("event!sut.SetTraceHeaderKey expects newline as separator",
"body", body)
return
}
key := body[:newlinePos]
value := body[newlinePos+1:]
thread.recordingSession.TraceHeader = thread.recordingSession.GetTraceHeader().Set(key, value)
}
})
default:
countlog.Debug("event!sut.unknown_helper",
"threadID", threadID,
"helperType", string(helperInfo[:newlinePos]))
}
}
func RecvFromKoala(threadID ThreadID) []byte {
thread := getThread(threadID)
response := thread.helperResponse
thread.helperResponse = nil
countlog.Trace("event!sut.recv_from_koala",
"threadID", threadID,
"response", response)
return response
}