<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/122387>122387</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[lldb] python API SBDebugger.GetOutputFileHandle() returns a file handle with the wrong mode for stdout/stderr
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ashgti
</td>
</tr>
</table>
<pre>
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:
```python
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:
- Debugger initializes with a `FILE*` https://github.com/llvm/llvm-project/blob/ba704d59569151f1b8b6552ed22a7b840f5e6256/lldb/source/Core/Debugger.cpp#L878
- Calling into this `StreamFile` constructor https://github.com/llvm/llvm-project/blob/ba704d59569151f1b8b6552ed22a7b840f5e6256/lldb/include/lldb/Host/StreamFile.h#L31C3-L31C13
- That calls into `NativeFile` https://github.com/llvm/llvm-project/blob/ba704d59569151f1b8b6552ed22a7b840f5e6256/lldb/source/Host/common/StreamFile.cpp#L30
- That sets the options to `options()` https://github.com/llvm/llvm-project/blob/ba704d59569151f1b8b6552ed22a7b840f5e6256/lldb/include/lldb/Host/File.h#L384
- That is initialized to `'r'` mode by default https://github.com/llvm/llvm-project/blob/ba704d59569151f1b8b6552ed22a7b840f5e6256/lldb/include/lldb/Host/File.h#L51
However, stdout and stderr should be set to `mode='w'` by default.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVc1u4zYQfhr6MohAUZYsHXTI2nE3wKJbYLfosSDFscSGJgVyFDd9-oKS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