Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update README.md to use --strace #70

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,10 @@ import (
"fmt"
"testing"

"github.com/xhd2015/xgo/runtime/trace"
_ "github.com/xhd2015/xgo/runtime/trace"
)

func TestTrace(t *testing.T) {
// begin trace
finish := trace.Begin()
defer finish()
A()
B()
C()
Expand All @@ -385,7 +382,8 @@ Run with `xgo`:
```sh
# run the test
# this will write the trace into TestTrace.json
xgo test ./
# --strace represents stack trace
xgo test --strace ./

# view the trace
xgo tool trace TestTrace.json
Expand All @@ -407,6 +405,20 @@ By default, Trace will write traces to a temp directory under current working di
- `XGO_TRACE_OUTPUT=<dir>`: traces will be written to `<dir>`,
- `XGO_TRACE_OUTPUT=off`: turn off trace.

Besides the `--strace` flag, xgo allows you to define which span should be collected, using `trace.Begin()`:
```go
import "github.com/xhd2015/xgo/runtime/trace"

func TestTrace(t *testing.T) {
A()
finish := trace.Begin()
defer finish()
B()
C()
}
```
The trace will only include `B()` and `C()`

# Concurrent safety
I know you guys from other monkey patching library suffer from the unsafety implied by these frameworks.

Expand Down
19 changes: 15 additions & 4 deletions README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,10 @@ import (
"fmt"
"testing"

"github.com/xhd2015/xgo/runtime/trace"
_ "github.com/xhd2015/xgo/runtime/trace"
)

func TestTrace(t *testing.T) {
// begin trace
finish := trace.Begin()
defer finish()
A()
B()
C()
Expand Down Expand Up @@ -397,6 +394,20 @@ xgo tool trace TestTrace.json
- `XGO_TRACE_OUTPUT=<dir>`: 堆栈记录被写入到`<dir>`目录下,
- `XGO_TRACE_OUTPUT=off`: 关闭堆栈记录收集。

除了使用`--strace`之外, xgo还允许你通过`trace.Begin()`的方式手动控制追踪范围:
```go
import "github.com/xhd2015/xgo/runtime/trace"

func TestTrace(t *testing.T) {
A()
finish := trace.Begin()
defer finish()
B()
C()
}
```
结果中只会包含`B()`和`C()`.

# 并发安全
我知道大部分人认为Monkey Patching不是并发安全的,但那是现有的库的实现方式决定的。

Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.25"
const REVISION = "6eeff524a454a6032a2294b47d1dc1c892b5545f+1"
const NUMBER = 186
const REVISION = "ae8695c56e8c7f1976d409c7fba953041983c299+1"
const NUMBER = 187

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

const VERSION = "1.0.25"
const REVISION = "6eeff524a454a6032a2294b47d1dc1c892b5545f+1"
const NUMBER = 186
const REVISION = "ae8695c56e8c7f1976d409c7fba953041983c299+1"
const NUMBER = 187

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
18 changes: 18 additions & 0 deletions runtime/trap/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ type interceptorManager struct {
funcMapping map[*core.FuncInfo][]*Interceptor // nested mapping
}

func (c *interceptorManager) hasAny() bool {
if c == nil {
return false
}
if len(c.head) > 0 {
return true
}
if len(c.tail) > 0 {
return true
}
for _, m := range c.funcMapping {
if len(m) > 0 {
return true
}
}
return false
}

func (c *interceptorManager) copy() *interceptorManager {
if c == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion runtime/trap/trap.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
return
}
local := getLocalInterceptorList()
if local == nil || (len(local.head) == 0 && len(local.tail) == 0) {
if !local.hasAny() {
return
}
// inherit interceptors of last group
Expand Down
Loading