diff options
author | Greg Clayton <gclayton@apple.com> | 2014-01-27 23:43:24 +0000 |
---|---|---|
committer | Greg Clayton <gclayton@apple.com> | 2014-01-27 23:43:24 +0000 |
commit | 44d937820b451296f80449ac3de296345e80f183 (patch) | |
tree | 00d14538eded9f7e8bd075128d6ad5fd5ac1caed /lldb/source/Host/common/File.cpp | |
parent | f1cb16e481a60dbac58affcf8c8e423dac3937cb (diff) | |
download | bcm5719-llvm-44d937820b451296f80449ac3de296345e80f183.tar.gz bcm5719-llvm-44d937820b451296f80449ac3de296345e80f183.zip |
Merging the iohandler branch back into main.
The many many benefits include:
1 - Input/Output/Error streams are now handled as real streams not a push style input
2 - auto completion in python embedded interpreter
3 - multi-line input for "script" and "expression" commands now allow you to edit previous/next lines using up and down arrow keys and this makes multi-line input actually a viable thing to use
4 - it is now possible to use curses to drive LLDB (please try the "gui" command)
We will need to deal with and fix any buildbot failures and tests and arise now that input/output and error are correctly hooked up in all cases.
llvm-svn: 200263
Diffstat (limited to 'lldb/source/Host/common/File.cpp')
-rw-r--r-- | lldb/source/Host/common/File.cpp | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/lldb/source/Host/common/File.cpp b/lldb/source/Host/common/File.cpp index bbd11858aab..ab61b393c54 100644 --- a/lldb/source/Host/common/File.cpp +++ b/lldb/source/Host/common/File.cpp @@ -76,8 +76,9 @@ FILE * File::kInvalidStream = NULL; File::File(const char *path, uint32_t options, uint32_t permissions) : m_descriptor (kInvalidDescriptor), m_stream (kInvalidStream), - m_options (0), - m_owned (false) + m_options (), + m_own_stream (false), + m_own_descriptor (false) { Open (path, options, permissions); } @@ -88,7 +89,8 @@ File::File (const FileSpec& filespec, m_descriptor (kInvalidDescriptor), m_stream (kInvalidStream), m_options (0), - m_owned (false) + m_own_stream (false), + m_own_descriptor (false) { if (filespec) { @@ -100,7 +102,8 @@ File::File (const File &rhs) : m_descriptor (kInvalidDescriptor), m_stream (kInvalidStream), m_options (0), - m_owned (false) + m_own_stream (false), + m_own_descriptor (false) { Duplicate (rhs); } @@ -141,7 +144,7 @@ File::SetDescriptor (int fd, bool transfer_ownership) if (IsValid()) Close(); m_descriptor = fd; - m_owned = transfer_ownership; + m_own_descriptor = transfer_ownership; } @@ -155,10 +158,31 @@ File::GetStream () const char *mode = GetStreamOpenModeFromOptions (m_options); if (mode) { + if (!m_own_descriptor) + { + // We must duplicate the file descriptor if we don't own it because + // when you call fdopen, the stream will own the fd +#ifdef _WIN32 + m_descriptor = ::_dup(GetDescriptor()); +#else + m_descriptor = ::fcntl(GetDescriptor(), F_DUPFD); +#endif + m_own_descriptor = true; + } + do { m_stream = ::fdopen (m_descriptor, mode); } while (m_stream == NULL && errno == EINTR); + + // If we got a stream, then we own the stream and should no + // longer own the descriptor because fclose() will close it for us + + if (m_stream) + { + m_own_stream = true; + m_own_descriptor = false; + } } } } @@ -172,7 +196,7 @@ File::SetStream (FILE *fh, bool transfer_ownership) if (IsValid()) Close(); m_stream = fh; - m_owned = transfer_ownership; + m_own_stream = transfer_ownership; } Error @@ -194,7 +218,7 @@ File::Duplicate (const File &rhs) else { m_options = rhs.m_options; - m_owned = true; + m_own_descriptor = true; } } else @@ -272,7 +296,10 @@ File::Open (const char *path, uint32_t options, uint32_t permissions) if (!DescriptorIsValid()) error.SetErrorToErrno(); else - m_owned = true; + { + m_own_descriptor = true; + m_options = options; + } return error; } @@ -328,27 +355,22 @@ Error File::Close () { Error error; - if (IsValid ()) + if (StreamIsValid() && m_own_stream) { - if (m_owned) - { - if (StreamIsValid()) - { - if (::fclose (m_stream) == EOF) - error.SetErrorToErrno(); - } - - if (DescriptorIsValid()) - { - if (::close (m_descriptor) != 0) - error.SetErrorToErrno(); - } - } - m_descriptor = kInvalidDescriptor; - m_stream = kInvalidStream; - m_options = 0; - m_owned = false; + if (::fclose (m_stream) == EOF) + error.SetErrorToErrno(); + } + + if (DescriptorIsValid() && m_own_descriptor) + { + if (::close (m_descriptor) != 0) + error.SetErrorToErrno(); } + m_descriptor = kInvalidDescriptor; + m_stream = kInvalidStream; + m_options = 0; + m_own_stream = false; + m_own_descriptor = false; return error; } |