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

[lldb] python API SBDebugger.GetOutputFileHandle() returns a file handle with the wrong mode for stdout/stderr #122387

Open
ashgti opened this issue Jan 9, 2025 · 5 comments
Labels

Comments

@ashgti
Copy link
Contributor

ashgti commented Jan 9, 2025

It looks like when lldb initializes the default OutputFileHandle and ErrorFileHandle on a new debugger instance it is not setting the handles as writable (mode='w').

Here is a simple python script using the lldb module to illustrate this:

import lldb

dbg = lldb.SBDebugger.Create()

print("output", dbg.GetOutputFileHandle()) #> output <_io.TextIOWrapper name=1 mode='r' encoding='UTF-8'>
print("error", dbg.GetErrorFileHandle()) #> error <_io.TextIOWrapper name=2 mode='r' encoding='UTF-8'>

I think this is happening because:

However, stdout and stderr should be set to mode='w' by default.

@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/issue-subscribers-lldb

Author: John Harrison (ashgti)

It looks like when lldb initializes the default OutputFileHandle and ErrorFileHandle on a new debugger instance it is not setting the handles as writable (mode='w').

Here is a simple python script using the lldb module to illustrate this:

import lldb

dbg = lldb.SBDebugger.Create()

print("output", dbg.GetOutputFileHandle()) #&gt; output &lt;_io.TextIOWrapper name=1 mode='r' encoding='UTF-8'&gt;
print("error", dbg.GetErrorFileHandle()) #&gt; error &lt;_io.TextIOWrapper name=2 mode='r' encoding='UTF-8'&gt;

I think this is happening because:

However, stdout and stderr should be set to mode='w' by default.

@DavidSpickett
Copy link
Collaborator

These handles are to the stdout/stderr of the debugger instance, rather than the program being debugged, right?

(as it would make sense that the debugee's output streams are read only)

@ashgti
Copy link
Contributor Author

ashgti commented Jan 10, 2025

Correct, this is where the debugger is writing output.

The reason this might be an issue is if you have a python script component and try to do something like:

print("Hello world", file=debugger.GetOutputFileHandle())

If the debugger is using the default stdout / stderr then the file handle is considered 'r' not 'w' so the print fails.

@jimingham
Copy link
Collaborator

jimingham commented Jan 10, 2025

Sadly, all our tests do SetOutputFileHandle first (because it's a pain to grub the test binary's stdout) so this wasn't noticed. And unless you change it programmatically, the Python script interpreter is going the same place as the debugger output, so I bet very few people go specifically to the debugger's output. Still, someone should fix this and add a test for the default file handle from the debugger.

As a side note, if you are writing lldb commands in Python, you should be writing to the command's return object not to the debugger stdout.

@ashgti
Copy link
Contributor Author

ashgti commented Jan 10, 2025

I have some scripts that are running some operations in a background thread, I'm mostly using the debugger.GetErrorFileHandle() to be able to write an error message if something fails in the background. I think by that point the result: lldb.SBCommandReturnObject object would have been cleaned up.

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

No branches or pull requests

5 participants