Skip to content

Commit

Permalink
Change threads
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-srhodes committed Oct 9, 2023
1 parent 745b8a2 commit 8a8de5a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 17 deletions.
22 changes: 15 additions & 7 deletions services/process/server/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (s *server) GetStacks(ctx context.Context, req *pb.GetStacksRequest) (*pb.G
// New thread so append last entry and start over.
if fields[0] == "Thread" {
// Depending on wrapper/gdb this may have additional fields but we don't need them.
if len(fields) < 6 {
if len(fields) < 4 {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "unparsable pstack output for new thread: %s", text)
}
Expand All @@ -244,13 +244,21 @@ func (s *server) GetStacks(ctx context.Context, req *pb.GetStacksRequest) (*pb.G
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse thread number: %s : %v", text, err)
}
if n, err := fmt.Sscanf(fields[3], "0x%x", &stack.ThreadId); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse thread id: %s : %v", text, err)
if fields[2] == "(Thread" && len(fields) >= 6 {
if n, err := fmt.Sscanf(fields[3], "0x%x", &stack.ThreadId); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse thread id: %s : %v", text, err)
}
if n, err := fmt.Sscanf(fields[5], "%d", &stack.Lwp); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse lwp: %s : %v", text, err)
}
}
if n, err := fmt.Sscanf(fields[5], "%d", &stack.Lwp); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse lwp: %s : %v", text, err)
if fields[2] == "(LWP" {
if n, err := fmt.Sscanf(fields[3], "%d", &stack.Lwp); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse lwp: %s : %v", text, err)
}
}
numEntries++
continue
Expand Down
20 changes: 12 additions & 8 deletions services/process/server/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ var (
"./testdata/linux_bad3.ps", // bad nice value
}

testdataPstackNoThreads = "./testdata/linux_pstack_no_threads.txt"
testdataPstackNoThreadsTextProto = "./testdata/linux_pstack_no_threads.textproto"
testdataPstackThreads = "./testdata/linux_pstack_threads.txt"
testdataPstackThreadsTextProto = "./testdata/linux_pstack_threads.textproto"
testdataPstackThreadsBadThread = "./testdata/linux_pstack_threads_bad_thread.txt"
testdataPstackThreadsBadThreadNumber = "./testdata/linux_pstack_threads_bad_thread_number.txt"
testdataPstackThreadsBadThreadID = "./testdata/linux_pstack_threads_bad_thread_id.txt"
testdataPstackThreadsBadLwp = "./testdata/linux_pstack_threads_bad_lwp.txt"
testdataPstackNoThreads = "./testdata/linux_pstack_no_threads.txt"
testdataPstackNoThreadsTextProto = "./testdata/linux_pstack_no_threads.textproto"
testdataPstackThreads = "./testdata/linux_pstack_threads.txt"
testdataPstackThreadsTextProto = "./testdata/linux_pstack_threads.textproto"
testdataPstackSingleThread = "./testdata/linux_pstack_single_thread.txt"
testdataPstackSingleThreadTextProto = "./testdata/linux_pstack_single_thread.textproto"
testdataPstackSingleThreadWithLWP = "./testdata/linux_pstack_single_thread_with_lwp.txt"
testdataPstackSingleThreadWithLWPTextProto = "./testdata/linux_pstack_single_thread_with_lwp.textproto"
testdataPstackThreadsBadThread = "./testdata/linux_pstack_threads_bad_thread.txt"
testdataPstackThreadsBadThreadNumber = "./testdata/linux_pstack_threads_bad_thread_number.txt"
testdataPstackThreadsBadThreadID = "./testdata/linux_pstack_threads_bad_thread_id.txt"
testdataPstackThreadsBadLwp = "./testdata/linux_pstack_threads_bad_lwp.txt"
)
18 changes: 16 additions & 2 deletions services/process/server/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ func TestPstackNative(t *testing.T) {
})
testutil.FatalOnErr("can't get native pstack", err, t)

// We're a go program. We have multiple threads.
if len(resp.Stacks) <= 1 {
// We're a go program. We have one or more threads.
if len(resp.Stacks) < 1 {
t.Fatalf("Not enough threads in native response. Response: %+v", prototext.Format(resp))
}

Expand Down Expand Up @@ -372,6 +372,20 @@ func TestPstack(t *testing.T) {
validate: testdataPstackThreadsTextProto,
pid: 1,
},
{
name: "A program with a single thread",
command: testutil.ResolvePath(t, "cat"),
input: testdataPstackSingleThread,
validate: testdataPstackSingleThreadTextProto,
pid: 1,
},
{
name: "A program with a single thread with an lwp",
command: testutil.ResolvePath(t, "cat"),
input: testdataPstackSingleThreadWithLWP,
validate: testdataPstackSingleThreadWithLWPTextProto,
pid: 1,
},
{
name: "bad pid - zero",
command: testutil.ResolvePath(t, "cat"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
stacks : <
thread_number : 1
stacks : "#0 0x00000000000807ec in runtime.futex.abi0 ()"
stacks : "#1 0x000000000004556c in runtime.futexsleep ()"
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Thread 1 (process 2947021):
#0 0x00000000000807ec in runtime.futex.abi0 ()
#1 0x000000000004556c in runtime.futexsleep ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stacks : <
thread_number : 9
lwp : 19049
stacks : "#0 0x00000000000807ec in runtime.futex.abi0 ()"
stacks : "#1 0x000000000004556c in runtime.futexsleep ()"
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Thread 9 (LWP 19049 "go"):
#0 0x00000000000807ec in runtime.futex.abi0 ()
#1 0x000000000004556c in runtime.futexsleep ()

0 comments on commit 8a8de5a

Please sign in to comment.