Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit af6317f

Browse files
authored
Merge pull request #36 from grantseltzer/debug-log-improvements
Add color to debug printing, and add log for loading all eBPF programs
2 parents a477fd2 + c5fec7c commit af6317f

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

cmd/weaver/functions_file.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ func read_functions_file(path string) ([]functionTraceContext, error) {
2222
continue
2323
}
2424

25-
if globalDebug {
26-
fmt.Println("parsing: " + funcString)
27-
}
25+
debugLog("parsing: %s\n", funcString)
26+
2827
err := parseFunctionAndArgumentTypes(&contexts[i], funcString)
2928
if err != nil {
3029
return nil, fmt.Errorf("could not parse function string '%s': %s", funcString, err.Error())

cmd/weaver/load_uprobe.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"math"
1010
"strings"
11+
"sync"
1112
"text/template"
1213

1314
bpf "github.com/iovisor/gobpf/bcc"
@@ -104,18 +105,18 @@ func bpfText(context *functionTraceContext) string {
104105
buf := new(bytes.Buffer)
105106
t.Execute(buf, context)
106107

107-
if globalDebug {
108-
// Print eBPF text
109-
fmt.Println(buf.String())
110-
}
108+
// Print eBPF text
109+
debugLog("%s\n", buf.String())
111110

112111
return buf.String()
113112
}
114113

115114
// loadUprobeAndBPFModule will, based on the passed context, install the bpf program and attach a uprobe to the specified function
116115
// It then prints results to the designated output stream.
117116
// This blocks until Ctrl-C or error occurs.
118-
func loadUprobeAndBPFModule(traceContext *functionTraceContext, runtimeContext context.Context) error {
117+
func loadUprobeAndBPFModule(traceContext *functionTraceContext, runtimeContext context.Context, wg *sync.WaitGroup) error {
118+
119+
defer runtimeContext.Err()
119120

120121
// Load eBPF filter and uprobe
121122
filterText := bpfText(traceContext)
@@ -203,6 +204,7 @@ func loadUprobeAndBPFModule(traceContext *functionTraceContext, runtimeContext c
203204
}
204205
}()
205206

207+
wg.Done()
206208
perfMap.Start()
207209
<-runtimeContext.Done()
208210
perfMap.Stop()

cmd/weaver/main.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/signal"
99
"path/filepath"
10+
"sync"
1011

1112
"github.com/urfave/cli/v2"
1213
)
@@ -109,15 +110,22 @@ func entry(c *cli.Context) error {
109110
runtimeContext, cancel := context.WithCancel(context.Background())
110111
defer cancel()
111112

113+
// wg is used to communicate back with the main thread when
114+
// all uprobe/eBPFs are installed
115+
var wg sync.WaitGroup
116+
wg.Add(len(contexts))
117+
112118
// Install eBPF program for each function to trace
113119
for i := range contexts {
114-
115120
contexts[i].binaryName = binaryFullPath
116-
117-
// Load uprobe and BPF code. This will block until Ctrl-C or an error occurs.
118-
go loadUprobeAndBPFModule(&contexts[i], runtimeContext)
121+
go loadUprobeAndBPFModule(&contexts[i], runtimeContext, &wg)
119122
}
120123

124+
go func() {
125+
wg.Wait()
126+
debugLog("All probes installed\n")
127+
}()
128+
121129
<-sig
122130

123131
return nil

cmd/weaver/output.go

+6
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ func printOutput(o output) error {
4949

5050
return nil
5151
}
52+
53+
func debugLog(format string, a ...interface{}) {
54+
if globalDebug {
55+
fmt.Fprintf(os.Stderr, "\x1b[96m"+format+"\x1b[0m", a...)
56+
}
57+
}

0 commit comments

Comments
 (0)