giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.31k stars 1.39k forks source link

[aarch64] IndexError on calling p.memory_maps() #2430

Open nehaljwani opened 4 months ago

nehaljwani commented 4 months ago

Summary

Description

I'm running a cross compiled python for aarch64. I've a setup qemu-aarch64 as the interpreter at /proc/sys/fs/binfmt_misc/qemu-aarch64 to be able to run the cross compiled program.

>>> import psutil
>>> import os
>>> p = psutil.Process(os.getpid())
>>> p.memory_maps()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/prefix/lib/python3.12/site-packages/psutil/__init__.py", line 1178, in memory_maps
    it = self._proc.memory_maps()
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/prefix/lib/python3.12/site-packages/psutil/_pslinux.py", line 1717, in wrapper
    return fun(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/prefix/lib/python3.12/site-packages/psutil/_pslinux.py", line 2091, in memory_maps
    for header, data in get_blocks(lines, current_block):
  File "/path/to/prefix/lib/python3.12/site-packages/psutil/_pslinux.py", line 2069, in get_blocks
    data[fields[0]] = int(fields[1]) * 1024
                          ~~~~~~^^^
IndexError: list index out of range

As one can see, there is no value in front of VmFlags (in the very first entry):

$ python3 -c "print(repr(open('/proc/self/smaps').read()))"|fmt -
$ /path/to/prefix/python3 -c "print(repr(open('/proc/self/smaps').read()))"|fmt - | head -30
'400000000000-400000001000 ---p 00000000 00:00 0
\nSize:                  4 kB\nKernelPageSize:        4 kB\nMMUPageSize:
4 kB\nRss:                   0 kB\nPss:                   0 kB\nPss_Dirty:
0 kB\nShared_Clean:          0 kB\nShared_Dirty:          0
kB\nPrivate_Clean:         0 kB\nPrivate_Dirty:         0
kB\nReferenced:            0 kB\nAnonymous:             0
kB\nLazyFree:              0 kB\nAnonHugePages:         0
kB\nShmemPmdMapped:        0 kB\nFilePmdMapped:         0
kB\nShared_Hugetlb:        0 kB\nPrivate_Hugetlb:       0 kB\nSwap:
0 kB\nSwapPss:               0 kB\nLocked:                0
kB\nTHPeligible:    0\nVmFlags:\n400000001000-400000801000 rw-p 00000000
00:00 0                          [stack]\nSize:                  8192
kB\nKernelPageSize:        4 kB\nMMUPageSize:           4 kB\nRss:
0 kB\nPss:                   0 kB\nPss_Dirty:             0
kB\nShared_Clean:          0 kB\nShared_Dirty:          0
kB\nPrivate_Clean:         0 kB\nPrivate_Dirty:         0 kB\nReferenced:
0 kB\nAnonymous:             0 kB\nLazyFree:              0
kB\nAnonHugePages:         0 kB\nShmemPmdMapped:        0
kB\nFilePmdMapped:         0 kB\nShared_Hugetlb:        0
kB\nPrivate_Hugetlb:       0 kB\nSwap:                  0 kB\nSwapPss:
0 kB\nLocked:                0 kB\nTHPeligible:    0\nVmFlags:
rd wr mr mw\n400000801000-40000081f000 r-xp 00000000 00:1c 23501397
...

The following diff resolves the issue, but I'm not 100% sure if it is the right fix as it seems more like a workaround:

diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 16718388..df183d01 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -2067,7 +2067,7 @@ class Process:
                     else:
                         try:
                             data[fields[0]] = int(fields[1]) * 1024
-                        except ValueError:
+                        except (ValueError, IndexError):
                             if fields[0].startswith(b'VmFlags:'):
                                 # see issue #369
                                 continue

Interestingly enough, if I read the /proc/<pid>/smaps file directly, VmFlags isn't unset:

$ cat /proc/2583591/smaps | head -30
00400000-00401000 r--p 00000000 00:1c 287073                             /opt/qemu-user/bin/qemu-aarch64
Size:                  4 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Rss:                   4 kB
Pss:                   4 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         4 kB
Private_Dirty:         0 kB
Referenced:            4 kB
Anonymous:             0 kB
LazyFree:              0 kB
AnonHugePages:         0 kB
ShmemPmdMapped:        0 kB
FilePmdMapped:         0 kB
Shared_Hugetlb:        0 kB
Private_Hugetlb:       0 kB
Swap:                  0 kB
SwapPss:               0 kB
Locked:                0 kB
THPeligible:    0
ProtectionKey:         0
VmFlags: rd mr mw me
...