Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of /proc/pid/smaps when path is empty #1691

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion process/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,9 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M
// function of parsing a block
getBlock := func(firstLine []string, block []string) (MemoryMapsStat, error) {
m := MemoryMapsStat{}
m.Path = firstLine[len(firstLine)-1]
if len(firstLine) >= 6 {
m.Path = strings.Join(firstLine[5:], " ")
}

for _, line := range block {
if strings.Contains(line, "VmFlags") {
Expand Down
67 changes: 67 additions & 0 deletions process/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSplitProcStat(t *testing.T) {
Expand Down Expand Up @@ -174,3 +175,69 @@ func TestFillFromTIDStatWithContext_lx_brandz(t *testing.T) {
assert.Equal(t, float64(0), cpuTimes.Iowait)
}
}

func TestProcessMemoryMaps(t *testing.T) {
t.Setenv("HOST_PROC", "testdata/linux")
pid := 1
p, err := NewProcess(int32(pid))
require.NoError(t, err)
maps, err := p.MemoryMaps(false)
require.NoError(t, err)

expected := &[]MemoryMapsStat{
{
"[vvar]",
0,
1,
0,
3,
4,
5,
6,
7,
8,
9,
},
{
"",
0,
1,
2,
3,
4,
0,
6,
7,
8,
9,
},
{
"[vdso]",
0,
1,
2,
3,
4,
5,
0,
7,
8,
9,
},
{
"/usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1",
0,
1,
2,
3,
4,
5,
6,
7,
0,
9,
},
}

require.Equal(t, expected, maps)
}
44 changes: 44 additions & 0 deletions process/testdata/linux/1/smaps
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ffffb5ecc000-ffffb5ece000 r--p 00000000 00:00 0 [vvar]
Rss: 0 kB
KernelPageSize: 4 kB
Size: 1 kB
Shared_Clean: 3 kB
Shared_Dirty: 4 kB
Private_Clean: 5 kB
Private_Dirty: 6 kB
Referenced: 7 kB
Anonymous: 8 kB
Swap: 9 kB
ffffb5eca000-ffffb5ecc000 rw-p 00000000 00:00 0
Rss: 0 kB
Size: 1 kB
Pss: 2 kB
Shared_Clean: 3 kB
Shared_Dirty: 4 kB
Private_Dirty: 6 kB
LazyFree: 0 kB
Referenced: 7 kB
Anonymous: 8 kB
Swap: 9 kB
ffffb5ece000-ffffb5ecf000 r-xp 00000000 00:00 0 [vdso]
Rss: 0 kB
Size: 1 kB
Pss: 2 kB
Shared_Clean: 3 kB
Shared_Dirty: 4 kB
Private_Clean: 5 kB
Private_Hugetlb: 0 kB
Referenced: 7 kB
Anonymous: 8 kB
Swap: 9 kB
ffffb5ecf000-ffffb5ed1000 r--p 0002a000 00:3d 2238525 /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1
Rss: 0 kB
Size: 1 kB
Pss: 2 kB
Shared_Clean: 3 kB
Shared_Dirty: 4 kB
Private_Clean: 5 kB
Private_Dirty: 6 kB
Referenced: 7 kB
THPeligible: 0
Swap: 9 kB
Loading