This is an archive of the discontinued LLVM Phabricator instance.

[lldb] [Host] Refactor TerminalState
ClosedPublic

Authored by mgorny on Sep 29 2021, 8:10 AM.

Details

Summary

Refactor TerminalState to make the code simpler. Move 'struct termios'
to a PImpl-style subclass. Add an RAII interface to automatically store
and restore the state.

Diff Detail

Event Timeline

mgorny requested review of this revision.Sep 29 2021, 8:10 AM
mgorny created this revision.
mgorny added inline comments.Sep 29 2021, 8:13 AM
lldb/source/Host/common/Terminal.cpp
124

I think passing 0 here was a mistake. The Restore() method passed fd instead.

lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
358

This instance didn't seem to be used at all.

lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
464–465

I'm wondering if we could use the RAII approach here and restore state (if saved) in the destructor. I'm just not 100% sure if that's what Debugger expects.

mgorny updated this revision to Diff 375908.Sep 29 2021, 8:54 AM
mgorny retitled this revision from [lldb] [Host] Merge TerminalState into Terminal to [lldb] [Host] Refactor TerminalState [WIP].
mgorny edited the summary of this revision. (Show Details)

Ok, let's start with smaller refactoring and see where it gets us.

mgorny updated this revision to Diff 375919.Sep 29 2021, 9:08 AM
mgorny edited the summary of this revision. (Show Details)
mgorny updated this revision to Diff 375928.Sep 29 2021, 9:36 AM

Accept Terminal arg in place of fd.

mgorny retitled this revision from [lldb] [Host] Refactor TerminalState [WIP] to [lldb] [Host] Refactor TerminalState.Sep 29 2021, 9:37 AM
shafik added a subscriber: shafik.Sep 29 2021, 9:41 AM
shafik added inline comments.
lldb/include/lldb/Host/Terminal.h
15–16

I don't think we need this forward declaration anymore.

mgorny updated this revision to Diff 375945.Sep 29 2021, 10:19 AM

Fix tcgetpgrp() call (again). Remove unneeded forward decl.

mgorny marked an inline comment as done.Sep 29 2021, 10:20 AM
mgorny added inline comments.
lldb/include/lldb/Host/Terminal.h
15–16

You are correct, thanks!

Overall, this seems like an improvement, though the class is still quite far from what I would consider an ideal state.

lldb/include/lldb/Host/Terminal.h
15–16

This is kind of a step backwards, as in llvm one tries to not expose system headers (https://llvm.org/docs/SupportLibrary.html#don-t-expose-system-headers). Obviously, lldb is quite far from that.

I think this is actually what the this code whas trying to achieve, though forward-declaring 3rd party entities is also a somewhat questionable practice. Pimpling it is the canonical solution, although it has its (mostly mental) overhead.

47–49

I guess it would be good to delete the copy operations as well.

mgorny marked 2 inline comments as done.Sep 30 2021, 6:48 AM
mgorny added inline comments.
lldb/include/lldb/Host/Terminal.h
47–49

This is now implicit via PImpl; or do you prefer explicit?

mgorny updated this revision to Diff 376191.Sep 30 2021, 6:49 AM
mgorny edited the summary of this revision. (Show Details)

Move termios data to PImpl-style subclass.

mgorny updated this revision to Diff 376271.Sep 30 2021, 10:32 AM

Move saving/restoring termios back into TerminalState, make Impl a trivial struct.

labath accepted this revision.Oct 1 2021, 2:23 AM

Is anyone passing save_process_group = true now? If not, I'd really like to delete it, as the implementation is quite scary.

lldb/include/lldb/Host/Terminal.h
47–49

implicit is fine

130

Maybe call the class Data as well?

lldb/source/Host/common/Terminal.cpp
92

Could we make Save private now? Or even inline it into the constructor ?

This revision is now accepted and ready to land.Oct 1 2021, 2:23 AM
mgorny marked 2 inline comments as done.Oct 1 2021, 2:48 AM

Is anyone passing save_process_group = true now? If not, I'd really like to delete it, as the implementation is quite scary.

Yes, Debugger::SaveInputTerminalState() does.

lldb/source/Host/common/Terminal.cpp
92

Unfortunately, no. Debugger still manually saves/restores.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptOct 1 2021, 3:53 AM