Skip to content

Commit

Permalink
make trace interceptor the highest priority
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 10, 2024
1 parent f34bf4c commit f174b18
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 103 deletions.
6 changes: 3 additions & 3 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import "fmt"

const VERSION = "1.0.21"
const REVISION = "60d0e314753d95e7d630a307ef991ac7bc21807e+1"
const NUMBER = 173
const VERSION = "1.0.22"
const REVISION = "f34bf4ca38af8b5adcf36b67de5ea6fb853e4823+1"
const NUMBER = 174

func getRevision() string {
revSuffix := ""
Expand Down
8 changes: 4 additions & 4 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
)

const VERSION = "1.0.21"
const REVISION = "60d0e314753d95e7d630a307ef991ac7bc21807e+1"
const NUMBER = 173
const VERSION = "1.0.22"
const REVISION = "f34bf4ca38af8b5adcf36b67de5ea6fb853e4823+1"
const NUMBER = 174

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand All @@ -24,7 +24,7 @@ func init() {
}
err := checkVersion()
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: xgo toolchain: %v\nnote: this message can be turned off by setting %s=true\n", err, XGO_CHECK_TOOLCHAIN_VERSION)
fmt.Fprintf(os.Stderr, "WARNING: xgo toolchain: %v\nnote: this message can be turned off by setting %s=false\n", err, XGO_CHECK_TOOLCHAIN_VERSION)
}
}

Expand Down
29 changes: 0 additions & 29 deletions runtime/test/trace/main.go

This file was deleted.

61 changes: 61 additions & 0 deletions runtime/test/trace/trace_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package trace

import (
"testing"

"github.com/xhd2015/xgo/runtime/mock"
"github.com/xhd2015/xgo/runtime/test/util"
"github.com/xhd2015/xgo/runtime/trace"
)

func TestMockedFuncShouldShowInTrace(t *testing.T) {
var root *trace.Root
trace.Options().OnComplete(func(r *trace.Root) {
root = r
}).Collect(func() {
mock.Patch(A, func() string {
return "mock_A"
})
h()
})
data, err := trace.MarshalAnyJSON(root.Export())
if err != nil {
t.Fatal(err)
}

// t.Logf("trace: %s", data)

expectTraceSequence := []string{
"{",
`"Name":"h",`,
`"Name":"A",`,
`mock_A`,
`"Name":"B",`,
`"Name":"C",`,
`"Name":"C",`,
"}",
}
err = util.CheckSequence(string(data), expectTraceSequence)
if err != nil {
t.Fatalf("%v", err)
}
}

func h() {
A()
B()
C()
}

func A() string {
return "A"
}

func B() string {
C()
return "B"
}

func C() string {
return "C"
}
35 changes: 2 additions & 33 deletions runtime/test/trace_panic_peek/trace_panic_peek_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"fmt"
"io"
"strings"
"testing"

"github.com/xhd2015/xgo/runtime/test/util"
"github.com/xhd2015/xgo/runtime/trace"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func TestTracePanicPeek(t *testing.T) {
`"Error":"Work panic: doWork panic",`,
"}",
}
err := CheckSequence(string(traceData), expectTraceSequence)
err := util.CheckSequence(string(traceData), expectTraceSequence)
if err != nil {
t.Fatalf("%v", err)
}
Expand Down Expand Up @@ -74,34 +74,3 @@ func doWork(w io.Writer) {
fmt.Fprintf(w, "call: doWork\n")
panic("doWork panic")
}

func indexSequence(s string, sequence []string, begin bool) (int, int) {
if len(sequence) == 0 {
return 0, 0
}
firstIdx := -1
base := 0
for i, seq := range sequence {
idx := strings.Index(s, seq)
if idx < 0 {
return i, -1
}
if firstIdx < 0 {
firstIdx = idx
}
s = s[idx+len(seq):]
base += idx + len(seq)
}
if begin {
return -1, firstIdx
}
return -1, base
}

func CheckSequence(output string, sequence []string) error {
missing, idx := indexSequence(output, sequence, false)
if idx < 0 {
return fmt.Errorf("sequence at %d: missing %q", missing, sequence[missing])
}
return nil
}
37 changes: 37 additions & 0 deletions runtime/test/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package util

import (
"fmt"
"strings"
)

func indexSequence(s string, sequence []string, begin bool) (int, int) {
if len(sequence) == 0 {
return 0, 0
}
firstIdx := -1
base := 0
for i, seq := range sequence {
idx := strings.Index(s, seq)
if idx < 0 {
return i, -1
}
if firstIdx < 0 {
firstIdx = idx
}
s = s[idx+len(seq):]
base += idx + len(seq)
}
if begin {
return -1, firstIdx
}
return -1, base
}

func CheckSequence(output string, sequence []string) error {
missing, idx := indexSequence(output, sequence, false)
if idx < 0 {
return fmt.Errorf("sequence at %d: missing %q", missing, sequence[missing])
}
return nil
}
2 changes: 1 addition & 1 deletion runtime/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func setupInterceptor() {
return
}
// collect trace
trap.AddInterceptor(&trap.Interceptor{
trap.AddInterceptorHead(&trap.Interceptor{
Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) {
key := uintptr(__xgo_link_getcurg())
localOptStack, ok := collectingMap.Load(key)
Expand Down
Loading

0 comments on commit f174b18

Please sign in to comment.