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

TypeError: cannot use a bytes pattern on a string-like object #7

Closed
sbmthakur opened this issue Jul 13, 2022 · 5 comments · Fixed by #9
Closed

TypeError: cannot use a bytes pattern on a string-like object #7

sbmthakur opened this issue Jul 13, 2022 · 5 comments · Fixed by #9
Labels

Comments

@sbmthakur
Copy link

The following snippet crashes with the above error when it comes across a system call that passes the filter.

import traceback
import ptracer
import re
import os

def callback(syscall):
    print(syscall.name, syscall.result.raw_value)

flt = [
    ptracer.SysCallPattern(
        name='openat',
        args=[
            re.compile(b'/tmp/.*'),
            lambda arg: arg.value & os.O_WRONLY
        ],
        result=lambda res: res.value > 0
    )
]

with ptracer.context(callback, filter=flt):
    open('/tmp/domf', mode='w')

Here's the error stack.

$ py a.py 
Traceback (most recent call last):
  File "/home/shubham/workspace/tracer/a.py", line 21, in <module>
    open('/tmp/domf', mode='w')
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/__init__.py", line 106, in __exit__
    _context.disable()
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/__init__.py", line 92, in disable
    raise error
ptracer._ptracer.PtracerError: Unhandled exception in ptrace process
Traceback (most recent call last):
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_ptracer.py", line 276, in _debugger_thread
    _debugger_thread_inner(main_pid, dbgproc_started, dbgthread_stop,
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_ptracer.py", line 405, in _debugger_thread_inner
    elif filter_ is None or filter_(syscall):
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_ptracer.py", line 294, in <lambda>
    filter_ = lambda sc: any(m.match(sc) for m in syscall_filter)
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_ptracer.py", line 294, in <genexpr>
    filter_ = lambda sc: any(m.match(sc) for m in syscall_filter)
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_syscall.py", line 52, in match
    return all(m[1](m[0](syscall)) for m in self.matcher)
  File "/home/shubham/workspace/tracer/venv/lib/python3.9/site-packages/ptracer/_syscall.py", line 52, in <genexpr>
    return all(m[1](m[0](syscall)) for m in self.matcher)
TypeError: cannot use a bytes pattern on a string-like object
@jparise jparise added the bug label Jul 13, 2022
@jparise
Copy link
Collaborator

jparise commented Jul 13, 2022

Have you tried using a string (not bytes) pattern? re.compile('/tmp/.*')

@sbmthakur
Copy link
Author

No system calls are captured if I do that. Also, my co-worker just informed me that the filter works as expected if we remove the args argument from it.

@jparise
Copy link
Collaborator

jparise commented Jul 14, 2022

@sbmthakur #9 should fix the root issue.

Also note that openat takes a file descriptor as its first argument (unlike open), so you'll need to add a placeholder for that argument position to your pattern:

    ptracer.SysCallPattern(
        name='openat',
        args=[
            None,  # fd argument
            re.compile(b'/tmp/.*'),
            lambda arg: arg.value & os.O_WRONLY
        ],
        result=lambda res: res.value > 0
    )

Please let me know if this solves your problem.

@sbmthakur
Copy link
Author

The snippet is working as expected. Thanks for the fix.

@jparise
Copy link
Collaborator

jparise commented Jul 18, 2022

Great! I just published version 0.6.1 with this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants