@@ -71,6 +71,27 @@ def sections(self):
7171
7272 return sections
7373
74+ def _filter_memory_call (call ):
75+ # mmap can operate on a fd or "MAP_ANONYMOUS" which gives a block of memory.
76+ # Ignore "MAP_ANONYMOUS + the "MAP_ANON" alias.
77+ if call .syscall == "mmap" and "MAP_ANON" in call .args [3 ]:
78+ return True
79+
80+ if call .syscall in ("munmap" , "mprotect" ):
81+ return True
82+
83+ return False
84+
85+
86+ def filter_memory (syscalls ):
87+ """Filter out memory allocation calls from File I/O calls.
88+
89+ Some calls (mmap, munmap, etc) can be used on files or to just get a block
90+ of memory. Use this function to filter out the memory related calls from
91+ other calls."""
92+
93+ return [call for call in syscalls if not _filter_memory_call (call )]
94+
7495
7596@support .requires_subprocess ()
7697def strace_python (code , strace_flags , check = True ):
@@ -93,8 +114,6 @@ def _make_error(reason, details):
93114 "-c" ,
94115 textwrap .dedent (code ),
95116 __run_using_command = [_strace_binary ] + strace_flags ,
96- # Don't want to trace our JIT's own mmap and mprotect calls:
97- PYTHON_JIT = "0" ,
98117 )
99118 except OSError as err :
100119 return _make_error ("Caught OSError" , err )
@@ -145,9 +164,14 @@ def get_events(code, strace_flags, prelude, cleanup):
145164 return all_sections ['code' ]
146165
147166
148- def get_syscalls (code , strace_flags , prelude = "" , cleanup = "" ):
167+ def get_syscalls (code , strace_flags , prelude = "" , cleanup = "" ,
168+ ignore_memory = True ):
149169 """Get the syscalls which a given chunk of python code generates"""
150170 events = get_events (code , strace_flags , prelude = prelude , cleanup = cleanup )
171+
172+ if ignore_memory :
173+ events = filter_memory (events )
174+
151175 return [ev .syscall for ev in events ]
152176
153177
@@ -177,5 +201,5 @@ def requires_strace():
177201 return unittest .skipUnless (_can_strace (), "Requires working strace" )
178202
179203
180- __all__ = ["get_events " , "get_syscalls " , "requires_strace " , "strace_python " ,
181- "StraceEvent" , "StraceResult" ]
204+ __all__ = ["filter_memory " , "get_events " , "get_syscalls " , "requires_strace " ,
205+ "strace_python" , " StraceEvent" , "StraceResult" ]
0 commit comments