Wire up recording of traces.

This commit is contained in:
Tom Wilkie
2015-09-17 04:20:31 +00:00
committed by Tom Wilkie
parent a3a0907ffc
commit b6e43c8f3f
4 changed files with 20 additions and 8 deletions

View File

@@ -37,9 +37,10 @@ func main() {
log.Fatalf("Could start docker watcher: %v", err)
}
store := newStore()
tracer := tracer{
ptrace: ptrace.NewPTracer(),
store: newStore(),
store: store,
ptrace: ptrace.NewPTracer(store),
docker: dockerRegistry,
}
defer tracer.Stop()

View File

@@ -3,6 +3,7 @@ package main
import (
"math/rand"
"sync"
"log"
"github.com/msackman/skiplist"
@@ -65,12 +66,15 @@ func (s *store) RecordConnection(pid int, connection *ptrace.Fd) {
newTrace := &trace{pid: pid, root: connection}
newTraceKey := newKey(connection)
log.Printf("Recording trace: %+v", newTrace)
// First, see if this new conneciton is a child of an existing connection.
// This indicates we have a parent connection to attach to.
// If not, insert this connection.
if parentNode := s.traces.Get(newTraceKey); parentNode != nil {
parentNode.Remove()
parentTrace := parentNode.Value.(*trace)
log.Printf(" Found parent trace: %+v", parentTrace)
parentTrace.children = append(parentTrace.children, newTrace)
} else {
s.traces.Insert(newTraceKey, newTrace)
@@ -84,6 +88,7 @@ func (s *store) RecordConnection(pid int, connection *ptrace.Fd) {
if childNode := s.traces.Get(childTraceKey); childNode != nil {
childNode.Remove()
childTrace := childNode.Value.(*trace)
log.Printf(" Found child trace: %+v", childTrace)
newTrace.children = append(newTrace.children, childTrace)
} else {
s.traces.Insert(childTraceKey, newTrace)

View File

@@ -16,6 +16,10 @@ const (
ptraceTracesysgoodBit = 0x80
)
type Store interface {
RecordConnection(int, *Fd)
}
// PTracer ptrace processed and threads
type PTracer struct {
@@ -29,6 +33,7 @@ type PTracer struct {
threads map[int]*thread
processes map[int]*process
store Store
}
type stopped struct {
@@ -37,7 +42,7 @@ type stopped struct {
}
// NewPTracer creates a new ptracer.
func NewPTracer() PTracer {
func NewPTracer(store Store) PTracer {
t := PTracer{
ops: make(chan func()),
stopped: make(chan stopped),
@@ -46,6 +51,7 @@ func NewPTracer() PTracer {
threads: make(map[int]*thread),
processes: make(map[int]*process),
store: store,
}
go t.waitLoop()
go t.loop()
@@ -135,6 +141,7 @@ func (t *PTracer) traceThread(pid int, process *process) *thread {
result := make(chan *thread)
t.ops <- func() {
thread := newThread(pid, process, t)
t.threads[pid] = thread
err := syscall.PtraceAttach(pid)
if err != nil {
@@ -142,7 +149,6 @@ func (t *PTracer) traceThread(pid int, process *process) *thread {
return
}
t.threads[pid] = thread
result <- thread
select {

View File

@@ -43,7 +43,7 @@ type thread struct {
callRegs syscall.PtraceRegs
resultRegs syscall.PtraceRegs
currentConnection *Fd
currentConnection *Fd // current incoming connection
}
func newThread(pid int, process *process, tracer *PTracer) *thread {
@@ -213,9 +213,9 @@ func (t *thread) handleClose(call, result *syscall.PtraceRegs) {
}
// if this connection was incoming, add it to 'the registry'
//if fd.direction == incoming {
// t.tracer.store.RecordConnection(t.process.pid, fd)
//}
if fd.direction == incoming {
t.tracer.store.RecordConnection(t.process.pid, fd)
}
}
func (t *thread) handleIO(call, result *syscall.PtraceRegs) {