From 33251d10ece3af6a201333fe80bb4b1eb1364ce7 Mon Sep 17 00:00:00 2001 From: Pierre Gimalac Date: Wed, 14 Aug 2024 18:48:35 +0200 Subject: [PATCH 1/2] fix(proc): parsing of smaps path --- process/process_linux.go | 4 +- process/process_linux_test.go | 67 ++++++++++++++++++++++++++++++++++ process/testdata/linux/1/smaps | 44 ++++++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 process/testdata/linux/1/smaps diff --git a/process/process_linux.go b/process/process_linux.go index 8f1d6c89c..e96f7fb05 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -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 = firstLine[5] + } for _, line := range block { if strings.Contains(line, "VmFlags") { diff --git a/process/process_linux_test.go b/process/process_linux_test.go index 9b9b77a88..19e42a172 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -12,6 +12,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSplitProcStat(t *testing.T) { @@ -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) +} diff --git a/process/testdata/linux/1/smaps b/process/testdata/linux/1/smaps new file mode 100644 index 000000000..81aa5d886 --- /dev/null +++ b/process/testdata/linux/1/smaps @@ -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 From a1f9e69b5d7c4acf8a61d720783371d49fcf857a Mon Sep 17 00:00:00 2001 From: Pierre Gimalac Date: Mon, 19 Aug 2024 10:34:55 +0200 Subject: [PATCH 2/2] fix: join path elements in case it contains spaces --- process/process_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/process/process_linux.go b/process/process_linux.go index e96f7fb05..103c3efe2 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -400,7 +400,7 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M getBlock := func(firstLine []string, block []string) (MemoryMapsStat, error) { m := MemoryMapsStat{} if len(firstLine) >= 6 { - m.Path = firstLine[5] + m.Path = strings.Join(firstLine[5:], " ") } for _, line := range block {