Skip to content

Commit

Permalink
add help and lift noflie rlimit
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jul 27, 2022
1 parent 66e3cc3 commit b5a6b3f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
11 changes: 9 additions & 2 deletions internal/bpf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (b *BPF) Load(uprobes []uprobe.Uprobe) (err error) {
suffix := fmt.Sprintf("_%x", up.Offset)
progName := progPrefix + suffix
structDefine.AddField(fieldPrefix+suffix, &ebpf.Program{}, fmt.Sprintf(`ebpf:"%s"`, progName))
spec.Programs[progName] = spec.Programs["entpoint"].Copy()
spec.Programs[progName] = spec.Programs[progPrefix].Copy()
instructions := []asm.Instruction{}
eventOffset := EventDataOffset
for _, args := range up.FetchArgs {
Expand All @@ -67,7 +67,14 @@ func (b *BPF) Load(uprobes []uprobe.Uprobe) (err error) {
}

spec.Programs[progName].Instructions = append(spec.Programs[progName].Instructions[:BpfInsertIndex], append(instructions, spec.Programs[progName].Instructions[BpfInsertIndex:]...)...)
spec.Programs[progName].Instructions[13].Offset += int16(len(instructions))

for i, ins := range spec.Programs[progName].Instructions {
if ins.OpCode == 21 { // goto
if i < BpfInsertIndex {
spec.Programs[progName].Instructions[i].Offset += int16(len(instructions))
}
}
}
}
b.objs = structDefine.Build().New()

Expand Down
2 changes: 1 addition & 1 deletion internal/eventmanager/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (p *EventManager) Add(event bpf.UfuncgraphEvent) {
if length == 0 && event.Location == 1 {
return
}
if length > 0 && event.Location == 0 && p.goroutine2events[event.StackId][length-1].Location == 0 && p.goroutine2events[event.StackId][length-1].StackDepth == event.StackDepth {
if length > 0 && event.Location == 0 && p.goroutine2events[event.StackId][length-1].Location == 0 && p.goroutine2events[event.StackId][length-1].StackDepth == event.StackDepth && p.goroutine2events[event.StackId][length-1].Ip == event.Ip {
return
}
p.goroutine2events[event.StackId] = append(p.goroutine2events[event.StackId], event)
Expand Down
5 changes: 2 additions & 3 deletions internal/uprobe/functree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/jschwinger233/ufuncgraph/elf"
"github.com/jschwinger233/ufuncgraph/utils"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ func parseFuncTrees(elf *elf.ELF, wildcards, exWildcards []string, searchDepth i
for _, symbol := range symbols {
if debugelf.ST_TYPE(symbol.Info) == debugelf.STT_FUNC {
for _, wc := range wildcards {
if utils.MatchWildcard(wc, symbol.Name) {
if MatchWildcard(wc, symbol.Name) {
funcnames = append(funcnames, symbol.Name)
break
}
Expand All @@ -83,7 +82,7 @@ func parseFuncTrees(elf *elf.ELF, wildcards, exWildcards []string, searchDepth i
return
}
for _, wc := range ex {
if utils.MatchWildcard(wc, name) {
if MatchWildcard(wc, name) {
tree.Err = fmt.Errorf("excluded by %s", wc)
break
}
Expand Down
37 changes: 33 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,47 @@ package main

import (
"os"
"syscall"

"github.com/cilium/ebpf/rlimit"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/sys/unix"
)

func init() {
// TODO: lift fileno rlimit
if err := rlimit.RemoveMemlock(); err != nil {
rlimit := syscall.Rlimit{
Cur: unix.RLIM_INFINITY,
Max: unix.RLIM_INFINITY,
}
if err := syscall.Setrlimit(unix.RLIMIT_MEMLOCK, &rlimit); err != nil {
log.Fatal(err)
}
rlimit = syscall.Rlimit{
Cur: 1048576,
Max: 1048576,
}
if err := syscall.Setrlimit(unix.RLIMIT_NOFILE, &rlimit); err != nil {
log.Fatal(err)
}
}

func main() {
app := &cli.App{
Name: "utrace",
Name: "ufuncgraph",
// TODO@zc: kernel version
Usage: "bpf(2)-based ftrace(1)-like function graph tracer for userspace! \n(only Golang in x86_64 little-endian Linux is supported for now)",
UsageText: `example: trace a specific function in etcd client "go.etcd.io/etcd/client/v3/concurrency.(*Mutex).tryAcquire"
ufuncgraph ./bin 'go.etcd.io/etcd/client/v3/concurrency.(*Mutex).tryAcquire'
example: trace all functions in etcd client
ufuncgraph ./bin 'go.etcd.io/etcd/client/v3/*'
example: trace a specific function and its downstream functions within 3 layers, but exclude the golang builtins
ufuncgraph --depth 3 ./bin 'go.etcd.io/etcd/client/v3/concurrency.(*Mutex).tryAcquire' '!runtime.*'
example: trace a specific function with some arguemnts and backtrace
ufuncgraph --backtrace ./bin 'go.etcd.io/etcd/client/v3/concurrency.(*Mutex).tryAcquire(pfx=+0(+8(%rax)):c128, n_pfx=+16(%ax):u64, myKey=+0(+24(%rax)):c128)'
`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "backtrace",
Expand Down Expand Up @@ -51,6 +76,10 @@ func main() {
bin := ctx.Args().First()
args := ctx.Args().Tail()

if bin == "" || ctx.Bool("help") {
return cli.ShowAppHelp(ctx)
}

tracer, err := NewTracer(bin, args, backtrace, depth)
if err != nil {
return
Expand Down

0 comments on commit b5a6b3f

Please sign in to comment.