mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-02 17:50:39 +00:00
It works again!
This commit is contained in:
@@ -8,17 +8,13 @@
|
||||
/* this function is run by the thread */
|
||||
void *thread_func(void *sock) {
|
||||
struct sockaddr_in dest;
|
||||
char buffer[1024];
|
||||
char in_buffer[1024];
|
||||
char out_buffer[1024];
|
||||
int sockfd = (int)sock;
|
||||
int clientfd;
|
||||
|
||||
printf("I'm thread %d\n", syscall(SYS_gettid));
|
||||
|
||||
if (read(sockfd, buffer, sizeof(buffer)) < 0) {
|
||||
perror("ERROR reading from socket");
|
||||
return;
|
||||
}
|
||||
|
||||
clientfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (clientfd < 0) {
|
||||
perror("ERROR opening socket");
|
||||
@@ -37,10 +33,11 @@ void *thread_func(void *sock) {
|
||||
return;
|
||||
}
|
||||
|
||||
int readbytes = recv(clientfd, buffer, sizeof(buffer), 0);
|
||||
int readbytes = recv(clientfd, in_buffer, sizeof(in_buffer), 0);
|
||||
close(clientfd);
|
||||
|
||||
if (write(sockfd, buffer, read) < 0) {
|
||||
int writtenbytes = snprintf(out_buffer, sizeof(out_buffer), "{\"qotd\": %s}\n", in_buffer);
|
||||
if (write(sockfd, out_buffer, writtenbytes) < 0) {
|
||||
perror("ERROR writing to socket");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -142,28 +142,9 @@ func (t *PTracer) traceThread(pid int, process *process) *thread {
|
||||
return
|
||||
}
|
||||
|
||||
var status syscall.WaitStatus
|
||||
if _, err = syscall.Wait4(pid, &status, 0, nil); err != nil {
|
||||
log.Printf("Wait %d failed: %v", pid, err)
|
||||
return
|
||||
}
|
||||
|
||||
thread.attached = true
|
||||
|
||||
err = syscall.PtraceSetOptions(pid, ptraceOptions)
|
||||
if err != nil {
|
||||
log.Printf("SetOptions failed, pid=%d, err=%v", pid, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = syscall.PtraceSyscall(pid, 0)
|
||||
if err != nil {
|
||||
log.Printf("PtraceSyscall failed, pid=%d, err=%v", pid, err)
|
||||
return
|
||||
}
|
||||
|
||||
t.threads[pid] = thread
|
||||
result <- thread
|
||||
|
||||
select {
|
||||
case t.childAttached <- struct{}{}:
|
||||
default:
|
||||
@@ -180,20 +161,18 @@ func (t *PTracer) waitLoop() {
|
||||
)
|
||||
|
||||
for {
|
||||
log.Printf("Waiting...")
|
||||
//log.Printf("Waiting...")
|
||||
pid, err = syscall.Wait4(-1, &status, syscall.WALL, nil)
|
||||
if err != nil && err.(syscall.Errno) == syscall.ECHILD {
|
||||
log.Printf( "No children to wait4")
|
||||
//log.Printf( "No children to wait4")
|
||||
<-t.childAttached
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf(" Wait failed: %v %d", err, err.(syscall.Errno))
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf(" PID %d stopped with signal %#x", pid, status)
|
||||
//log.Printf(" PID %d stopped with signal %#x", pid, status)
|
||||
t.stopped <- stopped{pid, status}
|
||||
}
|
||||
}
|
||||
@@ -221,9 +200,19 @@ func (t *PTracer) handleStopped(pid int, status syscall.WaitStatus) {
|
||||
return
|
||||
}
|
||||
|
||||
if status.Stopped() && status.StopSignal() == syscall.SIGTRAP|ptraceTracesysgoodBit {
|
||||
if !target.attached {
|
||||
target.attached = true
|
||||
|
||||
err = syscall.PtraceSetOptions(pid, ptraceOptions)
|
||||
if err != nil {
|
||||
log.Printf("SetOptions failed, pid=%d, err=%v", pid, err)
|
||||
return
|
||||
}
|
||||
|
||||
} else if status.Stopped() && status.StopSignal() == syscall.SIGTRAP|ptraceTracesysgoodBit {
|
||||
// pid entered Syscall-enter-stop or syscall-exit-stop
|
||||
target.syscallStopped()
|
||||
|
||||
} else if status.Stopped() && status.StopSignal() == syscall.SIGTRAP {
|
||||
// pid entered PTRACE_EVENT stop
|
||||
switch status.TrapCause() {
|
||||
@@ -254,7 +243,7 @@ func (t *PTracer) handleStopped(pid int, status syscall.WaitStatus) {
|
||||
}
|
||||
|
||||
// Restart stopped caller in syscall trap mode.
|
||||
log.Printf("Restarting pid %d with signal %d", pid, int(signal))
|
||||
// log.Printf("Restarting pid %d with signal %d", pid, int(signal))
|
||||
err = syscall.PtraceSyscall(pid, int(signal))
|
||||
if err != nil {
|
||||
log.Printf("PtraceSyscall failed, pid=%d, err=%v", pid, err)
|
||||
|
||||
Reference in New Issue
Block a user