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

Is there a way to capture 'stderr' / 'stdout' from the ptraced program? #79

Open
nrathaus opened this issue Mar 15, 2023 · 2 comments
Open

Comments

@nrathaus
Copy link

Let assume I have this code (it is incomplete):

pid = createChild(arguments, False, env)
dbg = PtraceDebugger()
process = dbg.addProcess(pid, is_attached)
process.cont()

There seems to be no way to handle the stderr and stdout, there are something called pass_fd in the createChild function, but there is no documentation (that I can find) how to do it, trying to use os.pipe() on it seems to not work os.read(..) seems to be blocked

Can you provide an example? or direct me in the right way?

@spoutn1k
Copy link
Collaborator

The pass_fds argument compliments the close_fds and allows control over the file descriptors to have the child inherit, however stdout/err are not accessible this way. Glancing at the code there is no way to control the value of those streams, however this might not be too hard to implement.

What is your use case ? Are you looking for a Popen style stdout/stderr ?

@nrathaus
Copy link
Author

The idea was to trace a program/debug it and capture its stdout/stderr so that you can see what is going on

At the moment the output is seen "locally" where python runs, but if you are trying to automate the debugging process - and don't have terminal visibility where the program runs, this is lost

Think of it like this:
[My Python] -> [python-trace] -> [App run]

The output of stdout/stderr from [App Run] cannot be piped back to [My Python]

If I would have run GDB I could do this:

    args = [
        "/bin/gdb",
        "-q",
        "-ex",
        "set debuginfod enabled off",
        "-ex",
        "catch throw",
        "-ex",
        "run",
        "-ex",
        "continue",
        "-ex",
        "backtrace",
        "-ex",
        "detach",
        "-ex",
        "quit",
        "--args",
    ] + sys.argv[1:]
    
    process = subprocess.Popen(
        args=args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )

    stdout, stderr = process.communicate()
    if isinstance(stdout, bytes):
        stdout = stdout.decode()

    if isinstance(stderr, bytes):
        stderr = stderr.decode()

    print(f"stdout:\n{stdout}")
    print(f"stderr:\n{stderr}")

This would use gdb to capture exceptions (both 'throws' as well as code crashes) as well as using Popen capture the stdout/stderr

Though unfortunately I do not have distinction here what is program output and what is gdb output

Let me know if you need more insight into the idea I am trying to achieve

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

No branches or pull requests

2 participants