[Lldb-commits] [PATCH] D41359: Add Utility/Environment class for handling... environments

Pavel Labath via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Dec 18 09:15:13 PST 2017


labath created this revision.
labath added reviewers: zturner, davide, jingham, clayborg.
Herald added subscribers: mgorny, emaste.

There was some confusion in the code about how to represent process
environment. Most of the code (ab)used the Args class for this purpose,
but some of it used a more basic StringList class instead. In either
case, the fact that the underlying abstraction did not provide primitive
operations for the typical environment operations meant that even a
simple operation like checking for an environment variable value was
several lines of code.

This patch adds a separate Environment class, which is essentialy a
llvm::StringMap<std::string> in disguise. To standard StringMap
functionality, it adds a couple of new functions, which are specific to
the environment use case:

- (most important) envp conversion for passing into execve() and likes. Instead of trying to maintain a constantly up-to-date envp view, it provides a function which creates a envp view on demand, with the expectation that this will be called as the very last thing before handing the value to the system function.
- insert(StringRef KeyEqValue) - splits KeyEqValue into (key, value) pair and inserts it into the environment map.
- compose(value_type KeyValue) - takes a map entry and converts in back into "KEY=VALUE" representation.

With this interface most of the environment-manipulating code becomes
one-liners. The only tricky part was maintaining compatibility in
SBLaunchInfo, which expects that the environment entries are accessible
by index and that the returned const char* is backed by the launch info
object (random access into maps is hard and the map stores the entry in
a deconstructed form, so we cannot just return a .c_str() value). To
solve this, I have the SBLaunchInfo convert the environment into the
"envp" form, and use it to answer the environment queries. Extra code is
added to make sure the envp version is always in sync.

(This also improves the layering situation as Args was in the Interpreter module
whereas Environment is in Utility.)


https://reviews.llvm.org/D41359

Files:
  include/lldb/API/SBLaunchInfo.h
  include/lldb/Host/Host.h
  include/lldb/Interpreter/Args.h
  include/lldb/Target/Platform.h
  include/lldb/Target/ProcessInfo.h
  include/lldb/Target/Target.h
  include/lldb/Utility/Environment.h
  packages/Python/lldbsuite/test/python_api/sblaunchinfo/TestSBLaunchInfo.py
  source/API/SBLaunchInfo.cpp
  source/API/SBPlatform.cpp
  source/API/SBProcess.cpp
  source/API/SBTarget.cpp
  source/Commands/CommandObjectProcess.cpp
  source/Host/freebsd/Host.cpp
  source/Host/linux/Host.cpp
  source/Host/macosx/Host.mm
  source/Host/netbsd/Host.cpp
  source/Host/openbsd/Host.cpp
  source/Host/posix/ProcessLauncherPosixFork.cpp
  source/Host/windows/Host.cpp
  source/Interpreter/Args.cpp
  source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
  source/Plugins/Platform/MacOSX/PlatformiOSSimulatorCoreSimulatorSupport.mm
  source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
  source/Plugins/Platform/POSIX/PlatformPOSIX.h
  source/Plugins/Platform/Windows/PlatformWindows.cpp
  source/Plugins/Platform/Windows/PlatformWindows.h
  source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
  source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
  source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
  source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
  source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
  source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp
  source/Target/Platform.cpp
  source/Target/Process.cpp
  source/Target/ProcessInfo.cpp
  source/Target/Target.cpp
  source/Utility/CMakeLists.txt
  source/Utility/Environment.cpp
  tools/lldb-server/lldb-gdbserver.cpp
  unittests/Host/HostTest.cpp
  unittests/Interpreter/TestArgs.cpp
  unittests/Utility/CMakeLists.txt
  unittests/Utility/EnvironmentTest.cpp
  unittests/tools/lldb-server/tests/TestClient.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41359.127378.patch
Type: text/x-patch
Size: 59057 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20171218/7ff9230a/attachment-0001.bin>


More information about the lldb-commits mailing list