[lldb-dev] How to redirect stdin/out/err to different pty?
Eran Ifrah
eran.ifrah at gmail.com
Sun Mar 23 08:07:01 PDT 2014
On Sun, Mar 23, 2014 at 4:29 PM, Piotr Rak <piotr.rak at gmail.com> wrote:
> Sorry I misinformed you about posix_spawn - it is not true for Linux and
> FreeBSD at least, it will use ordinary fork.
>
>
> 2014-03-23 15:23 GMT+01:00 Piotr Rak <piotr.rak at gmail.com>:
>
>
>> Hi,
>>
>> 2014-03-23 14:04 GMT+01:00 Eran Ifrah <eran.ifrah at gmail.com>:
>>
>> Thanks for your pointer Piotr. Here is the code I am using to open a
>>> pseudo-terminal (there is the UI part, which I left out):
>>>
>>> char __name[128];
>>> memset(__name, 0, sizeof(__name));
>>>
>>> int master(-1);
>>> m_slave = -1;
>>> if(openpty(&master, &m_slave, __name, NULL, NULL) != 0)
>>> return wxT("");
>>>
>>> // disable ECHO
>>> struct termios termio;
>>> tcgetattr(master, &termio);
>>> termio.c_lflag = ICANON;
>>> termio.c_oflag = ONOCR | ONLRET;
>>> tcsetattr(master, TCSANOW, &termio);
>>>
>>> m_tty = wxString(__name, wxConvUTF8);
>>>
>>> At the end, m_tty contains a string name (e.g. /dev/pts/19 ).
>>> Note that the above code works flawlessly when using it with gdb (i.e. if
>>> I pass this "/dev/pts/19" to gdb's switch -tty=/dev/pts/19 I will get
>>> all the inferior output/err/input to my internal terminal)
>>>
>>> However, doing the same with LLDB (using C++ API not the command line ,
>>> i.e. passing "/dev/pts/19" as an argument to SBTarget::Launch(...)) I get
>>> nothing as output...
>>>
>>> Looks sane to me.
>>
>>
>>> Also, I am not sure I am following the idea behind replacing the
>>> "Launch" function with my own fork(), looking at the code of Launch()
>>> suggests that it does more than a simple fork...
>>>
>>>
>> That was my idea to debug issue if nothing else helps.
>> Or rather bisect on which side it really is, sorry if I did not make it
>> clear...
>> So I was trying suggest replacing SBTarget::Launch with fork, write to
>> child stdout/err, and see if that works alone....
>>
>> SBTarget::Launch is usually actually posix_spawn right now, it uses
>> posix_spawnattr_addopen to open descriptors for your specified paths, and
>> should open it 3 times - given current implementation - even it is just one
>> file.
>>
>> Have you inspected SBProcess and SBError returned by SBTarget::Launch?
>>
>> I checked IsValid() on both and its OK for both. I can actually run
"next" Continue etc and seems to be working. Its just that I can't seem to
redirect the stdout/err to my own console.
Do you see your inferior process is indeed launching, just not displaying
>> anything?
>>
> Yes, ps -ef shows the debugee
>
>> Do you have an option to check if those terminals are actually being
>> opened (like examining /proc/<pid>/fd for linux)?
>>
> The terminal is opened. Like I mentioned in my previous email, using the
_same_ code with gdb works
I also have a standalone terminal application which I wrote which is also
using the same set of classes all of the are working for couple of years
now without any problems
I also tried this:
I typed in my konsole 'tty' and used that as the input for Launch - it also
seems to have no effect
>> Good luck,
>> /Piotr
>>
>> Any more hints?
>>> Eran
>>>
>>>
>>>
>>> On Sat, Mar 22, 2014 at 9:36 PM, Piotr Rak <piotr.rak at gmail.com> wrote:
>>>
>>>> Hi,
>>>>
>>>> It should.
>>>> Have you opened master pseudoterminal like?:
>>>>
>>>> int fd = posix_openpt(flags); // open("/dev/ptmx") might work here
>>>> too but less portable;
>>>> grantpt(fd);
>>>> unlockpt(fd);
>>>>
>>>> Depending on target you might need some bizarre ioctls here, but
>>>> assuming you are using Linux/FreeBSD/MacOSX
>>>> you should be fine.
>>>>
>>>> If you had already master pseudo-terminal file descriptor you can skip
>>>> steps above.
>>>>
>>>> You can use ptsname for master file descriptor it will return you name
>>>> of slave pseudo-terminal for your master.
>>>> Later you can pass name returned by ptsname(fd) as Launch arguments.
>>>>
>>>> If above won't work you can try replacing Launch() call with ordinary
>>>> fork, and in child process:
>>>>
>>>> slavefd = open(slavename, O_RDWR);
>>>>
>>>> dup2(0, slavefd);
>>>> dup2(1, slavefd);
>>>> dup2(2, slavefd);
>>>>
>>>> And see if that works alone for you...
>>>>
>>>> Good luck,
>>>> /Piotr
>>>>
>>>>
>>>> 2014-03-22 19:29 GMT+01:00 Eran Ifrah <eran.ifrah at gmail.com>:
>>>>
>>>>> Hello,
>>>>>
>>>>> I am trying to use the C++ API with good success so far.
>>>>> I am now at a point where I want to redirect stdin/out/err of the
>>>>> inferior to my application (my application creates a separate pseudo
>>>>> terminal window)
>>>>>
>>>>> Looking at the SBTarget::Launch, I thought that simply passing
>>>>> "/dev/pts/<some-number>" as the 3rd, 4th and 5th argument will do the trick
>>>>> .. well, it did not.
>>>>> I am missing something basic here, can anyone shed some light please?
>>>>> or give an example (better) of how to achieve this?
>>>>>
>>>>> Thanks!
>>>>>
>>>>> --
>>>>> Eran Ifrah
>>>>> Author of codelite, a cross platform open source C/C++ IDE:
>>>>> http://www.codelite.org
>>>>> wxCrafter, a wxWidgets RAD: http://wxcrafter.codelite.org
>>>>>
>>>>> _______________________________________________
>>>>> lldb-dev mailing list
>>>>> lldb-dev at cs.uiuc.edu
>>>>> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev
>>>>>
>>>>>
>>>>
>>>
>>>
>>> --
>>> Eran Ifrah
>>> Author of codelite, a cross platform open source C/C++ IDE:
>>> http://www.codelite.org
>>> wxCrafter, a wxWidgets RAD: http://wxcrafter.codelite.org
>>>
>>
>>
>
--
Eran Ifrah
Author of codelite, a cross platform open source C/C++ IDE:
http://www.codelite.org
wxCrafter, a wxWidgets RAD: http://wxcrafter.codelite.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20140323/296d0ceb/attachment.html>
More information about the lldb-dev
mailing list